requtize/config

该软件包已被废弃且不再维护。未建议替代软件包。

简单的配置库,可以处理PHP数组、YAML和INI文件作为配置,并将所有配置导出到一个PHP文件作为缓存。

1.2.0 2018-03-01 18:11 UTC

This package is auto-updated.

Last update: 2024-03-12 12:31:35 UTC


README

此库提供配置文件系统,允许您将多个文件和多种格式合并成一个对象,并通过简单的数组点表示法访问它。

  1. 将多个文件合并成一个对象
  2. 以相同的方式导入多种格式的文件: PHPINIYAML
  3. 从其他文件导入文件
  4. 简单的点表示法用于访问数组
  5. 缓存系统,用于保存解析的文件到一个文件中

安装

通过 composer.json

{
    "require": {
        "requtize/config": "^1.2.0"
    }
}

通过 Composer CLI

composer require requtize/config:^1.2.0

点表示法

点表示法是为了以简单的方式访问多维数组(PHP)。如果我们想访问某个值,我们需要通过点分隔每个索引。以下代码应该可以解释这一点

$array = [
    'one' => [
        'two' => [
            'three' => 'value'
        ]
    ]
];

// PHP access
$array['one']['two']['three'];

// Dot notation access
$config->get('one.two.three');

代码

// Without cache system
$config = new Config();
// With cache system
$config = new Config('config-filepath.php');

// Import files - multiple formats in the same way
$config->import('filename.php');
$config->import('filename.ini');
$config->import('filename.yaml');
// Or import as array
$config->import([ 'filename.php', 'filename.ini', 'filename.yaml' ]);

// Get value - each index in multidimensional array separate by dot
// Default value will be returned if given key(s) will not be existed
$value = $config->get('some.deep.index.in-config.file', 'default value');

// Set value in current request live (and in cache).
$config->set('key', 'some value to save');

// Key exists?
$config->has('key') ? 'yes' : 'no';

// Get all data in configs (in all files)
$config->all();

在配置文件中导入其他文件

如果您想导入其他文件,您不需要编写任何PHP代码来做这件事。只需在任何文件中使用 imports.files 索引,并在您想导入的每个索引中输入文件名。导入可以以相同的方式用于导入其他格式的文件。请记住,文件是相对于放置导入规则的文件进行搜索的!

return [
    // Other data...
    'imports' => [
        'filepath.php',
        '../../global-config/main.ini',
        './some-yaml.file.yaml'
    ]
    // Other data...
];
# Other data...
imports:
    "filepath.php"
    "../../global-config/main.ini"
    "./some-yaml.file.yaml"
# Other data...
; Other data...
[imports]
0 = "filepath.php"
1 = "../../global-config/main.ini"
2 = "./some-yaml.file.yaml"
; Other data...

@todo

  • 导入文件的名称前缀,以允许从不同的位置导入文件: '%root%/config.file.php''%server-config%/some.file.ini'
  • XML文件支持
  • 您想要更多吗...?

许可证

此代码根据MIT许可证授权。