PHPackages                             krystalcode/toggle - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. krystalcode/toggle

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

krystalcode/toggle
==================

Extensible feature toggle functionality for PHP applications.

01.6kPHPCI failing

Since Mar 26Pushed 6y ago1 watchersCompare

[ Source](https://github.com/krystalcode/php-toggle)[ Packagist](https://packagist.org/packages/krystalcode/toggle)[ RSS](/packages/krystalcode-toggle/feed)WikiDiscussions master Synced 6d ago

READMEChangelogDependenciesVersions (1)Used By (0)

[![SensioLabsInsight](https://camo.githubusercontent.com/56a435d98f5d72bbaf3043953aa9b19b3fc4a4867cd6075350d06c46aad906bc/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f36376632376362652d393132352d346237652d613131312d3234633261613736663138362f6269672e706e67)](https://insight.sensiolabs.com/projects/67f27cbe-9125-4b7e-a111-24c2aa76f186)

[![Scrutinizer Code Quality](https://camo.githubusercontent.com/bf65f9af4d6338f0dadc29ef8ea92e8bf9f577d32b39f4df24d0322ed5ec943d/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6b72797374616c636f64652f7068702d746f67676c652f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/krystalcode/php-toggle/?branch=master) [![Build Status](https://camo.githubusercontent.com/d1d33e6e4b0d5b7fa795a307add6de4c80be02382cfb2e213d923afb170c0918/68747470733a2f2f7472617669732d63692e6f72672f6b72797374616c636f64652f7068702d746f67676c652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/krystalcode/php-toggle) [![Code Coverage](https://camo.githubusercontent.com/690e25556eeeaafae3bfa0030be9345f02e90497d64b9297e33e81bac9596bb6/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6b72797374616c636f64652f7068702d746f67676c652f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/krystalcode/php-toggle/?branch=master) [![Dependency Status](https://camo.githubusercontent.com/e472bafa0291411b6c163a33a1ac86584945c025e34bc6d9fb26f461dd9add4b/68747470733a2f2f7777772e76657273696f6e6579652e636f6d2f757365722f70726f6a656374732f3534653065643664323731633933616131323030303163652f62616467652e7376673f7374796c653d666c6174)](https://www.versioneye.com/user/projects/54e0ed6d271c93aa120001ce) [![Latest Stable Version](https://camo.githubusercontent.com/73c6e7bad9495c3b8f4e3c4b91791a45f67c6f81ed9b0f645a2a6a6d398b047f/68747470733a2f2f706f7365722e707567782e6f72672f6b72797374616c636f64652f7068702d746f67676c652f762f737461626c652e737667)](https://packagist.org/packages/krystalcode/php-toggle) [![License](https://camo.githubusercontent.com/261093e3af4db16a2cb17ebbb957eb5dc486264377d5b6d372b63c1f2c831e54/68747470733a2f2f706f7365722e707567782e6f72672f6b72797374616c636f64652f7068702d746f67676c652f6c6963656e73652e737667)](https://packagist.org/packages/krystalcode/php-toggle)

Toggle
======

[](#toggle)

About
-----

[](#about)

Toggle is an easy to use, extensible library that aims to provide feature toggle functionality for PHP applications.

Model
-----

[](#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
------------

[](#requirements)

Currently depends on the Symfony Yaml component ().

How to use
----------

[](#how-to-use)

Provided Toggles

### YAML file

[](#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:

```
awesomefeature/dev: true
awesomefeature/stage: true
awesomefeature/prod: false

```

The following code will enable the feature on the development and stage environments and disable it on the production environment.

```
use KrystalCode\Toggle\Toggle;

if (Toggle::yaml('absolute/path/to/config.yml', 'awesomefeature/'.$yourCurrentEnvironment)) {
    // Code to be executed when the feature is enabled.
}

```

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:

```
theme: blue

```

The following code will enable the feature when the value of the variable "theme" is "blue".

```
use KrystalCode\Toggle\Toggle;

if (Toggle::yaml('absolute/path/to/config.yml', 'theme', 'blue')) {
    // Code to be executed when the feature is enabled.
}

```

### PHP file

[](#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:

```
