code-foundation/flow-config 问题修复 & 功能扩展

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

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

code-foundation/flow-config

Composer 安装命令:

composer require code-foundation/flow-config

包简介

Cascading key/value configuration of preferences for platforms, installs, and users.

README 文档

README

Latest Stable Version License codecov CircleCI

Introduction

Flow Config is a key value configuration platform built on top of doctrine. It provides an PHP API for setting configuration at the platform that can be set by an install, and then set for a user, or other entity. Defaults are set in a single location, rather than scattering them through the code.

Installation

composer require code-foundation/flow-config

Usage

Attach the EntityIdentifier interface to user classes

This allows EntityConfig classes to use the above

class User implements CodeFoundation\FlowConfig\Interfaces\EntityIdentifier
{
    public function getEntityType(): string
    {
        return 'user';
    }

    public function getEntityId(): string
    {
        return $this->id;
    }
}

Build configuration classes

// Build a read-only config
$baseConfig = new ReadonlyConfig([
    'timezone' => 'UTC'
]);

// Build a system config
$systemConfig = new DoctrineConfig($this->getEntityManager());

// Build a entity based config
$entityConfig = new DoctrineEntityConfig($this->getEntityManager());

// Build the cascading configuration objects that tries each of the above in turn.
$cascadeConfig = new CascadeConfig($baseConfig, $systemConfig, $entityConfig);

If you don't want the Config services to automatically flush changes to the database, pass a false for $autoFlush in the constructor of the service.

$systemConfig = new DoctrineConfig($this->getEntityManager(), false);

$entityConfig = new DoctrineEntityConfig($this->getEntityManager(), false);

Control key access using the AccessControlInterface

In some cases, you may wish to control each specific attempt to get/set specific keys, and this is where implementing the AccessControlInterface is useful.

By default, the NullAccessControl class is instantiated, which defaults to allowing all get and set attempts.

By implementing the interface, you have control over allowing or denying get and set access to keys based on the key itself, or optionally the entity associated with the key, or one of its method values.

class MyAccessControlClass implements AccessControlInterface
{
    // Set our read-only keys.
    private $readOnlyKeys = ['abc123'];
    
    // Set our keys that are restricted and never returned.
    private $restrictedKeys = ['xyz987'];

    // Set entities that cannot be modified
    private $readOnlyEntities = [MyEntityClass::class];

    public function canGetKey(string $key, ?EntityIdentity $entity = null): bool
    {
        // Return whether the key is not in our read-only array.
        return \in_array($key, $this->readOnlyKeys) === false;
    }
    
    public function getSetKey(string $key, ?EntityIdentity $entity = null): bool
    {
        // Check whether the key is in our restricted array.
        if (\in_array($key, $this->restrictedKeys) === true) {
            return false;
        }

        // Check whether the entity is read only
        if ($entity !== null && in_array(\get_class($entity), $this->readOnlyEntities) === true) {
            return false;
        }
        
        return true;
    }
}

Examples

$user1 = new User()->setId(999);
$user1 = new User()->setId(1001);

echo $systemConfig->get('timezone'); // UTC
echo $entityConfig->get('timezone'); // UTC
echo $cascadeConfig->getEntityConfigItem('timezone', $user1); // UTC
echo $cascadeConfig->getEntityConfigItem('timezone', $user2); // UTC

// Update the setting for that platform.
$entityConfig->set('timezone', 'Australia/Melbourne');
echo $systemConfig->get('timezone'); // UTC
echo $entityConfig->get('timezone'); // 'Australia/Melbourne'
echo $cascadeConfig->getEntityConfigItem('timezone', $user1); // 'Australia/Melbourne'
echo $cascadeConfig->getEntityConfigItem('timezone', $user2); // 'Australia/Melbourne'

// Update a given users settings
$cascadeConfig->setByEntity($user1, 'timezone', 'Pacific/Auckland');
echo $systemConfig->get('timezone'); // UTC
echo $entityConfig->get('timezone'); // 'Australia/Melbourne'
echo $cascadeConfig->getEntityConfigItem('timezone', $user1); // 'Pacific/Auckland'
echo $cascadeConfig->getEntityConfigItem('timezone', $user2); // 'Australia/Melbourne'

Supported platforms

Future plans

  • Validation
  • Allowed values
  • Factories
  • Eloquent backend

Contact

Github: https://github.com/code-foundation/flow-config Email: contact@codefoundation.com.au

License

Flow Config is distributed under the MIT license.

code-foundation/flow-config 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

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