earthit / config-loader
提供对文件或环境变量中结构化配置值的访问
1.0.0
2017-10-19 20:54 UTC
Requires
- php: >=5.2
Requires (Dev)
- togos/simpler-test: ^1.0.1
This package is auto-updated.
Last update: 2024-09-20 04:15:25 UTC
README
提供对存储在JSON文件和/或环境变量中的结构化配置变量的访问。
示例
假设config/database.json
看起来像
{ "driver": "postgres", "host": "db.example.org", "dbname": "OVERRIDE ME PLZ THX", "notes": [ "This is the greatest database configuration.", "My brother Bob said so." ] }
然后我们将用以下环境变量运行我们的PHP脚本
database_dbname=bobco otherthing_json='{"foo":"bar","baz","quux"}' otherthing_foo="jk actually not bar"
然后我们有一个这样的脚本
$loader = new EarthIT_ConfigLoader( 'database', $_ENV ) echo "Database: "; var_export($loader->get('database')); echo "Other thing: "; var_export($loader->get('otherthing'));
输出可能如下
Database: array (
driver => 'postgres',
host => 'db.example.org',
dbname => 'bobco',
notes => array (
'This is the greatest database configuration.',
'My brother Bob said so.'
)
)
Other thing: array (
'foo' => 'jk actually not bar',
'baz' => 'quux'
)
覆盖
变量按以下顺序合并,后面的步骤会“覆盖”前面步骤的值
- JSON文件
- 子目录中的文件(例如,'foo/bar.json'可以覆盖'foo.json'中的'bar')
- JSON编码的环境变量
- 叶子环境变量
如果数组值覆盖了另一个数组值,它们将合并而不是替换旧值。
环境变量名称
-
下划线有特殊含义,所以如果你有一个变量
foo_bar_baz=42
,它将作为array('foo'=>array('bar'=>array('baz'=>42)))
显示在从ConfigLoader获取时 -
后缀
json
也有特殊含义。这意味着由"_json"之前的部分命名的变量的值将由解码该环境变量的值来确定。