halloverden / symfony-feature-flag-bundle
通过命令启用和禁用功能标志的能力
1.1.0
2022-09-20 14:01 UTC
Requires
- php: >=8.1
- doctrine/doctrine-bundle: ^2.5
- halloverden/symfony-entity-utils-bundle: ^3.0
- symfony/dependency-injection: ^5.4|^6.1
- symfony/http-kernel: ^5.4|^6.1
README
动态激活和禁用功能的能力。
安装
确保已全局安装Composer,如Composer文档的安装章节所述。
使用Symfony Flex的应用程序
打开命令行,进入您的项目目录并执行
$ composer require halloverden/symfony-feature-flag-bundle
未使用Symfony Flex的应用程序
步骤 1:下载Bundle
打开命令行,进入您的项目目录,并执行以下命令以下载此Bundle的最新稳定版本
$ composer require halloverden/symfony-feature-flag-bundle
步骤 2:启用Bundle
然后,通过将其添加到项目中config/bundles.php文件中已注册的Bundle列表中启用Bundle
// config/bundles.php return [ // ... HalloVerden\FeatureFlagBundle\HalloVerdenFeatureFlagBundle::class => ['all' => true], ];
使用方法
创建表示您的功能的实体。
<?php namespace App\Entity\FeatureFlag; use Doctrine\ORM\Mapping as ORM; use HalloVerden\FeatureFlagBundle\Entity\FeatureFlag; /** * Class TestFeatureFlag * * @package App\Entity\FeatureFlag * * @ORM\Entity() */ class TestFeatureFlag extends FeatureFlag { /* If you add additonal properties, override setFromConsole */ /** * @inheritDoc */ public static function getType(): string { return 'TEST'; } }
为您的新的FeatureFlag创建并运行迁移。
bin/console doctrine:migrations:diff bin/console doctrine:migrations:migrate
创建FeatureFlag。
bin/console feature-flag:create TEST
激活功能
bin/console feature-flag:activate TEST
禁用功能
bin/console feature-flag:dectivate TEST
检查功能是否已激活
<?php namespace App\Services; use App\Entity\FeatureFlag\TestFeatureFlag; use HalloVerden\FeatureFlagBundle\Services\FeatureFlagServiceInterface; class SomeService { private FeatureFlagServiceInterface $featureFlagService; public function __construct(FeatureFlagServiceInterface $featureFlagService) { $this->featureFlagService = $featureFlagService; } public function test(): void { if (!$this->featureFlagService->isActive(TestFeatureFlag::class)) { return; // or throw an exception } // Do the thing that requires this feature to be active. } }