thewunder / conphigure
无框架配置库
3.0
2023-12-01 17:58 UTC
Requires
- php: >=8.1
Requires (Dev)
- php-coveralls/php-coveralls: ^2.0
- phpunit/phpunit: ^10.0
- squizlabs/php_codesniffer: ^2.3
- symfony/yaml: ^5.0 || ^6.0
- vlucas/phpdotenv: ^4.2 || ^5.0
Suggests
- ext-json: *
- ext-libxml: *
- ext-simplexml: *
- symfony/yaml: ^5.0 || ^6.0
- vlucas/phpdotenv: ^4.2 || ^5.0
README
Conphigure 是一个无框架的配置读取和检索库。如果你的应用程序已经超过了单个配置文件,这个库将是一个不错的选择。
它可以读取以下格式的配置文件
- php
- yaml
- json
- xml
- ini
- dotenv
Conphigure 还可以读取包含配置文件的整个目录。
安装
通过 Composer
$ composer require thewunder/conphigure
用法
如果你在 myfile.yml 中有配置
smtp: host: smtp.mycompany.com port: 25
在 PHP 应用程序中读取它,如下所示
$config = Conphigure::create(); //load configuration from a single file $config->read('/directory/myfile.yml'); //get a value $port = $config->get('smtp/port'); //add configuration from somewhere else (cache / database / etc) $arrayFromSomewhere = [ 'database' => [ 'host' => 'localhost' ] ]; $config->addConfiguration($arrayFromSomewhere); //you can also use it like an array $host = $config['database']['host']; $host = $config['database/host']; //throws an exception if a value is missing $value = $config->get('missing/key');
当读取配置目录时,Conphigure 会默认将每个文件中的配置组织到一个基于文件路径的公共根目录中。
例如,一个包含以下内容的 /directory/config/ 目录
- system.yml
- email.yml
- logging.yml
- subdirectory/something.yml
//read the directory $config->read('/directory/config/'); //get all configuration as an array $all = $config->all(); var_export($all); /* The result will be: [ 'system' => ['...'], //contents of system.yml 'email' => ['...'], //contents of email.yml 'logging' => ['...'], //contents of logging.yml 'subdirectory' => [ 'something' => ['...'], //contents of subdirectory/something.yml ] ]; */
这允许你保持组织良好,并使每个文件非常扁平。
变更日志
请参阅 CHANGELOG 了解最近的变化。
测试
$ composer test
贡献
请参阅 CONTRIBUTING 了解详细信息。
鸣谢
许可
MIT 许可证 (MIT)。请参阅 许可文件 了解更多信息。