PHP code example of solidworx / toggler

1. Go to this page and download the library: Download solidworx/toggler library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

solidworx / toggler example snippets




use SolidWorx\Toggler\Toggle;
use SolidWorx\Toggler\Storage\ArrayStorage;

$features = [
    'foo' => true,
    'bar' => false
];

$toggle = new Toggle(new ArrayStorage($features));



$toggle->isActive('foo'); // true
$toggle->isActive('bar'); // false



use SolidWorx\Toggler\Storage\StorageFactory;

$features = [
    'foo' => true,
    'bar' => false
];

$config = StorageFactory::factory($features); // $config will be an instance of ArrayStorage

// Using YAML
$config = StorageFactory::factory('/path/to/config.yml'); // $config will be an instance of YamlFileStorage



$features = [
    'foo' => function () {
        return true;
    },
    'bar' => [$myObject, 'checkBar']
];



use SolidWorx\Toggler\Storage\StorageFactory;
use SolidWorx\Toggler\Storage\ArrayStorage;
use SolidWorx\Toggler\Toggle;

$features = [
    'foo' => true,
    'bar' => false
];

$toggle = new Toggle(new ArrayStorage($features));

// Or using the StorageFactory factory
$toggle = new Toggle(StorageFactory::factory($features));



use SolidWorx\Toggler\Storage\EnvStorage;
use SolidWorx\Toggler\Toggle;

$toggle = new Toggle(new EnvStorage());

$toggle->isActive('MY_AWESOME_FEATURE'); // true if the environment variable MY_AWESOME_FEATURE is set to a truthy value

// config.yml
foo: true
bar: false



use SolidWorx\Toggler\Storage\StorageFactory;
use SolidWorx\Toggler\Storage\YamlFileStorage;
use SolidWorx\Toggler\Toggle;

$toggle = new Toggle(new YamlFileStorage('/path/to/config.yml'));

// Or using the StorageFactory factory
$toggle = new Toggle(StorageFactory::factory('/path/to/config.yml'));



// config.php
return[
    'foo' => true,
    'bar' => false,
];



use SolidWorx\Toggler\Storage\StorageFactory;
use SolidWorx\Toggler\Toggle;

$toggle = new Toggle(StorageFactory::factory('/path/to/config.php'));



use SolidWorx\Toggler\Storage\RedisStorage;
use SolidWorx\Toggler\Toggle;

$redis = new \Redis();
$toggle = new Toggle(new RedisStorage($redis));



use SolidWorx\Toggler\Storage\PDOStorage;
use SolidWorx\Toggler\Toggle;

$toggle = new Toggle(new PDOStorage('mysql:host=localhost', 'username', 'password', 'my_feature_table_name'));



$toggle->set('foo', true); // This will enable the foo feature
$toggle->set('bar', false); // This will disable the bar feature



$features = [
    'foo' => function (User $user) {
        return in_array('admin', $user->getGroups()); // Only enable features for users in the 'admin' group
    },
    'bar' => function () {
        return  (crc32($_SERVER['REMOTE_ADDR']) % 100) < 25; // Only enable this features for about 25% of visitors
    },
    'baz' => function (Request $request) {
        return false !== strpos($request->headers->get('referer'), 'facebook.com'); // Only enable this features for users that come from Facebook
    }
];



$user = User::find(); // Get the current logged-in user

if ($toggle->isActive('foo', [$user])) {
    
}

if ($toggle->isActive('bar', [$request])) {
    
}



use Symfony\Component\ExpressionLanguage\Expression;

$feaures = [
    'foo' => new Expression('valueOne > 10 and valueTwo < 10')
];



if ($toggle->isActive('foo', ['valueOne' => 25, 'valueTwo' => 5])) { // Will return true
    
}



use SolidWorx\Toggler\Twig\Extension\ToggleExtension;

$twig = new \Twig_Environment($loader);
$twig->addExtension(new ToggleExtension($toggle));

// config/bundles.php

return array(
   ...
   SolidWorx\Toggler\Symfony\TogglerBundle::class => ['all' => true],
   ...
);
bash
$ php bin/console toggler:get foo
bash
$ php bin/console toggler:get foo bar baz
bash
$ php bin/console toggler:set foo true
bash
$ php bin/console toggler:list