khartir/typed-config
Composer 安装命令:
composer require khartir/typed-config
包简介
A tool to create valid config objects from typed classes and data-arrays
README 文档
README
A package to create a typed configuration in PHP
Installation
composer require khartir/typed-config
Usage
Given some classes like this:
class Config {
private $foo;
private $barConfig;
public function __construct(string $foo, BarConfig $barConfig) {
$this->foo = $foo;
$this->barConfig = $barConfig;
}
}
class BarConfig {
private $value;
public function __construct(string $value) {
$this->value = $value;
}
}
You can create populated objects like this:
$builder = new \Khartir\TypedConfig\Builder();
$data = [
['barConfig' => ['value' => 'dummy']],
['foo' => 'bar'],
];
$config = $builder->build(Config::class, $data);
snake_case keys to camelCase config
If your array-keys are snake_case and your properties are camelCase you can add a converter:
$builder = new \Khartir\TypedConfig\Builder(new \Khartir\TypedConfig\Extractor\SnakeCaseExtractor());
Extending
Writing a custom resolver
Resolvers determine the value of a parameter from the list of values given to the builder.
You can write custom resolvers to implement special logic.
The example contains a resolver to transform strings to \DateTime-Objects.
class DateTimeResolver implements ResolverInterface
{
public const DATE_FORMAT = 'Y-m-d H:i:s';
public function canResolve(\ReflectionParameter $parameter): bool
{
return \in_array(
ReflectionHelper::getTypeName($parameter),
[\DateTimeImmutable::class, \DateTime::class],
true
);
}
public function resolve(\ReflectionParameter $parameter, array $values): \DateTimeInterface
{
$value = \end($values);
$class = ReflectionHelper::getTypeName($parameter);
$parsed = $class::createFromFormat(self::DATE_FORMAT, $value);
if (false === $parsed) {
throw InvalidArgumentException::createForParameter($value, $parameter);
}
return $parsed;
}
}
Testing
One problem with the nested config objects this library allows is testing. Specifically the need to define values for all values even though a specific test only cares about a couple of values.
For this reason, the DummyValueBuilder exists. Simply instantiate it the same as you would your
Builder.
The values for your config are then determined by the following rules:
- If you pass in a value, it is used, just like with the normal
Builder. If the parameter has a default value, that value is used.
This can be disabled, by calling
$dummyBuilder->useDefaultValue(false).If the parameter is nullable,
nullis used.This can be disabled, by calling
$dummyBuilder->useNullable(false).If none of the above match, a default value matching the parameter type is used.
The list of default values as
type => valuepairs can be:- overwritten be calling
setDefaultValues - added to be calling
addDefaultValues
The following defaults are used, if none are otherwise configured:
[ 'int' => 0, 'string' => '', 'float' => 0.0, 'array' => [], 'bool' => false, ]- overwritten be calling
khartir/typed-config 适用场景与选型建议
khartir/typed-config 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 141.89k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 08 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 khartir/typed-config 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 khartir/typed-config 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 141.89k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-08-21