yannickl88 / features-bundle
Symfony扩展,用于管理功能标签
Requires
- php: ^8.1
- symfony/config: ^5.4||^6.0
- symfony/dependency-injection: ^5.4||^6.0
- symfony/finder: ^5.4||^6.0
- symfony/framework-bundle: ^5.4||^6.0
- symfony/http-kernel: ^5.4||^6.0
Requires (Dev)
- phpspec/prophecy-phpunit: ^2.0
- phpunit/phpunit: ^9.5.8
- symfony/twig-bundle: ^5.4||^6.0
- symfony/yaml: ^5.4||^6.0
- twig/twig: ^3.4.3
Suggests
- twig/twig: Allows use of features in twig templates through the FeaturesExtension
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
,下面是您之前配置的配置。