mattferris/configuration

1.0 2024-05-29 18:57 UTC

This package is auto-updated.

Last update: 2024-08-29 19:38:35 UTC


README

Build Status

一个配置管理库。

composer require mattferris/configuration

Configuration 帮助您加载和管理运行时配置。它足够灵活,允许您加载任何类型的配置资源,但又不会过于复杂,以至于在小型项目中难以使用。

use MattFerris\Configuration\Configuration;
use MattFerris\Configuration\Locators\FileLocator;
use MattFerris\Configuration\Loader\PhpLoader;

// setup a file locator with one or more search paths
$locator = new FileLocator(['foo/path', 'bar/path']);

// setup a loader for the type of files you want to  load
$loader = new PhpLoader();

$config = new Configuration($locator, $loader);

// load a config file, the locator will search for the file using the search paths
$config->load('config.php');

// get a configuration value
$config->get('password');

// get a nested configuration value
$config->get('db.host');

// dump the configuration
$values = $config->get();

/*
 * $values contains:
 *
 * [
 *     'password' => 'banana',
 *     'db' => [
 *         'host' => 'localhost'
 *     ]
 * ]
 */

在上面的示例中,config.php 可能看起来像;

<?php

// php config files must store all configuration in the $config variable
$config = [
    'password' => 'banana',
    'db' => [
        'host' => 'localhost'
    ]
];

您可以使用 Configuration::has() 来测试一个键是否存在。

if ($config->has('foo')) {
    // do stuff
}

如果您不确定一个键是否存在,使用 has() 是一个好的选择。对于不存在的键调用 get() 会抛出 KeyDoesNotExistException 异常。

从其他 ConfigurationInterface 实例导入数据也是可能的。

$config->import($moduleConfig);

最后,load()import() 都接受一个键作为第二个参数,用于加载或导入数据。

$config->load('database.php', 'db');
$config->import($moduleConfig, 'module');