simoneddy / config
基本的只读配置类,具有文件/目录加载器和点符号表示
Requires
- php: >=8.0
Requires (Dev)
- symfony/var-dumper: ^5.3
- symfony/yaml: ^5.3
Suggests
- symfony/yaml: Required for YAML support
README
具有文件加载器的基本配置类。
2.0.0 版本中的变更
- ConfigLoader 类已被重命名为 PathLoader
- 使用 PathLoader->load 方法不再返回 Config 对象。相反,现在它返回从文件系统中加载的值数组。
- 从 PathLoader 中移除了 Config 对象的构造。
- 添加了一个 ConfigFactory 类,用于处理使用静态工厂方法时的构造。
- 添加了一个 MutableConfig 类,它扩展了 Config 并提供了设置和取消设置值的方法。
1.1 版本中的变更
- 现在可以加载 CSV 文件
安装
此库可以使用 composer 进行安装
composer require simoneddy/config
依赖关系
- PHP >=8.0
- symfony/yaml (可选,用于 yaml 支持)
使用
可以通过将值传递给类构造函数或使用各种工厂方法之一来创建 Config 对象
require 'vendor/autoload.php'; // Using the Config constructor. This method requires that all config values be supplied as the // constructors $values argument $config = new \Eddy\Config\Config($values = []); // Using the MutableConfig constructor. This method allows config values be supplied as the // constructors $values argument, but can be modified after construction. $config = new \Eddy\Config\MutableConfig($values = []); // Using the Config objects static factory method $config = \Eddy\Config\Config::fromPath(__DIR__ . '/config'); // Using the MutableConfig objects static factory method $config = \Eddy\Config\MutableConfig::fromPath(__DIR__ . '/config'); // Using the ConfigFactory directly $config = \Eddy\Config\ConfigFactory::fromPath(__DIR__ . '/config', $isMutable = false); // Using a new ConfigFactory to create an immutable Config object $config = (new \Eddy\Config\ConfigFactory())->createImmutable(__DIR__ . '/config'); // Using a new ConfigFactory to create a mutable MutableConfig object $config = (new \Eddy\Config\ConfigFactory())->createMutable(__DIR__ . '/config');
内部,此工厂方法使用 PathLoader 类扫描提供的路径并将所有 受支持文件类型 加载到数组中。然后将该数组提供给 ConfigFactory,它构建必要的配置类。
将忽略不受支持的文件。
如果路径是目录,PathLoader 将尝试从目录中包含的所有受支持文件加载值。这也适用于任何子目录及其内容。
文件作为键值对加载,使用基本文件名(不带扩展名)作为键,而文件内容作为值。
例如
// config directory contains the file 'test.php' which returns an array: // ['isTest' => true] $config = \Eddy\Config\Config::fromPath(__DIR__ . '/config'); // values can be retrieved with dot notation, where the filename is the parent // key: var_dump($config['test.isTrue']);
请注意 YAML 文件将被忽略,除非也安装了 symfony/yaml 包。
您还可以使用 Config 类构造函数,并直接提供配置值作为唯一参数
$config = new \Eddy\Config\Config([ 'someKey' => 'some value', 'someMoreKeys' => [ 'anotherKey' => 'another value' ] ]);
一旦创建配置对象,就可以像数组一样访问它,使用点符号指定嵌套键
echo $config['someKey']; // 'some value' // Using dot notation to access nested keys: var_dump(isset($config['someMoreKeys.anotherKey'])); // true
内部,数组访问方法使用 get($key) 和 has($key) 方法。这些方法也可以直接使用
echo $config->get('someKey'); // 'some value' var_dump($config->has('someMoreKeys.anotherKey')); // true
Eddy\Config\Config 对象是只读的,对象实例化后无法修改值。要使用可变性,请使用 Eddy\Confi\MutableConfig 类,它添加了设置和删除方法,并正确实现了 \ArrayAccess::offsetUnset 和 \ArrayAccess::offsetSet
// MutableConfig can be modified after construction $config = new \Eddy\Config\MutableConfig([ 'someKey' => 'some value', 'someMoreKeys' => [ 'anotherKey' => 'another value' ] ]); // Now offsetSet works $config['anotherKey'] = 'another value'; // Which is the same as using the set method: $config->set('andAnother', 'yet another value'); var_drump(isset($config['anotherKey'])); // true // OffsetUnset works too! unset($config['someKey']); // Which is using the remove method: $config->remove('andAnother'); var_dump($config->has('someKey')); // false var_dump($config->has('andAnother')); // false
当然,所有设置和取消设置都可以使用点符号来处理嵌套值
$config->set('parent.nested.nestedTwice.nestedThrice', 'nested value'); // returns an array: ['nested' => ['nestedTwice' => ['nestedThrice' => 'nested value']]] var_dump($config->get('parent')); unset($config['parent.nested.nestedTwice']); // returns an array: ['nested' => []] var_dump($config->get('parent'));
设计上,set 方法(以及随后的 offsetSet)将在必要时合并嵌套键 => 值对。如果您想完全覆盖父键,可以使用 overwrite 方法
$config->set('parent.nested.nestedTwice', 'nested value'); // returns an array: ['nested' => ['nestedTwice' => 'nested value']] var_dump($config->get('parent')); $config->overwrite('parent.newThing', 'new value'); // returns ['newThing' => 'new value'] // 'nestedTwice' is no longer set as 'parent' has been overwritten var_dump($config['parent']);
配置对象包含一个 toArray 方法,它返回所有配置值作为数组。返回的数组保持配置键和结构。
序列化
Config 类实现了 PHP 的 JsonSerializable 接口和符合 PHP 8 标准的序列化魔术方法。没有弃用警告!
支持的文件类型
ConfigLoader 支持以下文件类型
- PHP
- JSON
- CSV(解析为以文件名作为键的数组)
- YAML(需要 symfony/yaml 作为依赖项)
给定的路径也可以是一个包含支持文件和子目录的目录。目录结构将被用来确定嵌套级别。