定制 timdev/typed-config 二次开发

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

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

timdev/typed-config

Composer 安装命令:

composer require timdev/typed-config

包简介

Strongly-typed configuration for PHP applications

README 文档

README

What's This?

A small library to provide strictly typed access to data in an array, intended for use with a nested array of application configuration data. Nested values are dereferenced with dotted strings.

Usage

Install

Install this package with composer:

composer require timdev/typed-config

Usage

In a nutshell:

declare(strict_types=1);

// This should already be set in your php.ini, but this library depends on it,
// so you should verify that's set.
ini_set('assert.exception', 1);

// Could come from anywhere.
$configArray = [
    'myapp' => [
        'some-api' => [
            'baseUrl' => 'https://example.com/v1/',
            'timeout' => 30
        ],
        'allowed_ips' => [
            '192.168.0.1',
            '192.168.0.50'
        ],
        'optional_value' => null        
    ]
];

$config = new Config($configArray);

// Methods have narrow return type declarations:
$baseUrl = $config->string('myapp.some-api.baseUrl');
$timeout = $config->int('myapp.some-api.timeout');
$allowed = $config->list('myapp.allowed_ips');

// Attempting to access an undefined value throws.
$port = $config->int('invalid.key');  // Throws \TimDev\TypedConfig\Exception\KeyNotFound

// Note that the main methods do not have nulalble return types.
$optional = $config->bool('myapp.optional_value'); // Also throws!

// Use ->nullable to access valeus that are defined, but might be null:
$optional = $config->nullable->bool('myapp.optional_value'); 

Assumptions and Design

In order to use this library effectively, it's important to understand its design and the assumptions that inform that design.

Assumption: All valid configuration values are defined

As demonstrated in example above, client code attempting to access an undefined value will throw. Our opinion is that a library or application should ship a complete default configuration. This is always a win for users. This design means that users are alerted early when they try to access an invalid key due to a typo or transcription error.

If you think you need an optional configuration parameter, you should instead provide a default such as null or perhaps [].

Design: Relies on PHP's assert()

assert is a language construct with a very useful mechanism: they are optimized out in production. This library uses assert() to type-check your configuration values before they're returned from the various accessor-methods.

If you're unfamiliar with the configuration values that affect how assert works, you should review the documentation and your environment's configuration before using this library. The short version is that you'll want to ensure that assert.exception = 1 and zend.assertion = 1 in your development and testing environments.

Because this library depends on assert(), the type checks are optimized away in production environments (where zend.assertion = 0). This reduces some overhead but means that configuration access that would throw an AssertionError in development will instead:

  1. Throw a TypeError, or
  2. Not throw anything, in the case of a hash/list mismatch.

Motivation

We developed this while building an application with mezzio, where configuration data are generally pulled from an array. Arrays are nice, but often cause you to have to:

  1. Code defensively whenever you pull some value from your $config array.
  2. Add assertions or comments to let your static analyzer of choice figure out what is happening.

This library is an attempt to improve that situation.

Alternatives

There are other libraries that approach the problem from different perspectives.

selective-php/config takes a similar approach in that it provides strictly typed methods to fetch (possibly nullable) values. Unlike this library, it enforces types by casting values as it returns them. We take a more strict posture, and consider mismatched types to be an error in the software. Its nullable-typed methods return null if a config key is not defined, while we consider that to be an error as well.

setbased/typed-config is also similar. Like the previously discussed library, it will attempt to cast whatever it finds in the config data to the required type, and is more permissive about undefined config keys than this library is.

league/config takes an even more strict approach by helping you define a strict schema for your configuration data. We really like that approach (at least for some contexts), but are somewhat bewildered that it provides a single get(string $key): mixed accessor.

To Do

  • Do some benchmarks and determine if our usage of assert is actually providing any tangible benefit.

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

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

它主要适用于以下技术方向: 「configuration」 「config」 「array」 「dot」 「dot-access」 「strong typed」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 346
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 7
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-09-11