赵天煦/configure

PHP项目的配置仓库。

v1.0.1 2014-03-25 14:29 UTC

This package is not auto-updated.

Last update: 2024-09-14 15:21:51 UTC


README

Build Status Total Downloads Latest Stable Version

PHP项目的配置仓库。

Configure是一个用于PHP项目的配置仓库。如果你的应用程序依赖于某种配置设置,你可以使用Configure来处理它们。它具有简单的API,支持不同类型的配置文件格式

安装

您可以通过Composer安装Configure

{
    "require": {
        "petersuhm/configure": "dev-master"
    }
}

基本用法

使用Configure就像实例化ConfigurationRepository类并开始向其中添加设置一样简单

$di->settings = new \Petersuhm\Configure\ConfigurationRepository();

$di->settings->set('template_path', '../templates');

现在您可以开始查询仓库了

$di->settings->get('template_path');

// You can also provide a default value to be returned
$di->settings->get('not_set', 'default_value');

Configure也支持数组

$di->settings->set([
    'lang' => 'da',
    'country' => 'dk'
]);

// Multi dimensional arrays will be flattened using dot notation
$di->settings->set([
    'localization' => [
        'lang' => 'da',
        'country' => 'dk'
    ]
]);
$di->settings->get('localization.lang');
$di->settings->get('localization.country');

使用配置文件

目前,Configure支持YAML和PHP文件。

# config.yml
localization:
    lang: da
    country: dk
app_name: Configure
# config.php
<?php

return array(

    'localization' => array(
        'lang' => 'da',
        'country' => 'dk'
    ),

    'app_name' => 'Configure'
);

为了加载文件,您需要创建相关文件加载类的一个实例,并将其提供给仓库上的load()方法

$loader = new \Petersuhm\Configure\Loader\YamlFileLoader('config.yml');
// or
$loader = new \Petersuhm\Configure\Loader\ArrayFileLoader('config.php');

$di->settings->load($loader);

$di->settings->get('localization.lang');
$di->settings->get('app_name');

测试

Configure完全由单元测试覆盖。所有代码都是使用行为驱动方法编写的,使用了phpspec

$ vendor/bin/phpspec run

贡献

有关详细信息,请参阅CONTRIBUTING

致谢

许可

MIT许可(MIT)。有关更多信息,请参阅许可文件