yannickl88/features-bundle

Symfony扩展,用于管理功能标签

安装次数: 73,430

依赖项: 0

建议者: 0

安全: 0

星级: 19

关注者: 5

分支: 6

开放性问题: 1

类型:symfony-bundle

2.3.0 2024-06-06 11:01 UTC

This package is auto-updated.

Last update: 2024-09-06 11:34:38 UTC


README

此Symfony扩展提供了一种在项目内管理功能的方法。一个常见的用例是在特定条件下激活某些功能。例如,你可能希望在用户具有特定角色或不在生产环境中(如测试)时激活功能。

使用此扩展,您可以配置功能是激活还是不激活。使用解析器您决定功能何时激活或不激活。

要求

  • PHP 7.3 或更高版本
  • Symfony 4.2 或更高版本

建议通过composer安装: composer require yannickl88/features-bundle

之后,您需要在应用程序的内核中注册该扩展

<?php
// app/AppKernel.php

use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = [
            new Yannickl88\FeaturesBundle\FeaturesBundle(),
            // …
        ];
    }
}

使用方法

所有配置都是通过服务和应用程序配置来完成的。以下示例中,我们希望在GET参数beta设置为on时启用功能。

因此,在应用程序的config.yml中配置您的功能。

features:
    tags:
        beta: # our feature tag
            request: ["beta", "on"] # 'app.features.request_resolver' will resolve this key

在这里,我们定义了一个名为beta的功能标签,它将通过request解析器进行解析。现在我们需要配置request解析器。我们使用以下服务定义来完成此操作

services:
    app.features.request_resolver:
        class: App\Feature\RequestResolver
        arguments:
            - '@Yannickl88\FeaturesBundle\Feature\Feature'
        tags:
            # config-key is set to resolve the configured key: "request" with the options "beta" and "on"
            - { name: features.resolver, config-key: request }

在这里,我们创建了一个名为app.features.request_resolver的服务,并将其标记为features.resolver。然后,该扩展将捕获该服务并将其注册,以便我们可以在功能标签中使用它。我们还提供了一个config-key值。这是我们在config.yml中定义的beta标签下的键。这将把您的配置与解析器连接起来。

最后一件要做的事情是实现RequestResolver

namespace App\Feature;

use Symfony\Component\HttpFoundation\RequestStack;
use Yannickl88\FeaturesBundle\Feature\FeatureResolverInterface;

class RequestResolver implements FeatureResolverInterface
{
    private $request_stack;
    
    public function __construct(RequestStack $request_stack)
    {
        $this->request_stack = $request_stack;
    }

    /**
     * {@inheritdoc}
     */
    public function isActive(array $options = []): bool
    {
        // Feature is inactive when there is no request
        if (null === $request = $this->request_stack->getMasterRequest()) {
            return false;
        }

        // $options contains ["beta", "on"] for the 'beta' feature tag
        list($key, $expected_value) = $options;

        return $request->get($key) === $expected_value;
    }
}

现在我们可以在代码中使用该功能了。所以,如果我想检查功能,我可以按照以下方式注入它

services:
    app.some.service:
       class: App\Some\Service
       arguments:
           - '@Yannickl88\FeaturesBundle\Feature\Feature'
       tags:
           - { name: features.tag, tag: beta }

注意,这里我们不是直接注入功能,而是标记服务。扩展将为您替换功能。因此,您可以在代码中使用它,如下所示

namespace App\Some;

use Yannickl88\FeaturesBundle\Feature\Feature;

class Service
{
    private $feature;
    
    public function __construct(Feature $feature)
    {
        $this->feature = $feature;
    }

    public function someMethod(): void
    {
        if ($this->feature->isActive()) {
            // do some extra beta logic when this feature is active
        }
    }
}

因此,如果我现在将?beta=on添加到我的URL。功能将触发。

注意:如果您移除标记,它将注入一个已弃用的功能。此弃用功能在使用isActive时将触发警告,因此您可以快速看到哪里使用了未使用的功能。

Twig

在您的twig模板中也可以检查功能。只需使用feature函数检查功能是否启用。

{% if feature("beta") %}
    {# do some extra beta logic when this feature is active #}
{% endif %}

高级主题

可以为每个功能标签配置多个解析器。您只需在config.yml中继续添加更多即可。因此,在示例中,我们可以将其扩展为

features:
    tags:
        beta:
            request: ["beta", "on"]
            other: ~
            more: ["foo"]

所有解析器都必须解析为true,以便此功能处于活动状态。如果您想要检查多个条件,这很有用。

此外,如果您想使用多个解析器,其中只有一个需要解析为true,则可以使用链式解析器。可以这样做

features:
    tags:
        beta:
            chain:
                request: ["beta", "on"]
                other: ~
                more: ["foo"]

注意,这里我们有一个解析器chain,下面是您之前配置的配置。