innmind / config
此包已被弃用,不再维护。未建议替代包。
配置架构验证器
1.1.0
2018-04-02 08:32 UTC
Requires
- php: ~7.2
- innmind/immutable: ^2.8
Requires (Dev)
- giorgiosironi/eris: ^0.9.0
- phpunit/phpunit: ~6.0
- symfony/yaml: ^4.0
This package is auto-updated.
Last update: 2022-02-01 13:12:49 UTC
README
master |
develop |
|---|---|
这是一个库,用于强制执行配置架构,并侧重于架构的可视化。
它是在不断努力使用 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,但在 set、stream 和 sequence 类型的情况下,通过返回 Innmind\Immutable\Set、Innmind\Immutable\Stream 和 Innmind\Immutable\Sequence 的实例来转换数据,返回的 $data 也是一个 Innmind\Immutable\Map 而不是简单的数组,以便于操作配置数据。
您可以使用的关键格式完整列表
prototype<int>指示键为整数prototype<int>+指示键为整数,并且至少有一个键prototype<string>指示键为字符串prototype<string>+指示键为字符串,并且至少有一个键prototype<scalar>指示键为标量prototype<scalar>+指示键为标量,并且至少有一个键- 任何其他字符串都将被视为键名
您可以使用的数据格式的完整列表
- 具有
is_{type}函数的任何值,包括int、float、bool、string、scalar、array、object和resource,可以通过在前面添加?来声明为可选 set<{type}>其中type是Innmind\Immutable\Set所接受的任何值set<{type}>+其中type是Innmind\Immutable\Set所接受的任何值,并且至少有一个值stream<{type}>其中type是Innmind\Immutable\Stream所接受的任何值stream<{type}>+其中type是Innmind\Immutable\Stream所接受的任何值,并且至少有一个值序列sequence+必须至少有一个值enum({typeA|typeB})可能的值由|分隔?enum({typeA|typeB})可能的值由|分隔,但值是可选的
扩展行为
显示的格式是默认格式,但您也可以轻松地自定义它。为此,您需要实现 Structure 或 Property,然后使用它。
$config = new Config( new Structures( ...Structures::defaults()->add(MyStructure::class) ), new Properties( ...Properties::defaults()->add(MyProperty::class) ) );