Download the PHP package krystalcode/feature-toggle without Composer
On this page you can find all versions of the php package krystalcode/feature-toggle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download krystalcode/feature-toggle
More information about krystalcode/feature-toggle
Files in krystalcode/feature-toggle
Package feature-toggle
Short Description Extensible feature toggle functionality for PHP applications.
License MIT
Homepage https://github.com/krystalcode/feature-toggle
Informations about the package feature-toggle
FeatureToggle
About
FeatureToggle is an easy to use, extensible library that aims to provide feature toggle functionality for PHP applications.
Model
The library model is based on Toggles which are classes that define algorithms used to evaluate whether a feature should be on or off (enabled or disabled). Examples may be a Toggle that reads a configuration file and decides based on a variable value, a Toggle that checks whether the user has a subscription plan, or a Toggle that fetches information from an external API and decides based on that.
Requirements
Currently depends on the Symfony Yaml component (https://github.com/symfony/yaml).
How to use
Provided Toggles
YAML file
The YAML Toggle (ToggleConfigYaml class) loads the configuration variables from a YAML file and decides based on a variable value. It extends the ToggleConfig class which statically stores the configuration vatiables so that they are loaded only once even if feature toggling is used in multiple places in the code.
Assuming the following configuration file contents:
The following code will enable the feature on the development and stage environments and disable it on the production environment.
where the variable $yourCurrentEnvironment should have a value of "dev", "stage" or "prod". On runtime, the code will be executed in the development and staging environments but not in the production environment.
The configuration variables may also have other values apart from true or false. Say you would like to enable a feature only when the blue theme is in use:
The following code will enable the feature when the value of the variable "theme" is "blue".
PHP file
You can load the variables from a PHP configuration file. Using the same example as with YAML, your configuration file contents would be:
and your application code would be:
INI file
You can load the variables from an INI configuration file (.ini, see http://php.net/manual/en/function.parse-ini-file.php). Using the same example as with YAML, your configuration file contents would be:
and your application code would be:
Integrations
Yii Framework
You will need to add your configuration in the params.php file (Yii2) or in the main.php file (Yii1) as an array item with index 'featureToggle'. Using the same example as with YAML, your configuration file contents would be:
and your application code would be:
How to extend
Say you would like to enable a feature only for premium users on your website. You can write a custom Toggle as follows:
Full syntax
The examples above are using the easy syntax provided by a helper class. The full syntax for the YAML examples would be:
and
For PHP:
and for INI:
How to contribute
Feel free to submit pull requests. If you have ideas for new features or use cases that are not covered, open an issue to discuss.