krak/schema
Composer 安装命令:
composer require krak/schema
包简介
Declarative API for Defining and Processing Schemas
README 文档
README
The schema library provides the ability to define schemas with a declarative API and in turn using any of the processors to act on that schema.
We separate the concepts of the schema definition and the processors to allow us to build an AST describing a schema, and then allow different processors handle that structure for things like validation, generate a symfony config tree, building valid json schema, etc etc.
Installation
Install with composer at krak/schema
Usage
Defining a Schema
<?php use function Krak\Schema\{struct, listOf, dict, string, bool, int}; $schema = struct([ 'name' => string(), 'isAdmin' => bool(), 'age' => int(), 'tags' => listOf(string()), 'photos' => dict(struct([ 'url' => string(), 'width' => int(), 'height' => int(), ])) ]); /* would match a structure like: { "name": "Bob", "isAdmin": true, "age": 26, "tags": ["tall", "dark", "handsome"], "photos": { "small": { "url": "https://mydomain.com/images/bob/small", "width": 100, "height": 200 }, "large": { "url": "https://mydomain.com/images/bob/large", "width": 600, "height": 1200 }, } } */
Validation (Coming Soon)
Eventually we'll support the ability to take a schema and validate array structures against them.
Symfony Config Tree Processor
Declare and build symfony config tree builders declaratively with the configTree schema processor.
<?php use Symfony\Component\Config\Definition\{ConfigurationInterface, TreeBuilder}; use function Krak\Schema\ProcessSchema\SymfonyConfig\configTree; use function Krak\Schema\{struct, string}; final class Configuration implements ConfigurationInterface { public function getConfigTreeBuilder() { return configTree('aws', struct([ 'version' => string(), 'region' => string(), 'credentials' => struct([ 'key' => string(), 'secret' => string(), ]) ])); } }
Note: This currently supports Symfony 4 and 5 config.
Check out the feature test suite to see examples of all the supported api.
Comparison of Declarative vs Builder Syntax
Here's a seemingly simple config file that we'd want to validate the schema of:
my_package: string_key: 'abc' int_key: 1 struct_key: a: 1 b: 2 list_key: [1, 2, 3] list_of_struct_key: - a: 1 b: 2 struct_of_list: a: ['', ''] b: [0, 0]
Here is the builder syntax:
return (new TreeBuilder('my_package'))->getRootNode(); ->children() ->scalarNode('string_key')->end() ->integerNode('int_key')->end() ->arrayNode('struct_key') ->children() ->scalarNode('a')->end() ->integerNode('b')->end() ->end() ->end() ->arrayNode('list_key') ->integerPrototype()->end() ->end() ->arrayNode('list_of_struct_key') ->arrayPrototype() ->children() ->integerNode('a')->end() ->integerNode('b')->end() ->end() ->end() ->end() ->arrayNode('struct_of_list_key') ->children() ->arrayNode('a') ->scalarPrototype()->end() ->end() ->arrayNode('b') ->integerPrototype()->end() ->end() ->end() ->end() ->end() ->end();
Here is the declarative syntax for the same definition:
return configTree('my_package', struct([ 'string_key' => string(), 'int_key' => int(), 'struct_key' => struct([ 'a' => int(), 'b' => int(), ]), 'list_key' => listOf(int()), 'list_of_struct_key' => listOf(struct([ 'a' => int(), 'b' => int(), ])), 'struct_of_list_key' => struct([ 'a' => listOf(string()), 'b' => listOf(int()), ]) ]));
References
Original RFC Pull Request to Symfony: symfony/symfony#35127
Documentation
No formal API documentation is setup, but the src dir is under 200loc at this point. Also the tests directory gives a good overview of the various features as well.
Testing
Run composer test to run the test suite.
Roadmap
- Api Documentation
- Additional schema fns to support more string/numeric constraints (regex, min, max, etc)
- JSON Schema ProcessSchema
- Create the ability to export a schema definition to valid json schema json
- Validation ProcessSchema
- Create a function validation library for basic schemas
- Support custom validators and schema fns
- Symfony Validation ProcessSchema
- Export to symfony constraints to use with the SF validator. With one api, we could define schemas that export to SF config and SF validation!
krak/schema 适用场景与选型建议
krak/schema 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9.07k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2015 年 05 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「schema」 「config」 「validation」 「functional」 「declarative」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 krak/schema 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 krak/schema 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 krak/schema 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Extension for Opis JSON Schema
The bundle for easy using json-rpc api on your project
A Zend Framework module to quickly and easily set PHP settings.
EAV modeling package for Eloquent and Laravel.
serialize Symfony Forms into JSON schema
Build forms from schema
统计信息
- 总下载量: 9.07k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 23
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-05-21