PHP code example of dorumd / feature-switches

1. Go to this page and download the library: Download dorumd/feature-switches 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/ */

    

dorumd / feature-switches example snippets



// index.php

use Dorumd\FeatureSwitches\Domain\BasicFeatureSwitches;
use Dorumd\FeatureSwitches\Infrastructure\FileFeatureSwitchesStorage;

$configPath = __DIR__ . '/config/feature-switches.yaml';
$featureSwitchesStorage = new FileFeatureSwitchesStorage($configPath);

$featureSwitches = new BasicFeatureSwitches($featureSwitchesStorage);

echo $featureSwitches->featureIsEnabled('SEND_MERCHANT_NOTIFICATIONS');
echo $featureSwitches->featureIsEnabled('SEND_ORDER_EMAILS');

  
  // index.php
  use Dorumd\FeatureSwitches\Domain\BasicFeatureSwitches;
  use Dorumd\FeatureSwitches\Infrastructure\FileFeatureSwitchesStorage;

  $configPath = __DIR__ . '/config/feature-switches.yaml';
  $featureSwitchesStorage = new FileFeatureSwitchesStorage($configPath);
  
  $featureSwitches = new BasicFeatureSwitches($featureSwitchesStorage);
  echo $featureSwitches->featureIsEnabled('SEND_ORDER_EMAILS');
  

  
  // index.php
  use Dorumd\FeatureSwitches\Domain\BasicFeatureSwitches;
  use Dorumd\FeatureSwitches\Domain\BasicFeatureSwitch;
  use Dorumd\FeatureSwitches\Domain\PercentageSplitFeatureSwitch;
  use Dorumd\FeatureSwitches\Infrastructure\InMemoryFeatureSwitchesStorage;

  $featureSwitchesStorage = new InMemoryFeatureSwitchesStorage();
  $featureSwitchesStorage->store(new BasicFeatureSwitch('SEND_ORDER_EMAILS', false));
  $featureSwitchesStorage->store(new BasicFeatureSwitch('SEND_NOTIFICATION', true));
  $featureSwitchesStorage->store(
      new PercentageSplitFeatureSwitch('DISPLAY_SURVEY', true, ['percentage' => 50])
  );
  
  $featureSwitches = new BasicFeatureSwitches($featureSwitchesStorage);
  echo $featureSwitches->featureIsEnabled('SEND_ORDER_EMAILS');
  echo $featureSwitches->featureIsEnabled('DISPLAY_SURVEY');
  

  
  // index.php
  use Dorumd\FeatureSwitches\Domain\BasicFeatureSwitches;
  use Dorumd\FeatureSwitches\Domain\BasicFeatureSwitch;
  use Dorumd\FeatureSwitches\Domain\PercentageSplitFeatureSwitch;
  use Dorumd\FeatureSwitches\Infrastructure\FileFeatureSwitchesStorage;
  use Dorumd\FeatureSwitches\Infrastructure\CompositeStorage;
  use Dorumd\FeatureSwitches\Infrastructure\InMemoryFeatureSwitchesStorage;
  
  $configPath = __DIR__ . '/config/feature-switches.yaml';
  $fileFeatureSwitchesStorage = new FileFeatureSwitchesStorage($configPath);

  $featureSwitchesStorage = new InMemoryFeatureSwitchesStorage();
  $featureSwitchesStorage->store(new BasicFeatureSwitch('SEND_ORDER_EMAILS', false));
  $featureSwitchesStorage->store(new BasicFeatureSwitch('SEND_NOTIFICATION', true));
  $featureSwitchesStorage->store(
      new PercentageSplitFeatureSwitch('DISPLAY_SURVEY', true, ['percentage' => 50])
  );
  
  $featureSwitches = new BasicFeatureSwitches(
      // ordered ASC, last one overrides first one.
      new CompositeStorage([$fileFeatureSwitchesStorage, $featureSwitchesStorage])
  );
  echo $featureSwitches->featureIsEnabled('SEND_ORDER_EMAILS'); // false, in memory storage is overriding file.
  echo $featureSwitches->featureIsEnabled('DISPLAY_SURVEY');