maer/config

一个小型配置库

2.0.1 2018-05-09 15:16 UTC

This package is auto-updated.

Last update: 2024-09-09 02:05:09 UTC


README

Build Status

使用点表示法加载配置文件并设置或获取嵌套数组中的值。

安装

克隆此仓库或使用composer下载最新版本

$ composer require maer/config

用法

配置文件结构

默认情况下,配置文件可以是返回数组的PHP文件

<?php

return [
    'name'    => 'Chuck Norris',
    'skill'   => 'Everything',
    'movies'  => [
        'genres' => [
            'action'
        ],
        'titles' => [
            'Missing in Action',
            'The Delta Force'
        ]
    ],
];

或Json文件 (必须具有.json扩展名)

{
    "name": "Chuck Norris",
    "skill": "Everything",
    "movies": {
        "genres": [
            "action"
        ],
        "titles": [
            "Missing in Action",
            "The Delta Force"
        ]
    }
}

或ini文件 (必须具有.ini扩展名)

name = "Chuck Norris"
skill = "Everything"

实例

由于所有加载的配置文件都存储在用于加载文件的Config实例中,因此您需要在整个应用程序中始终使用相同的Config实例。

获取Config类实例有两种方式

  1. 创建一个新实例
  2. 使用工厂

创建一个新实例

如果您有自己的工厂或正在使用依赖注入,您可能希望自己创建实例。以下是方法

<?php
# Use composers autoloader
require __DIR__ . '/vendor/autoload.php';

$config = new Maer\Config\Config;

# Load a config file (you can also send in an array with multiple config files
# or send the array to the constructor upon instantiation.
$config->load('path-to-your-config-file');

$name = $config->get('name', 'this optional string will be returned if the key does not exist');
# Returnes: Chuck Norris

$config->set('name', 'Jackie Chan');
$name = $config->get('name');
# Returnes: Jackie Chan

# If you haven't loaded any file (or if you want to merge with another array),
# you can pass an array as the first parameter. This uses the array_replace_recursive() strategy.
$config->set(['name' => 'Chuck Norris', 'skill' => 'Something new']);

# Use dot notation for multidimensional arrays
$genres = $config->get('movies.genres'));
# Returnes: ['action']

# Push a new item to an existing array
$config->push('movies.genres', 'Some new genre'));
# If the target isn't an array, an UnexpectedValueException will be thrown.

# Check if a key is exists
if ($config->has('name')) {
    // Do stuff
}

# Check if a config file is loaded
if ($config->isLoaded('path-to-config-file')) {
    // Do stuff
}

使用工厂

如果您只想快速获取实例,而懒得创建自己的工厂或使用依赖注入,您可以使用包含在包中的工厂。它总是返回相同的实例

<?php
# Use composers autoloader
require __DIR__ . '/vendor/autoload.php';

$config = Maer\Config\Factory::getInstance();

# ...after that, it's all the same as before

读取器

如上所述,此库默认支持php-、json-和ini文件。如果您想读取其他格式的文件,您可以添加自己的读取器。

创建读取器

创建读取器时,它必须实现接口 Maer\Config\Readers\ReaderInterface

示例

class JsonReader implements Maer\Config\Readers\ReaderInterface
{
    public function read($file)
    {
        $content = json_decode(file_get_contents($file), true, 512);
        return is_array($content) ? $content : [];
    }
}

注册读取器

在您可以使用读取器之前,您需要注册它,并告诉库它应该用于哪种文件扩展名。

假设我们创建了一个用于yaml的读取器,我们希望将其与yml-文件扩展名相关联

# Either add the reader to an existing config instance
$config->setReader('yml', new MyYamlReader);

# or you can add readers when you instantiate the config class as a second argument
$options = [
    'readers' => [
        'yml' => new MyYamlReader,
    ],
];

$config = new Config(['/path-to-some-config'], $options);

如果您想为多个文件扩展名使用相同的读取器(例如,我们允许ymlyaml作为文件扩展名),就像上面那样注册,并传递相同的读取器。

当然,您可以覆盖默认的php、ini和json读取器,为这些文件扩展名注册自己的读取器。

重要:如果您想使用自己的读取器,您必须在尝试加载配置文件之前或同时通过构造函数注册它。

注意

如果不同配置文件之间存在名称/键的冲突,则将使用最后加载的文件中的值。

如果您有任何问题、建议或问题,请告诉我!

祝您编码愉快!