rkulik / config
轻量级配置文件加载器
1.2.0
2019-02-10 09:53 UTC
Requires
- php: ^7.1
Requires (Dev)
- phpunit/phpunit: ^7.0
This package is auto-updated.
Last update: 2024-09-10 22:16:27 UTC
README
轻量级配置文件加载器。目前支持PHP和JSON文件。
要求
此包需要PHP 7.1或更高版本。
安装
通过composer
$ composer require rkulik/config
用法
实例化基本配置
<?php // config.php return [ 'hello' => 'world', ];
<?php // index.php require 'vendor/autoload.php'; $configFactory = new \Rkulik\Config\ConfigFactory(); $config = $configFactory->make('config.php'); echo $config->get('hello'); // world
使用自定义解析器实例化配置
对于特殊需求,例如处理不受支持的配置文件类型,建议使用自定义解析器。
<?php // config.php return [ 'hello' => 'world', ];
<?php // CustomParser.php use Rkulik\Config\FileParser\FileParserInterface; class CustomParser implements FileParserInterface { /** * {@inheritdoc} */ public function parse(string $file): array { $data = require $file; array_walk($data, function (&$item) { $item = strrev($item); }); return $data; } }
<?php // index.php require 'vendor/autoload.php'; require 'CustomParser.php'; $configFactory = new \Rkulik\Config\ConfigFactory(); $customParser = new CustomParser(); $config = $configFactory->make('config.php', $customParser); echo $config->get('hello'); // dlrow
使用"点"表示法
使用"点"表示法对配置数据进行CRUD操作。
<?php // config.php return [ 'hello' => [ 'beautiful' => 'world', ], ];
<?php // index.php require 'vendor/autoload.php'; $configFactory = new \Rkulik\Config\ConfigFactory(); $config = $configFactory->make('config.php'); echo $config->get('hello.beautiful'); // world
测试
$ composer test
变更日志
请参阅变更日志了解最近的变化。
贡献
请参阅贡献指南以获取详细信息。
安全性
如果您发现任何安全相关的问题,请通过rene@kulik.io发送电子邮件,而不是使用问题跟踪器。
致谢
许可证
MIT许可证(MIT)。请参阅许可证文件以获取更多信息。