germania-kg / configreader
使用 Symfony YAML 合并默认和自定义配置文件。支持 CacheItemPools。
Requires
- php: ^7.4|^8.0
- germania-kg/cachecallable: ^2.0
- psr/cache: ^1.0|^2.0|^3.0
- psr/log: ^1.0|^2.0|^3.0
- symfony/yaml: ^2.0|^3.0|^4.0|^5.0|^6.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: ^8.0|^9.0
Suggests
- tedivm/stash: Good caching solution
- dev-master
- 3.2.0
- 3.1.11
- 3.1.10
- 3.1.9
- 3.1.8
- 3.1.7
- 3.1.6
- 3.1.5
- 3.1.4
- 3.1.3
- 3.1.2
- 3.1.1
- 3.1.0
- 3.0.5
- 3.0.4
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 2.2.0
- 2.1.1
- 2.1.0
- 2.0.1
- 2.0.0
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- dev-dependabot/composer/guzzlehttp/psr7-2.5.0
- dev-develop
This package is auto-updated.
Last update: 2024-09-19 23:43:04 UTC
README
使用 Symfony YAML 合并默认和自定义配置文件
安装
- v2 需要 PHP 7.1
- v3.0 需要 PHP 7.2+
- v3.1 需要 PHP 7.3+
- v3.2 需要 PHP 7.4+
$ composer require germania-kg/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/GermaniaKG/ConfigReader.git
$ cd ConfigReader
$ composer install
单元测试
要么将 phpunit.xml.dist
复制到 phpunit.xml
并根据您的需要进行调整,要么保持不变。运行 PhpUnit 测试或 composer 脚本,如下所示
$ composer test # or $ vendor/bin/phpunit
测试结果日志位于 tests/log
目录中。