brightnucleus / config
最小化、可重用配置组件。
Requires
- brightnucleus/exceptions: >=0.2
- symfony/options-resolver: >=2.8
Requires (Dev)
- malukenho/docheader: ^0.1.5
- mikey179/vfsstream: ^1.6
- phpunit/phpunit: ^4.0|^5.0
- squizlabs/php_codesniffer: ^2.8
This package is auto-updated.
Last update: 2024-09-12 20:12:14 UTC
README
这是一个非常轻量级的配置组件,用于帮助您编写可重用代码。它只提供基本功能,并旨在在库和小型项目中使用。如果您需要用于更复杂项目的配置组件,请查看 Symfony 配置组件。
目录
安装
通过 Composer 使用此组件是最佳方式
composer require brightnucleus/config
基本用法
希望成为可配置的类应在其构造函数中接受一个 ConfigInterface
,以便可以注入配置。然后,周围的代码应注入一个对象实例(例如,该组件提供的通用 Config
)。这样,接受配置的类可以以 100% 可重用的方式编写,而所有特定于项目的组件将通过配置注入。
处理配置数据
检查键的存在性
要检查配置是否具有某个键,您可以使用 ConfigInterface::hasKey($key)
方法,或者如果您在类中使用 ConfigTrait
,则可以使用便利方法 $this->hasConfigKey($key)
。
获取键的值
要获取某个键的配置值,您可以使用 ConfigInterface::getKey($key)
方法,或者如果您在类中使用 ConfigTrait
,则可以使用便利方法 $this->getConfigKey($key)
。
如果您在配置文件中使用闭包,您还可以使用由 ConfigTrait
提供的便利函数 $this->getConfigCallable( $key, array $args )
,该函数将立即执行闭包,并通过传递提供的参数返回结果值。
嵌套键
如果您的键是嵌套的,您可以在一个请求中提供多个键级别。因此,每次您需要提供一个键并希望使用嵌套键时,您可以通过提供以逗号分隔的键列表($this->getConfigKey( 'level1', 'level2', 'level3' );
)或包含键列表的字符串($this->getConfigKey( 'level1/level2/level3' );
)来实现。
您可以按需混合和匹配这两种方法。
默认分隔符是: /
、\
和 .
。您可以通过将分隔符数组作为第四个参数传递给 Config
的构造函数来选择不同的分隔符。
示例 - 配置文件
下面的代码片段显示了配置文件的基本结构。
<?php namespace BrightNucleus\Example; /* * Example class main settings. */ $example = [ 'test_key' => 'test_value', ]; return [ 'BrightNucleus' => [ 'Example' => $example, ], ];
示例 - 可配置类
以下是向插件提供配置的示例设置。
<?php namespace BrightNucleus\Example; use BrightNucleus\Config\ConfigInterface; use BrightNucleus\Config\ConfigTrait; use BrightNucleus\Exception\RuntimeException; class Example { use ConfigTrait; /** * Instantiate an Example object. * * @param ConfigInterface $config Config to parametrize the object. * @throws RuntimeException If the Config could not be parsed correctly. */ public function __construct( ConfigInterface $config ) { $this->processConfig( $config ); } /** * Do something. */ public function run() { $key = 'test_key'; return sprintf( _( 'The value of the config key "$1%s" is "$2%s".'), $key, $this->getConfigKey( $key ) ); } }
示例 - 将配置获取到类中
您可以将所有配置合并到 1 个文件中,并仅使用 getSubConfig()
方法将“子配置”传递给各个组件。这样,您可以避免为每个组件进行额外的文件访问和额外的验证过程。
要创建一个 ConfigInterface
的新实例,您应该使用 ConfigFactory
。基本方法 ConfigFactory::create()
可以接受一个值数组,或者一个或多个文件名(作为字符串的绝对路径)。
如果您提供了一个以逗号分隔的文件名列表,它们将按顺序处理,直到成功加载第一个文件。
有一个便利函数 ConfigFactory::createSubConfig()
可以立即从已加载的配置文件中获取子配置。这允许您快速跳过供应商/包前缀,并将相关数据仅传递给新对象。
以下是您如何将配置文件传递给类的方法
<?php namespace BrightNucleus\Example; use BrightNucleus\Config\ConfigFactory; function init() { $configFile = __DIR__ . '/config/example_settings.php'; $config = ConfigFactory::createSubConfig($configFile, 'BrightNucleus\Example'); $example = new Example( $config ); // Outputs: // The value of the config key "test_key" is "test_value". echo $example->run(); }
示例 - 加载默认配置的类,除非配置已注入
ConfigTrait
提供了一些便利功能,允许您编写可以接收注入的 Config 但如果未注入则回退到默认配置文件的类。
以下是您如何编写这样的类的示例
<?php namespace BrightNucleus\Example; use BrightNucleus\Config\ConfigInterface; use BrightNucleus\Config\ConfigTrait; use BrightNucleus\Exception\RuntimeException; class Example { use ConfigTrait; /** * Instantiate an Example object. * * For this constructor, the `$config` argument is optional, and the class will * load a default configuration if none was injected. * * @param ConfigInterface|null $config Optional. Config to parametrize the object. * @throws RuntimeException If the Config could not be parsed correctly. */ public function __construct( ConfigInterface $config = null ) { // We either process the $config that was injected or fetch a default one. $this->processConfig( $config ?: $this->fetchDefaultConfig() ); } /** * Get the default configuration file name. * * This is used to override the default location. * * @return string Path & filename to the default configuration file. */ protected function getDefaultConfigFile() { return __DIR__ . '/../config/my_default_config.php'; } }
示例 - 将多个配置合并成一个
您可以将以逗号分隔的文件名列表提供给 ConfigFactory::merge()
方法。它们将按顺序加载并合并为一个连贯的配置。对于每个重复的配置键,较后文件的值将覆盖较前文件的值。
在我们的示例中,我们将定义一个新的配置文件 override_settings.php
,它覆盖了默认配置文件中已设置的键。
<?php namespace BrightNucleus\Example; /* * Example class main settings. */ $example = [ 'test_key' => 'override_value', ]; return [ 'BrightNucleus' => [ 'Example' => $example, ], ];
<?php namespace BrightNucleus\Example; use BrightNucleus\Config\ConfigFactory; function init() { $configFile = __DIR__ . '/config/example_settings.php'; $overrideFile = __DIR__ . '/config/override_settings.php'; $config = ConfigFactory::merge($configFile, $overrideFile); $example = new Example( $config ); // Outputs: // Both files will be loaded, but values from the second override the first. // The value of the config key "test_key" is "override_value". echo $example->run(); }
配置格式
Bright Nucleus Config 组件可以扩展以加载多种不同的文件格式。基本包包含一个非常基础的 PHPLoader
类。它可以加载仅 return
数组的简单 PHP 文件。
计划添加其他格式(如 JSON 或 YAML)的附加包,并将很快发布。
自定义加载器仅在需要时才会懒加载。
高级用法
配置架构
待办事项
配置验证
待办事项
自定义实现
待办事项
贡献
欢迎所有反馈/错误报告/拉取请求。
许可证
此代码根据 MIT 许可证发布。
有关完整的版权和许可信息,请查看随源代码一起分发的 LICENSE
文件。