nemishkor / toggle-bundle
此套餐提供与qandidate/toggle的集成。它提供您在Symfony应用程序中实现功能开关所需的服务和配置。
1.3.1
2020-10-20 14:58 UTC
Requires
- php: >=7.2
- doctrine/common: ~3.0
- qandidate/toggle: ^2.0
- symfony/framework-bundle: ^4.4||^5.0
- symfony/http-foundation: ^4.4||^5.0,>=5.0.7
- symfony/http-kernel: ^4.4||^5.0
- symfony/security-bundle: ^4.4||^5.0
Requires (Dev)
- broadway/coding-standard: ^1.0
- matthiasnoback/symfony-config-test: ^4.0
- matthiasnoback/symfony-dependency-injection-test: ^4.0
- phpstan/phpstan: @stable
- phpunit/phpunit: ^8.0
- symfony/browser-kit: ^4.4||^5.0
- symfony/twig-bundle: ^4.4||^5.0
- twig/twig: ^3.0
Suggests
- twig/twig: For using the twig helper
README
此套餐提供与我们的切换库的集成。它提供您在应用程序中实现功能开关所需的服务和配置。
关于
在以下位置阅读关于此存储库的博客系列文章:
- http://labs.qandidate.com/blog/2014/08/18/a-new-feature-toggling-library-for-php/
- http://labs.qandidate.com/blog/2014/08/19/open-sourcing-our-feature-toggle-api-and-ui/
安装
使用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。