qandidate / toggle-bundle
此包提供了与 qandidate/toggle 的集成。它提供了您在 Symfony 应用程序中实现功能开关所需的服务和配置。
1.4.2
2022-07-12 16:52 UTC
Requires
- php: >=7.2
- doctrine/annotations: ^1.13
- doctrine/common: ^2.13||^3.0
- qandidate/toggle: ^2.0
- symfony/framework-bundle: ^4.4||^5.0
- symfony/http-foundation: ^4.4.7||^5.0.7
- symfony/http-kernel: ^4.4.13||^5.1.5
- symfony/security-bundle: ^4.4||^5.0
Requires (Dev)
- broadway/coding-standard: ^1.2
- matthiasnoback/symfony-config-test: ^4.0
- matthiasnoback/symfony-dependency-injection-test: ^4.0
- phpstan/phpstan: ^1.0
- phpunit/phpunit: ^9.5
- 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。