minond/configurare

配置管理

v2.0.0 2014-02-02 04:57 UTC

This package is not auto-updated.

Last update: 2024-09-23 14:44:14 UTC


README

Build Status Coverage Status Latest Stable Version Dependencies Status Scrutinizer Quality Score SensioLabsInsight

示例用法

初始化配置

use Efficio\Configurare\Configuration;

$conf = new Configuration;
$conf->setDirectory('./config/');
# config/app.yml
name: 'My Application'
usa:
  utah:
    provo:
      author: 'Marcos Minond'

配置文件格式和解析器

// available parsers
use Efficio\Configurare\Parser\Json;

$conf->setExtension('.json'); // default is '.yml'
$conf->setParser(new Json); // default is Efficio\Configurare\Parser\Yaml

使用自定义格式

use Efficio\Configurare\Parser\Parser;

class CustomParser implements Parser
{
    /**
     * takes a raw string, parses it, and returns the array representing the
     * data
     * @param string $raw
     * @return array
     */
    public function decode($raw)
    {
        return unserialize($raw);
    }

    /**
     * takes an array or an object and converts it into a string that can be
     * saved in a file
     * @param mixed $obj
     * @return string
     */
    public function encode($obj)
    {
        return serialize($obj);
    }
}

$conf->setExtension('.custom');
$conf->setParser(new CustomParser);

获取值

// looks for in ./config/app.yml
// this gets [ 'name': ]
echo $conf->get('app:name'); // => My Application

// this gets [ 'usa': 'utah': 'provo': 'author': ]
echo $conf->get('app:usa:utah:provo:author'); // => Marcos Minond

// you can also get nested configuration files
// looks in config/users/2014/jan.yml
echo $conf->get('users/2014/jan:activities:music');

设置值

// if a key(s) already exists, just set it
$conf->set('app:name', 'My Other Application');

// if they do not then the write must be forced by passing a third parameter
// set to true
$conf->set('app:does:not:exists:yet', 'yes', true);
# config/app.yml
name: 'My Other Application'
usa:
  utah:
    provo:
      author: 'Marcos Minond'
does:
  not:
    exists:
      yet: 'yes'

环境

添加环境允许使用额外的配置文件,这些文件可能或可能不被版本控制跟踪。例如,您可以提交一个“default” config/app.yml配置文件,该文件对环境(即数据库连接信息)作出假设,并使用config/app.prod.yml覆盖它。此“prod”文件包含敏感数据,仅存储在运行应用程序的生产服务器上。这允许您使用相同的配置检索代码,为您的环境获取正确的配置,并且不需要将其提交到源代码控制。

// I can have one enviroment or multiple
$conf->setEnvironments([ 'dev', 'test' ]);

// the following files will be parsed and merged before the configuration
// value is sent back
// - config/database.yaml
// - config/database.dev.yaml
// - config/database.test.yaml
$conf->get('database:connection:username');

缓存

Configurare与Cache包兼容

use Efficio\Cache\NullCache;
$conf->setCache(new NullCache);