antfroger/progressive-bundle

用于加载 Progressive 库的包,该库可以渐进式、快速且简单地启用新功能

安装: 3

依赖: 0

建议: 0

安全: 0

星标: 1

关注者: 2

分支: 1

公开问题: 1

类型:symfony-bundle

v1.1 2022-03-25 17:50 UTC

This package is auto-updated.

Last update: 2024-09-08 21:56:18 UTC


README

Symfony 集成功能库 Progressive

Build Status Latest stable version

安装

使用 Composer 安装此包

composer require antfroger/progressive-bundle

配置

1. 启用 Bundle

首先,将 Bundle 添加到项目 config/bundles.php 文件中已注册 Bundle 列表

// config/bundles.php

return [
    // ...
    Af\ProgressiveBundle\AfProgressiveBundle::class => ['all' => true],
];

2. 配置 Bundle

创建配置文件 config/packages/af_progressive.yaml

唯一必需的键是 config。该键需要配置应用程序功能的 yaml 文件的路径。
最小配置如下所示

# config/packages/af_progressive.yaml
af_progressive:
  config: '%kernel.project_dir%/config/features.yaml'

但是,您也可以在 context 键下定义要存储在 Context 对象 中的变量。

# config/packages/af_progressive.yaml
af_progressive:
  config: '%kernel.project_dir%/config/features.yaml'
  context:
    env: '%kernel.environment%'

然后,您需要创建包含您功能的文件。
它必须包含至少 features

# config/features.yaml
features: []

但很快,您就可以开始添加更多功能
(每次更改此文件时,您可能需要清除应用程序的缓存:php bin/console cache:clear)

# config/features.yaml
features:
  dark-theme: true
  call-center:
    between-hours:
        start: 9
        end: 19
  homepage-v2:
    partial:
      env: ['dev', 'preprod']
      roles: ['ROLE_ADMIN']
  slack-message:
    env: ['dev', 'preprod']
  secret-feature:
    users: ['antoine']
  new-design:
    unanimous:
      env: ['dev', 'preprod']
      roles: ['ROLE_DEV', 'ROLE_ADMIN']

查看 Progressive 文档 以获取有关功能配置的更多信息。

使用方法

您可以使用 Progressive 在控制器中,使用 Symfony 的自动注入

  public function info(Progressive $progressive): Response
  {
      if ($progressive->isEnabled('call-center')) {
          // Do what you want when the feature `call-center` is enabled
      }
  }

或在模板中

{% if is_enabled('call-center') %}
    {# Do what you want when the feature `call-center` is enabled #}
{% endif %}

规则

内置规则

Progressive 内置了多个内置规则

  • enabled: true|false
    enabled 启用(或禁用)了在所有人、任何地方、所有时间的功能。

Symfony 特定规则

此包提供了一些特定的 Symfony 规则

env: []

env 根据应用程序环境启用(或禁用)功能。
值是一个环境名称的数组。

features:
  send-slack-message:
    env: ['dev', 'preprod']

roles: []

roles 只为特定角色启用(或禁用)功能。
值是一个角色名称的数组。

此示例配置仅允许管理员和开发人员使用 new-amazing-homepage 功能。

features:
  new-amazing-homepage:
    roles: ['ROLE_ADMIN', 'ROLE_DEV']

users: []

users 比较于 roles 更细致,因为它允许您在用户级别启用功能。
值是一个用户名的数组。

此示例配置仅允许用户 antoine 和 ted 使用 secret-feature 功能。

features:
  secret-feature:
    users: ['antoine', 'ted']

创建自己的规则

我相信您很快就会想要创建自己的规则,以便根据应用程序逻辑渐进式地启用功能。
这正是自定义规则发挥作用的地方!(有关 自定义规则 的更多信息,请参阅 Progressive 文档

要创建自己的规则并在 feature.yaml 文件中使用它们,您只需要创建一个扩展 Progressive\Rule\RuleInterface 的类。
就是这样!
Symfony 自动注入负责其余部分。

假设您想在联系页面中显示聊天,但仅在工作时间内显示(例如早上9点到晚上7点)。

  1. 首先,创建规则
// src/Progressive/BetweenHours.php
namespace App\Progressive;

use Progressive\ParameterBagInterface;
use Progressive\Rule\RuleInterface;

class BetweenHours implements RuleInterface
{
    /**
     * {@inheritdoc}
     */
    public function getName(): string
    {
        return 'between-hours';
    }

    /**
     * {@inheritdoc}
     */
    public function decide(ParameterBagInterface $bag, array $hours = []): bool
    {
        if (!isset($hours['start']) || !is_int($hours['start']) || !isset($hours['end']) || !is_int($hours['end'])) {
            return false;
        }

        $now = new \DateTime();
        $hour = $now->format('H');
        return $hours['start'] <= $hour && $hour < $hours['end'];
    }
}
  1. 现在,您可以在 feature.yaml 文件中使用这个新规则
features:
  customer-service-chat:
    between-hours: # same as `BetweenHours::getName()`
      start: 9
      end: 19
  1. 您现在使用了一个使用这个新规则的功能。
    让我们在一个控制器或模板中使用它
public function customerService(Progressive $progressive): Response
  {
      if ($progressive->isEnabled('customer-service-chat')) {
        // ...
      }
  }
{% if is_enabled('customer-service-chat') %}
    {# Display the chat #}
{% endif %}

策略

多亏了策略,您可以组合规则的强大功能。
假设您想启用您的新功能 one-click-payment

  • 针对开发环境和预生产中的所有人...
  • 但仅针对生产环境中的管理员...
  • 以及两位测试人员(antoine和laurent)。

这个配置将完成工作

features:
  one-click-payment:
    partial:
      env: ['dev', 'preprod']
      roles: ['ROLE_ADMIN']
      users: ['antoine', 'laurent']

Progressive自带两个内置规则
(但它们只是嵌套的规则,您可以创建自己的策略!)

  • unanimous: []
    unanimous 是一种策略(几个规则的组合)。如果所有条件都满足/如果所有规则都为真,则启用功能。
  • partial: []
    partial 也是一种策略。如果满足条件之一/如果至少有一个规则为真,则启用功能。

命令

progressive:features

命令 progressive:features 列出了Progressive中配置的所有功能

  $ php bin/console progressive:features
  Available features:
    dark-theme
    homepage-v2
    customer-service-chat

如果您指定了功能的名称,将显示其配置

  $ php bin/console progressive:features dark-theme
  Name:
    dark-theme

  Config:
    unanimous:
      env: dev, preprod
      roles: ROLE_DEV

progressive:rules

命令 progressive:rules 列出了Progressive提供的规则

  $ php bin/console progressive:rules
  Available rules:
    enabled
    env
    partial
    roles
    unanimous
    users

Web工具栏 & 分析器

为了快速轻松地访问应用程序中可用的功能 - 以及查看哪些功能对您已启用 - Symfony的Web工具栏包括一个包含您定义的功能的标签页。

web toolbar

Symfony的分析器也包括一个标签页,列出应用程序的所有功能和它们的相应配置。

profiler