拼图/配置

5.0.0 2023-03-28 07:07 UTC

This package is auto-updated.

Last update: 2024-08-28 10:31:11 UTC


README

PHP 7.x 用户请使用 puzzle/configuration 4.x PHP 5.6 用户请使用 puzzle/configuration 3.x

将配置实现隐藏在公共接口后面。

一些优势

  • 应用程序不依赖于配置实现的细节
  • 应用程序不需要管理文件系统问题(对于基于文件系统的实现)
  • 应用程序可以轻松测试,即使是配置边缘情况(缺少或错误的配置值)
  • 在依赖注入容器中将配置定义为服务

质量保证

SensioLabsInsight

安装

使用 composer

{
    "require": {
            "puzzle/configuration" : "~4.0"
    }
}

文档

作为服务的配置

<?php

class Example
{
    public function __construct(Puzzle\Configuration $config)
    {
        $threshold = $config->read('app/detection/threshold');
    }
}

读取配置值的方式取决于所选实现。

到目前为止,提供了 2 种实现

  • 内存(用于单元测试目的)
  • Yaml(基于 Symfony/Yaml)。

对于 YAML 实现,'app/detection/threshold' 在 app.yml 文件中表示 detection[thresold]。当你实例化 YamlConfiguration 对象时,需要提供可以找到 yaml 文件的位置

<?php

$fileSystem = new Gaufrette\Filesystem(
    new Local('path/to/yaml/files/root/dir')
);
$config = new Puzzle\Configuration\Yaml($fileSystem);

$example = new Example($config);
# app.yml 

detection:
  threshold: 3

单元测试

<?php

$config = new Puzzle\Configuration\Memory(array(
    'app/detection/threshold' => 2
);

$example = new ExampleTest($config);

默认值

<?php

$configuration->read('a/b/c', 'default value if a/b/c does not exist');

但是,如果需要 a/b/c

<?php

// will throw an exception if a/b/c does not exist
$configuration->readRequired('a/b/c');

回退策略

<?php

// Since 1.5.0
// returns value associated to first existing key
// will throw an exception if none exist
$configuration->readFirstExisting('a/b/c', 'd/e/f', 'x/y/z');

覆盖配置

如果你需要某些配置(部分或全部)覆盖另一个配置

<?php

// Since 1.6.0
$defaultFileSystem = new Gaufrette\Filesystem(
    new Local('path/to/yaml/files/root/dir')
);
$defaultConfig = new Puzzle\Configuration\Yaml($defaultFileSystem);

$fileSystem = new Gaufrette\Filesystem(
    new Local('path/to/another/config/files')
);
$localConfig = new Puzzle\Configuration\Yaml($fileSystem);

$config = new Puzzle\Configuration\Stacked();
$config->overrideBy($defaultConfig)
       ->overrideBy($localConfig);

// values will be read in localConfig first. They will be read in default config only if they don't exist in local one.

另一个例子

<?php

$fileSystem = new Gaufrette\Filesystem(
    new Local('path/to/yaml/files/root/dir')
);
$defaultConfig = new Puzzle\Configuration\Yaml($fileSystem);

$overrideConfig = new Puzzle\Configuration\Memory(array(
    'app/detection/threshold' => 2
);

$config = new Puzzle\Configuration\Stacked();
$config->overrideBy($defaultConfig)
       ->overrideBy($overrideConfig);

你可以在栈中添加尽可能多的配置实例。最后添加的是最优先的。

如果你想添加最不优先的,请使用 addBase() 方法

<?php

// Since 2.0.0
$config = new Puzzle\Configuration\Stacked();
$config->overrideBy($overrideConfig)
       ->addBase($defaultConfig);

前缀配置

你可以使用自动前缀装饰器 PrefixedConfiguration。对于“命名空间类似”的配置(例如日志记录器或多个数据库)可能很有用。

# logger.yml 

app:
  filename: app.log
  verbosity: INFO
users:
  filename: users.log
  verbosity: WARNING
<?php

// Since 1.7.0
$fileSystem = new Gaufrette\Filesystem(
    new Local('path/to/yaml/files/root/dir')
);
$config = new Puzzle\Configuration\Yaml($fileSystem);
$config = new Puzzle\PrefixedConfiguration($config, "logger/$loggerName");

$filename = $config->readRequired('filename');
$verbosity = $config->readRequired('verbosity');

变更日志

4.x --> 5.x

  • 停止对 PHP 7 的支持。最低版本为 8.0

3.x --> 4.x

  • 停止对 PHP 5.6 和 7.0 的支持。最低版本为 7.1.0

2.x --> 3.x

  • 停止对 PHP 5.5 的支持。最低版本为 5.6.0

1.x -> 2.x

  • 停止对 PHP 5.4 的支持。最低版本为 5.5.0