elbucho/config

多文件格式面向对象的配置系统

v1.0.4 2018-11-24 05:02 UTC

This package is auto-updated.

Last update: 2024-09-24 18:15:29 UTC


README

本项目为您的所有配置文件提供了一个面向对象的配置系统。这包括以下文件类型支持

  • INI 文件
  • PHP 文件(返回数组)
  • JSON 文件
  • XML 文件
  • YAML 文件

用法

Config 类通过键值对数组实例化。嵌套键以独立的 Config 类的形式实例化。

检索值

可以通过魔术 __get() 方法或 get() 方法访问键。get() 方法支持点分表示法(见以下示例)。

$config = new Elbucho\Config\Config(array('foo' => array('bar' => 1)));

$config->foo->bar == 1

$config->get('foo.bar') == 1

$config->get('foo')->bar == 1

$config->get('foo')->get('bar') == 1

当使用魔术 __get() 方法时,如果键不存在,Config 类将返回 false。当使用 get() 方法时,您可以指定如果键不存在时的返回值。

$config->foo->bar1 == false

$config->get('foo.bar1', 2) == 2

$config->foo->get('bar1', 'missing') == 'missing'

设置值

您可以使用魔术 __set() 方法或使用 set() 方法来设置值

$config->foo->bar = 2

$config->set('foo.bar', 2)

$config->foo->set('bar', 2)

如果您使用 set() 方法,并且指定了一个当前不存在的路径,则该路径将被创建

$config->get('foo.bar.foobar') == false

$config->set('foo.bar.foobar.asdf', 'fdsa')

取消设置值

您可以使用魔术 __unset() 方法或使用 remove() 来取消设置值

unset($config->foo->bar)

$config->remove('foo.bar')

$config->foo->remove('bar')

检查键是否存在

您可以通过魔术 __isset() 方法或使用 exists() 来确定键是否已设置

isset($config->foo->bar)

$config->exists('foo.bar')

$config->foo->exists('bar')

返回数组

您可以使用 toArray() 方法将 Config 对象返回为数组

$config->toArray() == ['foo' => ['bar' => 1]]

追加值

如果您想合并两个 Config 类,可以使用 append 函数

$config1 = new Config(array('foo' => array('bar' => 1)));
$config2 = new Config(array('foo' => array('baz' => 2)));
$config1->append($config2);

$config1->toArray() == ['foo' => ['bar' => 1, 'baz' => 2]]

文件加载器

您可以通过使用 Loader 类之一将配置文件加载到 Config 对象中

要加载文件并从存储的值中创建一个与 Config 兼容的数组,请执行以下操作

$loader = new YamlFileLoader();
$config = new Config($loader->load('/path/to/config.yaml'));

目录加载器

您还可以使用目录加载器加载给定目录中的所有文件

$loader = new Elbucho\Config\Loader\DirectoryLoader();
$config = new Config($loader->load('/path/to/config/directory'));

文件名(不包括扩展名)将作为 $config 对象中的键。例如,假设这个文件在您的配置目录中作为 "database.yml" 存在

host:   localhost
port:   3306
dbname: test
user:   test_user
pass:   test_password

当您运行上述代码导入此文件时,您的配置对象将如下所示

$config->toArray() == [
    'database'  => [
        'host'      => 'localhost',
        'port'      => 3306,
        'dbname'    => 'test',
        'user'      => 'test_user',
        'pass'      => 'test_password'
    ]
]

环境覆盖

在许多情况下,您可能希望有一组适用于所有环境的配置文件,并希望使用特定于环境的价值覆盖特定的键。

例如,假设我们的配置文件夹格式如下所示

/config
    /environment
        /live
            database.yml
        /test
            database.yml
    database.yml
    framework.yml

在这个例子中,我们希望加载 /config/database.yml/config/framework.yml 中所有环境共有的键,但由于我们处于生产环境,我们希望使用 /config/environment/live 中的值覆盖某些键。以下是实现方法

$environment = 'live';
$configPath = '/config';
$environmentPath = $configPath . '/environment/' . $environment;

$loader = new Elbucho\Config\Loader\DirectoryLoader();
$config = new Config($loader->load($configPath));
$config->remove('environment');

$config->append(
    new Config($loader->load($environmentPath))
);

扩展类

每个加载器都必须符合 Elbucho\Config\LoaderInterface 接口。如果您想编写自定义文件加载器,当遇到打开或解析文件问题时,应抛出 Elbucho\Config\InvalidFileException 异常。

还可以通过 DirectoryLoader 的 registerLoader() 方法注册新的加载器。

$loader = new Elbucho\Config\Loader\DirectoryLoader();

// Registers the CustomFileLoader() class to handle files with '.xyz' extensions
$loader->registerLoader(
    new CustomFileLoader(),
    'xyz'
);