code-foundation/flow-config

平台、安装和用户的偏好设置的关键/值配置级联。

0.3.0 2020-01-19 02:28 UTC

This package is auto-updated.

Last update: 2024-09-20 07:16:03 UTC


README

Latest Stable Version License codecov CircleCI

介绍

Flow Config 是建立在 doctrine 之上的键值配置平台。它提供了一个 PHP API,可以在平台上设置安装可以设置的配置,然后为用户或其他实体设置。默认值在单个位置设置,而不是通过代码散布。

安装

composer require code-foundation/flow-config

使用方法

将 EntityIdentifier 接口附加到用户类中

这允许 EntityConfig 类使用上述内容

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

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

构建配置类

// 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);

如果您不希望配置服务自动将更改刷新到数据库中,请将服务构造函数中的 $autoFlush 参数传递为 false。

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

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

使用 AccessControlInterface 控制键访问

在某些情况下,您可能希望控制对特定键的每个特定尝试获取/设置,这时实现 AccessControlInterface 是有用的。

默认情况下,将实例化 NullAccessControl 类,它默认允许所有 getset 尝试。

通过实现该接口,您可以控制根据键本身、或可选地与键关联的实体或其方法值,允许或拒绝获取和设置访问权限。

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;
    }
}

示例

$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'

支持的平台

未来计划

  • 验证
  • 允许的值
  • 工厂
  • Eloquent 后端

联系

Github: https://github.com/code-foundation/flow-config 邮箱: [email protected]

许可证

Flow Config 在 MIT 许可证下分发。