thewunder/conphigure

无框架配置库

3.0 2023-12-01 17:58 UTC

This package is auto-updated.

Last update: 2024-09-22 16:56:56 UTC


README

Latest Version on Packagist Software License Total Downloads

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)。请参阅 许可文件 了解更多信息。