weew / config
简单的配置加载器。
Requires
- symfony/yaml: ^3.0
- weew/contracts: ^1.1
- weew/helpers-array: ^1.0
- weew/helpers-filesystem: ^1.0
Requires (Dev)
- phpunit/phpunit: ^4.7
- satooshi/php-coveralls: ^0.6.1
This package is not auto-updated.
Last update: 2024-09-14 18:58:30 UTC
README
目录
安装
composer require weew/config
加载配置
配置加载器负责加载配置文件。您只需提供配置文件的位置(文件或目录),它就会扫描位置以找到您的配置文件,并返回一个配置对象。
$loader = new ConfigLoader(); $loader->addPath('path/to/my/config'); $config = $loader->load();
访问配置
您可以通过配置键轻松访问配置值。
// get config value $config->get('key', 'defaultValue'); // set config value $config->set('key', 'value'); // check if config is set $config->has('key'); // remove config value $config->remove('key'); // check if config is set, throws MissingConfigException // optionally a type may be specified (string, int, array, etc..) $config ->ensure('key', 'errorMessage') ->ensure('stringNode', 'errorMessage', 'string') ->ensure('arrayNode', 'errorMessage', 'array');
配置格式
以下格式是开箱即用的。
纯PHP格式示例
return [ 'key' => 'value', 'list' => [ 'key' => 'value', ], ];
INI格式示例
key = value [list] key = value
YAML格式示例
key: 'value' list: key: value
JSON格式示例
{ "key": "value", "list": { "key": "value", } }
参考
您可以从配置文件中引用其他配置值。要创建引用,只需将配置键用花括号 `{config.key}` 包围即可。
// config1 return [ 'list' => [ 'foo' => 'bar' ] ]; // config2 return [ 'reference' => 'foo {list.foo}' ]; // returns 'foo bar' $config->get('reference');
您甚至可以引用整个配置块。
// config1 return [ 'list' => [ 'foo' => 'bar' ] ]; // config2 return [ 'reference' => '{list}' ]; // returns ['foo' => 'bar'] $config->get('reference');
现在当您访问 `reference` 值时,您将返回 "bar"。请注意,引用在访问时进行插值(当您调用 $config->get() 时)。这意味着如果您更改配置值,所有引用它的人都将收到更新后的值而不是旧值。
运行时配置
有时您可能希望从数组或类似结构中应用运行时配置,这些结构也具有高于从文件系统中加载的配置的优先级。
$loader->addRuntimeConfig(['my' => 'config']); // or $loader->addRunetimeConfig(new Config(['my' => 'config']));
环境
通常您希望将配置分割到多个文件或目录中,并根据您的当前环境加载它们。这可以通过环境设置来实现。
设置环境
开箱即用支持dev、test和prod环境。可以动态添加自定义环境。
$loader->setEnviroonment('prod');
工作原理
要了解环境检测的工作原理,让我们看看这个目录结构
- test
- db.php
- prod
- db.php
- config.php
- config_test.php
- config_prod.php
在测试环境中,只有 "test" 目录及其内容("config_test.php" 和 "config.php")将被加载。然而,在prod环境中,它将只加载 "prod" 目录、"config_prod.php" 和 "config.php"。
已添加到配置加载器的文件和文件夹将按注册顺序加载。在目录内,文件按字母顺序加载。
添加自定义环境
要创建自己的环境,您必须在环境检测器上注册一个新的规则。第一个参数是环境的名称,第二个是一个掩码数组。
$loader->getEnvironmentDetector() ->addEnvironmentRule('integ', ['integ', 'integration', 'stage']);
以下是匹配集成环境的某些文件和目录的列表
- stage
- _stage
- _stage_
- foo_stage
- _stage.txt
- foo_stage.txt
以下文件不会匹配
- stagefoo
- foostage
- foo_stage_bar
- _stagebar
作为分隔符,您可以使用以下字符中的任意一个: ._+:-
。
忽略文件
以下文件默认被忽略
- dist
- _dist
- _dist_
- foo_dist
- _dist.txt
- foo_dist.txt
- ignore
- _ignore
- _ignore_
- foo_ignore
- _ignore.txt
- foo_ignore.txt
您可以指定自定义规则来忽略某些文件
$loader->getEnvironmentDetector() ->addIgnoreRule('sample', ['dist', 'ignore', 'sample']);
扩展
配置加载器提供了多个扩展点来修改其行为和功能。
自定义配置驱动器
添加您自己的驱动器非常简单。您只需实现 IConfigDriver 接口并将驱动器的实例传递给配置加载器。您可以同时拥有多个活动驱动器。
class MyDriver implements IConfigDriver {} $loader->addDriver(new MyDriver());
自定义环境检测器
您可以替换默认的环境检测器以使用自己的。只需创建一个新的检测器,该检测器实现了 IEnvironmentDetector 接口,并将其传递给配置加载器。还可以查看如何 创建您自己的环境。
class MyDetector implements IEnvironmentDetector {} $loader->setEnvironmentDetector(new MyDetector());