定制 khartir/typed-config 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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:

  1. If you pass in a value, it is used, just like with the normal Builder.
  2. If the parameter has a default value, that value is used.

    This can be disabled, by calling $dummyBuilder->useDefaultValue(false).

  3. If the parameter is nullable, null is used.

    This can be disabled, by calling $dummyBuilder->useNullable(false).

  4. If none of the above match, a default value matching the parameter type is used.

    The list of default values as type => value pairs 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,
    ]
    

khartir/typed-config 适用场景与选型建议

khartir/typed-config 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 141.89k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 08 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 khartir/typed-config 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 khartir/typed-config 我们能提供哪些服务?
定制开发 / 二次开发

基于 khartir/typed-config 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 141.89k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 11
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-08-21