PHP code example of ajgarlag / feature-flag-bundle
1. Go to this page and download the library: Download ajgarlag/feature-flag-bundle 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/ */
namespace App\Feature;
use Ajgarlag\FeatureFlagBundle\Attribute\AsFeature;
#[AsFeature('xmas')]
final class XmasFeature
{
public function __invoke(): bool
{
return date('m-d') === '12-25';
}
}
namespace App\Feature;
use Ajgarlag\FeatureFlagBundle\Attribute\AsFeature;
#[AsFeature('xmas', method: 'isXmas')]
final class XmasFeature
{
public function isXmas(): bool
{
return date('m-d') === '12-25';
}
}
namespace App\Feature;
use Ajgarlag\FeatureFlagBundle\Attribute\AsFeature;
final class FeatureService
{
#[AsFeature('weekend')]
public function isWeekend(): bool
{
return date('N') >= 6;
}
#[AsFeature] // The feature will be named "App\Feature\FeatureService::isAnotherFeature"
public function isAnotherFeature(): bool
{
return true;
}
}
namespace App\Controller;
use Symfony\Component\Routing\Attribute\Route;
class SomeController
{
#[Route('/some/path', condition: "feature_is_enabled('some_feature')")]
public function index()
{
// ...
}
}
namespace App\Controller;
use Symfony\Component\Routing\Attribute\Route;
class SomeController
{
#[Route('/some/path', condition: "feature_get_value('some_feature') == 'some_value'")]
public function index()
{
// ...
}
}
namespace App\FeatureProvider;
use Ajgarlag\FeatureFlagBundle\Provider\ProviderInterface;
class MyProvider implements ProviderInterface
{
public function get(string $featureName): ?callable
{
// ...
}
}
namespace App\FeatureProvider;
use App\Repository\FeatureAssignmentRepository;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
use Symfony\Component\FeatureFlag\Provider\ProviderInterface;
final class DoctrineProvider implements ProviderInterface
{
public function __construct(
private readonly FeatureAssignmentRepository $featureAssignmentRepository,
) {
}
public function get(string $featureName): ?callable
{
// Set context. Example: user identifier, IP, hostname, etc.
$context = [];
return function () use ($featureName) {
return $this->featureAssignmentRepository->featureIsEnabled($featureName, $context);
};
}
}
// config/services.php
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
use Symfony\Component\Cache\Psr16Cache;
use Unleash\Client\Unleash;
use Unleash\Client\UnleashBuilder;
return function(ContainerConfigurator $container): void {
// Application service definition
$services->set('gitlab.client_factory')
->class(UnleashBuilder::class)
->factory([UnleashBuilder::class, 'createForGitlab'])
->call('withGitlabEnvironment', [env('GITLAB_ENVIRONMENT')], true)
->call('withAppUrl', [env('GITLAB_URL')], true)
->call('withInstanceId', [env('GITLAB_INSTANCE_ID')], true)
->call('withHttpClient', [service('psr18.http_client')], true)
// Using a cache is recommended to limit API calls (named "cache.unleash" in this example)
->call('withCacheHandler', [inline_service(Psr16Cache::class)->args([service('cache.unleash')])], true)
;
$services->set('gitlab.client')
->class(Unleash::class)
->factory([service('gitlab.client_factory'), 'build'])
;
};
namespace App\FeatureProvider;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\FeatureFlag\Provider\ProviderInterface;
use Unleash\Client\Configuration\Context;
use Unleash\Client\Configuration\UnleashContext;
use Unleash\Client\Unleash;
class GitlabProvider implements ProviderInterface
{
public function __construct(
#[Autowire(service: 'gitlab.client')] private readonly Unleash $unleash,
private readonly Security $security,
) {
}
public function get(string $featureName): ?callable
{
// Set context. Example: user identifier, IP, hostname, etc.
$context = new UnleashContext(
currentUserId: $this->security->getUser()?->getUserIdentifier()
);
return fn () => $this->unleash->isEnabled($featureName, $context);
}
}
namespace App\FeatureProvider;
use Ajgarlag\FeatureFlagBundle\Provider\ProviderInterface;
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
#[AutoconfigureTag('ajgarlag.feature_flag.provider', ['priority' => 10])]
class MyProvider implements ProviderInterface
{
// ...
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.