nemishkor/toggle-bundle

此套餐提供与qandidate/toggle的集成。它提供您在Symfony应用程序中实现功能开关所需的服务和配置。

安装: 3

依赖项: 0

建议者: 0

安全: 0

星标: 1

关注者: 2

分支: 27

类型:symfony-bundle

1.3.1 2020-10-20 14:58 UTC

This package is auto-updated.

Last update: 2024-09-20 23:49:08 UTC


README

此套餐提供与我们的切换库的集成。它提供您在应用程序中实现功能开关所需的服务和配置。

Build Status

关于

在以下位置阅读关于此存储库的博客系列文章:

安装

使用Symfony Flex

使用Symfony Flex是安装和配置QandidateToggleBundle的最简单方法。

确保您已安装Symfony Flex

$ composer require symfony/flex ^1.0
$ composer config extra.symfony.allow-contrib true

安装套餐

$ composer require qandidate/toggle-bundle ^1.0

Symfony Flex将自动注册和配置套餐。

手动

将套餐添加到您的composer.json

$ composer require qandidate/toggle-bundle ^1.0

将套餐添加到您的Kernel

$bundles = array(
    // ..
    new Symfony\Bundle\SecurityBundle\SecurityBundle(),
    new Symfony\Bundle\TwigBundle\TwigBundle(),
    new Qandidate\Bundle\ToggleBundle\QandidateToggleBundle(),
);

配置

qandidate_toggle:
    persistence: in_memory|redis|factory|config
    context_factory: null|your.context_factory.service.id
    redis_namespace: toggle_%kernel.environment% # default, only required when persistence = redis
    redis_client: null|your.redis_client.service.id # only required when persistence = redis
    collection_factory: # only required when persistence = factory
        service_id: your.collection_factory.service.id
        method: create

Symfony的示例配置

qandidate_toggle:
    persistence: config
    toggles:
      always-active-feature:
        name: always-active-feature
        status: always-active
      inactive-feature:
        name: inactive-feature
        status: inactive
        conditions: 
      conditionally-active:
        name: conditionally-active
        status: conditionally-active
        conditions:
         - name: operator-condition
           key: user_id
           operator:
               name: greater-than
               value: 42

示例用法

用法可能因您的应用程序而异。此示例使用提供的UserContextFactory,但您可能需要创建自己的工厂。

<!-- services.xml -->

<service id="acme.controller" class="Acme\Controller">
    <argument type="service" id="qandidate.toggle.manager" />
    <argument type="service" id="qandidate.toggle.user_context_factory" />
</service>
// Acme\Controller

public function __construct(
    /* ArticleRepository, Templating, ..*/ 
    ToggleManager $manager, 
    ContextFactory $contextFactory
) {
    // ..
    $this->manager = $manager;
    $this->context = $contextFactory->createContext();
}

// ..

public function articleAction(Request $request)
{
    $this->article = $this->repository->findBySlug($request->request->get('slug'));

    return $this->templating->render('article.html.twig', array(
        'article'        => $article,
        'enableComments' => $this->manager->active('comments', $this->context),
    ));
}

您可以在Resources/doc/example目录中找到一个使用Symfony MicroKernelTrait的示例。

注解用法

您还可以在控制器上使用@Toggle注解。当切换未激活时,将抛出404异常。

use Qandidate\Bundle\ToggleBundle\Annotations\Toggle;

/**
 * @Toggle("cool-feature")
 */
class FooController
{

    /**
     * @Toggle("another-cool-feature")
     */
    public function barAction()
    {
    }

    public function bazAction()
    {
    }
}

Twig用法

如果您使用Twig,您还可以使用该函数

{% if feature_is_active('comments') %}
    {# Awesome comments #}
{% endif %}

或者Twig测试

{% if 'comments' is active feature %}
    {# Awesome comments #}
{% endif %}

这两个都注册在ToggleTwigExtension中。

数据收集器

使用数据收集器,您可以查看所有开关的概览。在工具栏中,您可以查看所有条件和当前状态。

在面板中,您有两个列表

  • 您可以看到所有键及其当前值。
  • 然后您可以看到所有配置的开关、它们的条件和它们是否激活。

测试

要运行PHPUnit测试

$ ./vendor/bin/phpunit

许可证

MIT,请参阅LICENSE。