chonla/cfg

配置加载/卸载器

0.1.3 2015-02-25 03:47 UTC

This package is auto-updated.

Last update: 2024-09-20 18:12:37 UTC


README

Config Loader/Dumper是Noodlehaus/Config包的扩展。

支持的配置加载器

PHP, INI, XML, JSON, 和 YAML

支持的配置卸载器

JSON 和 YAML

API

与Noodlehause/Config类似,Config对象可以静态创建或从Config实例化。

$conf = Config::load('config.json');
$conf = new Config('config.json');

使用get()来获取值

// Get value using key
$debug  = $config->get('debug');

// Get value using nested key
$secret = $config->get('security.secret');

// Get a value with a fallback
$ttl    = $config->get('app.timeout', 3000);

使用set()来设置值(当然!)

$conf = Config::load('config.json');
$conf->set('app.timeout', 1000);

使用save()将设置导出到文件

$conf = Config::load('config.yaml');
$conf->set('app.timeout', 1000);
$conf->save('new-config.yaml');

示例(来自Noodlehaus/Config)

这是一个我们将称之为config.json的JSON文件示例。

{
    "app": {
        "host": "localhost",
        "port": 80,
        "base": "/my/app"
    },
    "security": {
        "secret": "s3cr3t-c0d3"
    },
    "debug": false
}

这是同一配置文件以PHP格式表示

<?php
return array(
    'app' => array(
        'host' => 'localhost',
        'port' => 80,
        'base' => '/my/app'
    ),
    'security' => array(
        'secret' => 's3cr3t-c0d3'
    ),
    'debug' => false
);

或者在一个PHP文件中,该文件返回一个创建配置的函数

return function () {
    // Normal callable function, returns array
    return array(
    'app' => array(
        'host' => 'localhost',
        'port' => 80,
        'base' => '/my/app'
    ),
    'security' => array(
        'secret' => 's3cr3t-c0d3'
    ),
    'debug' => false
    );
};

或者以INI格式

debug = false

[app]
host = localhost
port = 80
base = /my/app

[security]
secret = s3cr3t-c0d3

或者以XML格式

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <app>
        <host>localhost</host>
        <port>80</port>
        <base>/my/app</base>
    </app>
    <security>
        <secret>s3cr3t-c0d3</secret>
    </security>
    <debug>false</debug>
</config>

或者以YAML格式

app:
    host: localhost
    port: 80
    base: /my/app
security:
    secret: s3cr3t-c0d3
debug: false

许可协议

MIT: http://chonla.mit-license.org/