Make use of Feature Flags

Feature flags are used to switch between different strategies in the code or to include features in the build that are not ready for primetime yet or have been labeled as experimental.

Every feature flag consists of a database record that includes the name of the feataure a humand readable description and the state (enabled / disabled). The second part of a feature flag is an implementing class. This class allows one to define actions to be executed when a feature is enabled or disabled, much like the setUp and tearDown methods in tests.

Create the database record

The easiest way to create the database record is to use the framework’s DB migration capabilites. Create a SQL file in the migrations folder and use the following insert query as a reference:

insert into feature_flags ( ff_name, ff_caption, ff_description, ff_enabled ) values ( 'UseRestmanImport', 'Use Restman for Bundle Imports', 'If activated API Gateway Manager will use Restman instead of GMU to import bundles to a gateway.', 1 )

Implementing Class

This is an example from the API Gateway Manager

<?php namespace Osiris\FeatureFlags\Flags; class UseRestmanForImports implements FlagInterface { public function activate(): void { // nothing to do } public function deactivate(): void { // nothing to do } }

Register the Flag with the Framework

To register the newly created flag with the framework, we need to add it to the list of available features in the class FeatureNames.

Note, that the constant name of the feature needs to be the same in this class and in the SQL statement.

abstract class FeatureNames { public const IMPORT_DEPENDENCY_GRAPH = 'ImportDependencyGraph'; public const USE_RESTMAN_FOR_IMPORTS = 'UseRestmanImport'; public static function factory($flag): FlagInterface { $implementations = [ static::IMPORT_DEPENDENCY_GRAPH => ImportDependencyGraph::class, static::USE_RESTMAN_FOR_IMPORTS => UseRestmanForImports::class ]; $implementation = $implementations[$flag]; return new $implementation; } }

How to Check if a Feature is Enabled

To check if a feature is enabled you can use the static method in the feature classe

 

Related pages