mileschou/toggle

The feature toggle library for PHP
3,608 6
Install
composer require mileschou/toggle
Latest Version:v2.0.0
PHP:^7.4 | ^8.0
License:MIT
Last Updated:Jun 18, 2025
Links: GitHub  ·  Packagist
Maintainer: MilesChou

Toggle

tests codecov Codacy Badge Latest Stable Version Total Downloads License

The feature toggle library for PHP

Concept

Coming soon...

Usage

The Toggle class is the core class. All feature config will set on this object.

Feature Toggle

Use the fixed result:

<?php

use MilesChou\Toggle\Toggle;

$toggle = new Toggle();
$toggle->create('f1', true);

// Will return true
$toggle->isActive('f1');

Use the object with fixed result:

<?php

use MilesChou\Toggle\Feature;
use MilesChou\Toggle\Toggle;

$toggle = new Toggle();
$toggle->add('f1', Feature::create(true));

// Will return true
$toggle->isActive('f1');

Use callable to decide the return dynamically:

<?php

use MilesChou\Toggle\Toggle;

$toggle = new Toggle();
$toggle->create('f1', function() {
    return true;
});

// Will return true
$toggle->isActive('f1');

Use callable with Context:

<?php

use MilesChou\Toggle\Toggle;

$toggle = new Toggle();
$toggle->create('f1', function($context) {
    return $context['return'];
});

// Will return true
$toggle->isActive('f1', ['return' => true]);

Parameters

Feature instance can store some parameter. For example:

<?php

use MilesChou\Toggle\Toggle;

$toggle = new Toggle();

$toggle->create('f1', true, ['name' => 'Miles']);
$toggle->create('f2', false, ['name' => 'Chou']);

// Will return 'Chou'
$toggle->feature('f1')->params('name');

// Also using in callback
$toggle->create('f3', function($context, array $params) {
    return $params['key'] === $context['key'];
}, ['key' => 'foo']);

Control Structure

This snippet is like if / switch structure:

<?php

use MilesChou\Toggle\Toggle;

$toggle = new Toggle();
$toggle->create('f1');
$toggle->create('f2');
$toggle->create('f3');

$toggle
    ->when('f1', function ($context, $params) {
        // Something when f1 is on
    })
    ->when('f2', function ($context, $params) {
        // Something when f2 is on
    })
    ->when('f3', function ($context, $params) {
        // Something when f3 is on
    });

Processors

Refer the Toggle Processor Project.

Related Packages

jlis/judge

Judge handles feature and value toggles/flaggings for Laravel.

1,037 2
toggle/goutte

A Laravel 5 service provider for Goutte - Made by artisan for artisans.

366 6
lucadegasperi/laravel-feature-context

A Laravel 4 Feature Context for Behat

88 1
francescomalatesta/laravel-feature

A simple package to manage feature flagging in a Laravel project.

207,017 210