项目作者: trompette

项目描述 :
Feature toggle infrastructure enabling continuous deployment
高级语言: PHP
项目地址: git://github.com/trompette/php-feature-toggles.git
创建时间: 2019-12-17T13:18:18Z
项目社区:https://github.com/trompette/php-feature-toggles

开源协议:MIT License

下载


License
Stable Version
Automated Tests
Static Analysis

trompette/feature-toggles

This PHP library implements a feature toggle infrastructure.

Using trompette/feature-toggles library can help a team to deliver new
features to users iteratively and safely, in other words: it enables continuous
deployment.

For more information on the topic, see Feature Toggles (aka Feature Flags) on
MartinFowler.com
.

Installation

The trompette/feature-toggles library is distributed on
Packagist.

It can be added as a project dependency with the following command:

  1. composer require trompette/feature-toggles

Standalone usage

When working on a new version of a page, deploying gradually the new version can
bring a lot of confidence to a team.

But it also brings more work, as the team needs to:

  • keep the code implementing the current version,
  • add the code implementing the new version,
  • and consistently enable the new version for some users.

With trompette/feature-toggles library, enabling the new version is done by
asking the toggle router if the current user has a
feature:

  1. if ($toggleRouter->hasFeature($currentUser, 'new_page_version')) {
  2. $templating->render('new_page.tpl', $newParameters);
  3. } else {
  4. $templating->render('page.tpl', $parameters);
  5. }

Feature registry

Before using the toggle router, new_page_version
feature has to be registered:

  1. use Trompette\FeatureToggles\FeatureDefinition;
  2. use Trompette\FeatureToggles\FeatureRegistry;
  3. $featureRegistry = new FeatureRegistry();
  4. $featureRegistry->register(new FeatureDefinition(
  5. $name = 'new_page_version',
  6. $description = 'awesome new version of a page',
  7. $strategy = 'whitelist'
  8. ));

Toggling strategies

When defining a feature, a toggling strategy has
to be referenced to specify the algorithm deciding if a target has a feature.

Implemented strategies are:

  • feature is enabled for all targets or disabled for all targets, see
    OnOff class,
  • feature is enabled for whitelisted targets only, see
    Whitelist class,
  • feature is enabled for a percentage of all targets, see
    Percentage class.

And strategies can be combined with boolean operators, like so:
onoff and whitelist, onoff or whitelist or percentage, etc.

Toggle router

Now that the feature registry is configured, the
toggle router can be created:

  1. use Doctrine\DBAL\Connection;
  2. use Trompette\FeatureToggles\DBAL\WhitelistStrategyConfigurationRepository;
  3. use Trompette\FeatureToggles\ToggleRouter;
  4. use Trompette\FeatureToggles\WhitelistStrategy\Whitelist;
  5. $connection = new Connection(...);
  6. $repository = new WhitelistStrategyConfigurationRepository($connection);
  7. $whitelist = new Whitelist($repository);
  8. $toggleRouter = new ToggleRouter(
  9. $featureRegistry,
  10. $strategies = ['whitelist' => $whitelist]
  11. );

Strategies are injected as an array indexed with names: these are the references
that should be used when registering features.

Feature configuration

The toggle router can be used to configure a feature
for a given strategy:

  1. $toggleRouter->configureFeature('feature', 'onoff', 'on');
  2. $toggleRouter->configureFeature('feature', 'onoff', 'off');
  3. $toggleRouter->configureFeature('feature', 'whitelist', 'allow', 'target');
  4. $toggleRouter->configureFeature('feature', 'whitelist', 'disallow', 'target');
  5. $toggleRouter->configureFeature('feature', 'percentage', 'slide', 25);
  6. $toggleRouter->configureFeature('feature', 'percentage', 'slide', 50);

Configuration changes are persisted by calling the associated method on the
strategy instance.

All Doctrine DBAL configuration repositories can migrate a schema, since they
all implement the SchemaMigrator interface:

  1. use Doctrine\DBAL\Connection;
  2. use Trompette\FeatureToggles\DBAL\WhitelistStrategyConfigurationRepository;
  3. $connection = new Connection(...);
  4. $repository = new WhitelistStrategyConfigurationRepository($connection);
  5. $repository->migrateSchema();

Usage with Symfony

All previous code is optional when using Symfony: everything is glued together
by the FeatureTogglesBundle class.

Registering the bundle in
config/bundles.php is needed to benefit from the Symfony integration:

  1. return [
  2. // ...
  3. Trompette\FeatureToggles\Bundle\FeatureTogglesBundle::class => ['all' => true],
  4. ];

Bundle configuration

The bundle can be configured as
described by config:dump-reference:

  1. # Default configuration for extension with alias: "feature_toggles"
  2. feature_toggles:
  3. doctrine_dbal_connection: doctrine.dbal.default_connection
  4. declared_features:
  5. # Prototype
  6. name:
  7. description: ~ # Required
  8. strategy: ~ # Required

For technical details, see
FeatureTogglesConfiguration
class.

Container services

There is only one service declared as public: the toggle
router
with Trompette\FeatureToggles\ToggleRouter
or Trompette\FeatureToggles\ToggleRouterInterface as identifier.

There are also useful console commands defined as services and tagged with
console.command:

  1. feature-toggles
  2. feature-toggles:configure-feature Configures a feature
  3. feature-toggles:migrate-dbal-schema Migrates DBAL schema
  4. feature-toggles:show-feature-configuration Shows a feature configuration

More information about the commands can be found in their help messages.

For technical details, see
FeatureTogglesExtension class.

License

The trompette/feature-toggles library is released under the MIT License.

See the LICENSE file for more details.

Acknowledgments

The trompette/feature-toggles library is inspired by a practice and a tool
used by the Food Assembly development team.

The team discovered the practice with the article Web Experimentation with New
Visitors
on Etsy’s Engineering
Blog
.