PHP code example of michalzimka / feature-toggle

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

    

michalzimka / feature-toggle example snippets


#[ORM\Column(length: 255, unique: true)]
private string $name;

#[ORM\Column(type: 'boolean')]
private bool $active;



namespace App\Repository;

use Doctrine\ORM\EntityManagerInterface;
use FeatureToggle\FeatureToggle;
use FeatureToggle\Repository\FeatureToggleRepositoryInterface;
use App\Entity\FeatureToggle as FeatureToggleEntity;

class DatabaseFeatureToggleRepository implements FeatureToggleRepositoryInterface
{
    public function __construct(
        private EntityManagerInterface $entityManager,
    ) {}

    public function findByName(string $name): ?FeatureToggle
    {
        $entity = $this->entityManager
            ->getRepository(FeatureToggleEntity::class)
            ->findOneBy(['name' => $name]);

        if (!$entity) {
            return null;
        }

        return new FeatureToggle()
            ->setName($entity->getName())
            ->setActive($entity->isActive());
    }

    public function findAll(): array
    {
        $entities = $this->entityManager->getRepository(FeatureToggleEntity::class)->findAll();
        $toggles = [];

        foreach ($entities as $entity) {
            $toggles[] = new FeatureToggle()
                ->setName($entity->getName())
                ->setActive($entity->isActive());
        }

        return $toggles;
    }
}

use FeatureToggle\FeatureManager;

$featureManager = new FeatureManager($yourRepository);

if ($featureManager->isActive('new_awesome_feature')) {
    // Awesome feature is active
}
bash
   php bin/console toggle:list [--active=<value>]
bash
   php bin/console toggle:list
   
bash
   php bin/console toggle:list --active=true
   
bash
   php bin/console toggle:list --active=false