ride / lib-config
Ride框架的配置库
1.1
2023-08-23 13:12 UTC
Requires
- ride/lib-system: ^1.0.0
README
PHP Ride框架的配置库。
本库包含内容
Config
Config接口定义了一个配置数据容器,用于获取和设置参数。提供了一个通用实现。
ConfigIO
要读取和写入配置数据源,使用ConfigIO接口。默认情况下,你可以使用ParserConfigIO,它将你的选择解析器包裹在ride-lib-system的文件浏览器中。你可以在CachedConfigIO周围包装任何IO以提高性能。
Parser
Parser接口用于读取和写入不同的文件格式。提供了ini和json的实现。
代码示例
查看此代码示例以了解本库的可用性
<?php use ride\library\config\io\CachedConfigIO; use ride\library\config\io\ParserConfigIO; use ride\library\config\parser\JsonParser; use ride\library\config\ConfigHelper; use ride\library\config\GenericConfig; use ride\library\system\file\browser\FileBrowser; function foo(FileBrowser $fileBrowser) { // Create the config helper, our IO and the config itself will use this. $configHelper = new ConfigHelper(); // Let's use the JSON format... $parser = new JsonParser(); // Now we create a config input/output implementation for all config/parameters.json files found in the file browser $configIO = new ParserConfigIO($fileBrowser, $configHelper, $parser, 'parameters.json', 'config'); // optionally, you can wrap it around a cached version $cacheFile = $fileBrowser->getFileSystem()->getFile(__DIR__ . '/config.cache'); $configIO = new CachedConfigIO($configIO, $cacheFile); // As final step, we create the config instance which is the main access point to the configuration parameters. $config = new GenericConfig($configIO, $configHelper); // You can get a value, optionally with a default. $name = $config->get('system.name'); // null, not set $name = $config->get('system.name', 'Ride'); // 'Ride' as default value // You can set a value, which is automatically written to the IO. $config->set('system.name', 'My System'); $config->set('system.secret', 'ABCDEF'); // You can get parameters which are not leafs of the configuration tree $parameters = $config->get('system'); // [ // 'name' => 'My System', // 'secret' => 'ABCDEF' // ] // you can use the config helper to flatten a structure $config->set('system.directory.cache', 'cache'); $config->set('system.directory.template', 'templates'); $parameters = $config->get('system'); // [ // 'name' => 'My System', // 'secret' => 'ABCDEF' // 'directory' => [ // 'cache' => 'cache', // 'template' => 'templates', // ] // ] $parameters = $configHelper->flattenConfig($parameters); // [ // 'name' => 'My System', // 'secret' => 'ABCDEF' // 'directory.cache' => 'cache', // 'directory.template' => 'templates', // ] }
限制
你无法为具有子键的键设置值。这意味着,如果你有一个名为system.directory.cache的键的值,你不能为system.directory设置值。
安装
你可以使用Composer来安装此库。
composer require ride/lib-config