tomkyle / configreader
germania-kg/configreader 的继任者:使用 Symfony YAML 合并默认和自定义配置文件。支持 PSR-6 CacheItemPools。
Requires
- php: ^8.1
- germania-kg/cachecallable: ^2.0
- psr/cache: ^2.0|^3.0
- psr/log: ^2.0|^3.0
- symfony/yaml: ^6.1|^7.0
Requires (Dev)
- monolog/monolog: ^2.1|^3.0
- php-coveralls/php-coveralls: ^2.0
- phpspec/prophecy-phpunit: ^2.0
- phpstan/phpstan: ^1.9
- phpunit/phpunit: ^10.0
Replaces
This package is auto-updated.
Last update: 2024-08-30 01:44:30 UTC
README
PHP 8.1+ 版本的 GermaniaKG/ConfigReader 继任者。由原作者创建。使用 Symfony YAML 读取并合并默认和自定义配置文件。
安装
此包版本为 v3.3,需要 PHP 8.1+ 对于 7.1 到 8.0 的旧 PHP 版本,请前往 GermaniaKG/ConfigReader 或 Packagist 上的 germania-kg/configreader 安装版本 3.2 及以下版本的包。
$ composer require tomkyle/configreader
路线图
- 从 v4 开始,命名空间将从
Germania\ConfigReader
更改为tomkyle\ConfigReader
接口
ConfigReaderInterface 需要一个 __invoke 方法,该方法可以用任意数量的文件名字符串调用
<?php namespace Germania\ConfigReader; interface ConfigReaderInterface { public function __invoke( ... $files ); }
用法
YamlConfigReader
YamlConfigReader 实现 ConfigReaderInterface。它内部使用 array_replace_recursive。如果给定的配置文件不存在,则不会发生任何事情。在任何情况下,返回值都是数组。
<?php use Germania\ConfigReader\YamlConfigReader; $reader = new YamlConfigReader( "/path/to/configs"); // Returns array $config = $reader("defaults.yaml", "optionals.yaml");
PSR-6 缓存支持
CacheConfigReader 也实现 ConfigReaderInterface,并结合了一个 ConfigReaderInterface 实例和 PSR-6 缓存功能。
<?php use Germania\ConfigReader\YamlConfigReader; use Germania\ConfigReader\CacheConfigReader; $reader = new YamlConfigReader( "/path/to/configs"); $cache_item_pool = ... // PSR-6 CacheItemPool $cache_lifetime = 3600; $logger = ... $cache_reader = new CacheConfigReader($reader, $cache_item_pool, $cache_lifetime); $cache_reader = new CacheConfigReader($reader, $cache_item_pool, $cache_lifetime, $logger); $config = $cache_reader("defaults.yaml", "optionals.yaml");
YAML 解析选项
setYamlFlags 方法允许设置整数标志,由 Symfony 的 YAML 组件使用。有关可能值的列表,请参阅官方文档: Symfony YAML 组件文档。
在配置文件中使用时的想法
- 使用 Yaml::PARSE_CONSTANT 来评估使用
.env
配置创建的常量 - 使用 Yaml::PARSE_DATETIME 来简化 string-to-DateTime 转换的工作
不要使用 Yaml::PARSE_OBJECT_FOR_MAP,因为它将破坏内部的 array_replace_recursive 调用。这是一个值得未来版本讨论的好话题。
<?php use use Symfony\Component\Yaml\Yaml; $reader = new YamlConfigReader( "/path/to/configs"); $reader->setYamlFlags( Yaml::PARSE_DATETIME | Yaml::PARSE_CONSTANT );
排除结果
给定一个类似这样的 YAML 映射
# ignoring.yaml # Exclude a single item: _ignore: foo # ... or even multiple items: _ignore: - foo - qux foo: bar qux: baz name: john
要排除某些元素,请使用 setIgnoreKey 来设置一个 YAML 映射项的名称 它包含要排除的键。 在我们的示例中,结果将不包含 foo
或 _ignore
。小心不要过度使用此功能!
$reader = new YamlConfigReader( "/path/to/configs"); $reader->setIgnoreKey( "_ignore" ); $config = $reader("ignoring.yaml"); # Will both be FALSE: isset( $config["_ignore"]) isset( $config["foo"]) # Reset again $reader->setIgnoreKey( null );
异常
当 YamlConfigReader 遇到 Symfony\Component\Yaml\Exception\ParseException 时,它将捕获它,并将其包装在 Germania\ConfigReader\ParseException 中。此类实现了 ConfigReaderExceptionInterface,您可以关注它
use Germania\ConfigReader\ConfigReaderExceptionInterface; try { $config = $reader("defaults.yaml", "optionals.yaml"); } catch (ConfigReaderExceptionInterface $e) { echo $e->getMessage(); }
开发
$ git clone https://github.com/tomkyle/ConfigReader.git
$ cd ConfigReader
$ composer install
单元测试
要么将 phpunit.xml.dist
复制到 phpunit.xml
并根据您的需要进行调整,要么保留原样。运行 PhpUnit 测试或 composer 脚本,如下所示
$ composer test # or $ vendor/bin/phpunit
测试结果日志位于 tests/log
目录中。