mr-luke/configuration
Composer 安装命令:
composer require mr-luke/configuration
包简介
Dot notation array host configuration package.
关键字:
README 文档
README
This package provides array host (wrapper) package that supports dot notation access and schema validation.
Getting Started
Good software development follows many patterns and architectures. We design things that depends on many other parts. Some of them are well structured Objects but many times we need to have some configurations. Often we use array as our config host but it can produce mass of unexpected side-effects becasue of one reason - array is not an Object so it can't follow any schema. But what if it can...
During my work I developed a simple wrapper tool that helped me with schema sensitive arrays and I decided to make it a package. I hope you enjoy it!
Installation
To install through composer, simply put the following in your composer.json file and run composer update
{
"require": {
"mr-luke/configuration": "~1.0"
}
}
Or use the following command
composer require "mr-luke/configuration"
Usage
Let's move to a Schema class. It's a validation tool with one interface method:
public function check(array $insert, bool $throw = true): bool
$insert- This is your array that is a subject of validation$throw- This option change behavior of validation
By default check method throws an InvalidArgumentException when $insert doesn't follow schema.
Step One
Create your Schema array:
$instruction = [ 'first_key' => 'required|string', 'second_key' => 'nullable|integer', 'third_key' => 'required|float', ];
Available rules:
required- given key must not be emptynullable- given key can be nullboolean- given key must be boolean typefloat- given key must be float typeinteger- given key must be integer typestring- given key must be string and can't be other types
Step Two
Create new instance of Mrluke\Configuration\Schema:
$schema = new Schema(array $instruction);
Note! From v1.2.0 you can create Schema by static method createFromFile(string $path, bool $json = false).
Step Three
Create new instance of Mrluke\Configuration\Host with Schema as a dependency and your $configArray is automatically validated.
$host = new Host($configArray, $schema);
If your $configArray doesn't follow given Schema, you will get InvalidArgumentException. You can also use Host without any Schema due to it's optional parameter of Mrluke\Configuration\Host.
Your configuration is Wrapped!
Now you have an access to Host methods:
/** * Return given key from array. * * @param string $key * @param mixed $default * @return mixed */ public function get(string $key, $default = null)
Your key can follow dot notation to access nested keys:
$host->get('mysql.database', 'my_db');
By default if key is not present, Host returns null. You can also use magic getter to acces config:
$host->mysql;
You can check if given hey is present:
/** * Determine if given key is present. * * @param string $key * @return bool */ public function has(string $key): bool
Plans
Feel free to contribute because I am aware that there are some things to improve. For now:
- Nested Schema support
- New validation rules support
- New Schema's file format
mr-luke/configuration 适用场景与选型建议
mr-luke/configuration 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 475.85k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2018 年 11 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「configuration」 「array」 「host」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mr-luke/configuration 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mr-luke/configuration 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mr-luke/configuration 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A set of useful PHP classes.
Configuration component
Trait providing methods to set class properties with an array.
Traits to build collections of specific objects
This adds functions about array. If you feel like there few php built-in functions about array, this will be useful.
Convert array or camel case to underscore, or underscore to others.
统计信息
- 总下载量: 475.85k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 18
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-11-25