innmind/config

此包已被弃用,不再维护。未建议替代包。

配置架构验证器

1.1.0 2018-04-02 08:32 UTC

This package is auto-updated.

Last update: 2022-02-01 13:12:49 UTC


README

master develop
Scrutinizer Code Quality Scrutinizer Code Quality
Code Coverage Code Coverage
Build Status Build Status

这是一个库,用于强制执行配置架构,并侧重于架构的可视化。

它是在不断努力使用 symfony/config 构建原型定义之后诞生的,以及原型定义不能与属性定义混合的事实。

注意:原型是一个允许在数组中重复子架构定义的描述,例如 prototype<int>: bool 允许您有如下的配置:[1 => true, 2 => false /* etc... */]

安装

composer require innmind/config

用法

为了更好地可视化配置架构,您应该通过 yaml 或 json(或任何其他简约格式)定义它。以下是一个展示此库可能性的示例。

# schema.yml
prototype<string>:
    type: string
    alias: '?string'
    repository: '?string'
    factory: '?string'
    labels: set<string>
    identity:
        property: string
        type: string
    properties:
        prototype<string>:
            type: string
            prototype<scalar>: mixed
use Innmind\Config\Config;
use Symfony\Component\Yaml\Yaml;

$config = (new Config)->build(Yaml::parseFile('schema.yml'));
$data = $config->process($data);

当您调用 process 时,它将验证 $data,但在 setstreamsequence 类型的情况下,通过返回 Innmind\Immutable\SetInnmind\Immutable\StreamInnmind\Immutable\Sequence 的实例来转换数据,返回的 $data 也是一个 Innmind\Immutable\Map 而不是简单的数组,以便于操作配置数据。

您可以使用的关键格式完整列表

  • prototype<int> 指示键为整数
  • prototype<int>+ 指示键为整数,并且至少有一个键
  • prototype<string> 指示键为字符串
  • prototype<string>+ 指示键为字符串,并且至少有一个键
  • prototype<scalar> 指示键为标量
  • prototype<scalar>+ 指示键为标量,并且至少有一个键
  • 任何其他字符串都将被视为键名

您可以使用的数据格式的完整列表

  • 具有 is_{type} 函数的任何值,包括 intfloatboolstringscalararrayobjectresource,可以通过在前面添加 ? 来声明为可选
  • set<{type}> 其中 typeInnmind\Immutable\Set 所接受的任何值
  • set<{type}>+ 其中 typeInnmind\Immutable\Set 所接受的任何值,并且至少有一个值
  • stream<{type}> 其中 typeInnmind\Immutable\Stream 所接受的任何值
  • stream<{type}>+ 其中 typeInnmind\Immutable\Stream 所接受的任何值,并且至少有一个值
  • 序列
  • sequence+ 必须至少有一个值
  • enum({typeA|typeB}) 可能的值由 | 分隔
  • ?enum({typeA|typeB}) 可能的值由 | 分隔,但值是可选的

扩展行为

显示的格式是默认格式,但您也可以轻松地自定义它。为此,您需要实现 StructureProperty,然后使用它。

$config = new Config(
    new Structures(
        ...Structures::defaults()->add(MyStructure::class)
    ),
    new Properties(
        ...Properties::defaults()->add(MyProperty::class)
    )
);