vshf/php-config 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

vshf/php-config

Composer 安装命令:

composer require vshf/php-config

包简介

PHP configuration system to handle app settings

README 文档

README

VSHF PHP Config Manager is a settings/configuration manager for PHP applications. It provides functionality to handle settings and resources with their properties.

Usage

To use the VSHF Config Manager, you need to instantiate the Config class:

$settings = new \VSHF\Config\Config();

You can also initialize it with settings:

$settings = new \VSHF\Config\Config([
   'setting1' =>'value1',
   'setting2' =>'value2'
]);

Contexts

The default setting context is the app context. When initializing with settings, they will be added to this context.

You can hydrate different contexts later on:

$settings->hydrate(
    [
        'settingA' =>'valueA',
        'settingB' =>'valueB'
    ],
    'myContext'
);

This will create an internal settings tree with the following structure:

[
    'app' => [
        'setting1' =>'value1',
        'setting2' =>'value2'
    ],
    'myContext' =>[
        'settingA' =>'valueA',
        'settingB' =>'valueB'
    ]
]

Resources and properties

In the case of having a collection of resources and their properties, where the properties can be considered as settings for each resource, you can use the Config Manager to handle them.

For example, if you have a collection of services with the isPriced property, you can observe the value of this property for different services.

[
    'service_1' => [
        'isPriced' => FALSE,
        ...
    ],
    'service_2' => [
        'isPriced' => TRUE,
        ...
    ],
    ...
]

To hydrate with a resource collection:

foreach ($collection as $itemId => $item) {
    $settings->hydrateResource(
        [
             'isPriced' => TRUE,
             // other properties
        ],
        'services', // Context
        $itemId // Resource ID
    );
}

Observers

Each setting must have its corresponding Observer, which implements the ObserverInterface. An observer can handle one or more settings.

To register an observer:

// In the main context:
$settings->registerObserver('settingId', MyObserver::class);


// In a custom context
$settings->registerObserver('settingId', MyObserver::class, 'myContext');

// Observing multiple settings with a single observer:
$settings->registerObserver('settingA', MyObserver::class);
$settings->registerObserver('settingB', MyObserver::class);

Resource property observers

Similarly, each resource property must have its PropertyObserver, which implements the PropertyObserverInterface. An observer can handle one or more properties.

To register a PropertyObserver:

$settings->registerObserver('propertyId', MyPropertyObserver::class, 'services');

// Observing multiple properties with a single observer:
$settings->registerObserver('propertyA', MyPropertyObserver::class, 'services');
$settings->registerObserver('propertyB', MyPropertyObserver::class, 'services');

Get and save

To retrieve a setting:

// From the main context:
$settings->get('settingA');

// From a custom context:
$settings->get('settingA', 'myContext');

To save a setting:

// In the main context:
$settings->save('settingA', 'newValue');

// In a custom context:
$settings->save('settingA', 'newValue', 'myContext');

Resource properties

To retrieve a property of a given resource:

$settings->getProperty('propertyA', 'services', 'resourceId');

To save a property of a given resource:

$settings->saveProperty('propertyA', 'services', 'resourceId');

To retrieve a given resource with all its properties:

$settings->getResourceProperties('services', 'resourceId');

Setting dependencies

A setting or a resource property can depend on one or more other settings, even from different contexts.

To set dependencies, the dependencies() method of the observer must return a Dependency object.

// Inside settingA's observer class
public static function dependencies(){
    $dependency = new \VSHF\Config\Dependency();
    $dependency
        ->on('settingB')
        ->beingEqualTo('certainValue')
        ;
    return $dependency;
}

In this example, if settingB is equal to certainValue, then settingA is properly returned. Otherwise, NULL is returned.

It's important to note that NULL should not be a default/proper setting value.

Complex dependencies

Dependencies can be complex and involve logical operators such as AND and OR. You can construct complex dependencies using the Dependency object.

$dependency = new \VSHF\Config\Dependency();

// AND
$dependency
    ->on('settingB')
    ->beingEqualTo('certainValue')
    ->and('settingC')
    ->beingTruthy()
    ;
    
// OR
$dependency
    ->on('settingB')
    ->beingEqualTo('certainValue')
    ->or('settingC')
    ->beingTruthy()
    ;

//INVALID, triggers an error
$dependency
    ->on('settingB')
    ->beingEqualTo('certainValue')
    ->and('settingC')
    ->beingTruthy()
    ->or('settingD')
    ->beingFalsy()
    ;

/*
 * This is equal to:
 *      settingB === certainValue && (
 *          settingC || !settingD
 *      )
 */
$dependency
    ->on('settingB')
    ->beingEqualTo('certainValue')
    ->andGroup()
    ->on('settingC')
    ->beingTruthy()
    ->or('settingD')
    ->beingFalsy()
    ->endGroup()
    ;

/*
 * This is equal to:
 *      settingB === certainValue || (
 *          settingC && !settingD
 *      )
 */
$dependency
    ->on('settingB')
    ->beingEqualTo('certainValue')
    ->orGroup()
    ->on('settingC')
    ->beingTruthy()
    ->and('settingD')
    ->beingFalsy()
    ->endGroup()
    ;

License

This project is open-source software licensed under the MIT license

vshf/php-config 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-05-05