From 269a18e95b8eb3960d9f9495c3ed9ad46c7e456d Mon Sep 17 00:00:00 2001 From: Youssef BENHSSAIEN Date: Fri, 1 Mar 2024 18:09:26 +0100 Subject: [PATCH 1/4] feat: Unleash as env variable processor --- README.md | 35 ++++++++++++++++++ composer.json | 1 + .../UnleashEnvVarProcessor.php | 36 +++++++++++++++++++ src/Resources/config/services.yaml | 5 +++ src/UnleashClientBundle.php | 3 ++ 5 files changed, 80 insertions(+) create mode 100644 src/DependencyInjection/UnleashEnvVarProcessor.php diff --git a/README.md b/README.md index 519f497..1ad0d1f 100644 --- a/README.md +++ b/README.md @@ -341,6 +341,41 @@ have access to implicit `variant` variable. {% endfeature %} ``` +## Environment variable + +You can use it within a yaml/xml/php configuration as an environment variable and combine it with others [Environment Variable Processors](https://symfony.com/doc/current/configuration/env_var_processors.html) + +### YAML +```yaml +# config/services.yaml +parameters: + # The next line in case you need a default value + %env(unleash:feature_name)%: false + feature_toggle: %env(unleash:feature_name)% +``` +### XML +```xml + + + + + %env(unleash:feature_name)% + + + +``` +### PHP +```php +// config/services.php +$container->setParameter('feature_toggle', '%env(unleash:feature_name)%'); +``` + ## Custom strategies Defining custom strategies is very easy because they get automatically injected, you just need to create a class diff --git a/composer.json b/composer.json index 1701793..bd60ab0 100644 --- a/composer.json +++ b/composer.json @@ -7,6 +7,7 @@ "symfony/event-dispatcher": "^5.0 | ^6.0 | ^7.0", "symfony/http-client": "^5.0 | ^6.0 | ^7.0", "symfony/cache": "^5.0 | ^6.0 | ^7.0", + "symfony/dependency-injection": "^5.0 | ^6.0 | ^7.0", "nyholm/psr7": "^1.0", "unleash/client": "^1.6 | ^2.0", "php": "^8.2" diff --git a/src/DependencyInjection/UnleashEnvVarProcessor.php b/src/DependencyInjection/UnleashEnvVarProcessor.php new file mode 100644 index 0000000..27f0472 --- /dev/null +++ b/src/DependencyInjection/UnleashEnvVarProcessor.php @@ -0,0 +1,36 @@ +client = $client; + } + + public function getEnv(string $prefix, string $name, \Closure $getEnv): bool + { + $value = $this->client->isEnabled($name); + + // Env vars declared from yaml/xml files have string type + // 1 : Use unleash value first + // 2 : Retrieve the value of declared/default env var of unleash value is false or not retrieved + return $value || filter_var($getEnv($name), FILTER_VALIDATE_BOOL); + } + + /** + * @return string[] + */ + public static function getProvidedTypes(): array + { + return [ + 'unleash' => 'string', + ]; + } +} diff --git a/src/Resources/config/services.yaml b/src/Resources/config/services.yaml index 3fa3e0e..56199c6 100644 --- a/src/Resources/config/services.yaml +++ b/src/Resources/config/services.yaml @@ -160,3 +160,8 @@ services: $cache: '@unleash.client.internal.cache' tags: - console.command + + unleash.client.env_var_processor: + class: Unleash\Client\Bundle\DependencyInjection\UnleashEnvVarProcessor + arguments: + $client: '@unleash.client.unleash' diff --git a/src/UnleashClientBundle.php b/src/UnleashClientBundle.php index 0707a6e..fdf1d02 100644 --- a/src/UnleashClientBundle.php +++ b/src/UnleashClientBundle.php @@ -9,6 +9,7 @@ use Unleash\Client\Bundle\DependencyInjection\Compiler\BootstrapResolver; use Unleash\Client\Bundle\DependencyInjection\Compiler\CacheServiceResolverCompilerPass; use Unleash\Client\Bundle\DependencyInjection\Compiler\HttpServicesResolverCompilerPass; +use Unleash\Client\Bundle\DependencyInjection\UnleashEnvVarProcessor; use Unleash\Client\Strategy\StrategyHandler; final class UnleashClientBundle extends Bundle @@ -19,6 +20,8 @@ public function build(ContainerBuilder $container): void ->addTag('unleash.client.strategy_handler'); $container->registerForAutoconfiguration(BootstrapProvider::class) ->addTag('unleash.client.bootstrap_provider'); + $container->registerForAutoconfiguration(UnleashEnvVarProcessor::class) + ->addTag('container.env_var_processor'); $container->addCompilerPass( new HttpServicesResolverCompilerPass(), From 6dd67f88b20cd1228b97b1ebd216131bc93c2f92 Mon Sep 17 00:00:00 2001 From: Youssef Benhssaien Date: Thu, 7 Mar 2024 17:27:29 +0100 Subject: [PATCH 2/4] refactor: Constructor PHP8 style Co-authored-by: Rikudou_Sage --- src/DependencyInjection/UnleashEnvVarProcessor.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/DependencyInjection/UnleashEnvVarProcessor.php b/src/DependencyInjection/UnleashEnvVarProcessor.php index 27f0472..5c5b78a 100644 --- a/src/DependencyInjection/UnleashEnvVarProcessor.php +++ b/src/DependencyInjection/UnleashEnvVarProcessor.php @@ -7,11 +7,8 @@ final class UnleashEnvVarProcessor implements EnvVarProcessorInterface { - private Unleash $client; - - public function __construct(Unleash $client) + public function __construct(private Unleash $client) { - $this->client = $client; } public function getEnv(string $prefix, string $name, \Closure $getEnv): bool From 46b51d5dcf69b1546ead5a39a172ea11be126e2f Mon Sep 17 00:00:00 2001 From: Youssef Benhssaien Date: Thu, 7 Mar 2024 17:28:01 +0100 Subject: [PATCH 3/4] refactor: env_var_processor tag Co-authored-by: Rikudou_Sage --- src/Resources/config/services.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Resources/config/services.yaml b/src/Resources/config/services.yaml index 56199c6..1dc382d 100644 --- a/src/Resources/config/services.yaml +++ b/src/Resources/config/services.yaml @@ -165,3 +165,5 @@ services: class: Unleash\Client\Bundle\DependencyInjection\UnleashEnvVarProcessor arguments: $client: '@unleash.client.unleash' + tags: + - container.env_var_processor From 6c2356922ce838c735f24d80c389aa34fe6ed527 Mon Sep 17 00:00:00 2001 From: Youssef BENHSSAIEN Date: Sun, 10 Mar 2024 14:46:05 +0100 Subject: [PATCH 4/4] refactor: remove tag from ClientBundle --- src/DependencyInjection/UnleashEnvVarProcessor.php | 3 ++- src/UnleashClientBundle.php | 3 --- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/DependencyInjection/UnleashEnvVarProcessor.php b/src/DependencyInjection/UnleashEnvVarProcessor.php index 5c5b78a..76a7501 100644 --- a/src/DependencyInjection/UnleashEnvVarProcessor.php +++ b/src/DependencyInjection/UnleashEnvVarProcessor.php @@ -4,6 +4,7 @@ use Symfony\Component\DependencyInjection\EnvVarProcessorInterface; use Unleash\Client\Unleash; +use Closure; final class UnleashEnvVarProcessor implements EnvVarProcessorInterface { @@ -11,7 +12,7 @@ public function __construct(private Unleash $client) { } - public function getEnv(string $prefix, string $name, \Closure $getEnv): bool + public function getEnv(string $prefix, string $name, Closure $getEnv): bool { $value = $this->client->isEnabled($name); diff --git a/src/UnleashClientBundle.php b/src/UnleashClientBundle.php index fdf1d02..0707a6e 100644 --- a/src/UnleashClientBundle.php +++ b/src/UnleashClientBundle.php @@ -9,7 +9,6 @@ use Unleash\Client\Bundle\DependencyInjection\Compiler\BootstrapResolver; use Unleash\Client\Bundle\DependencyInjection\Compiler\CacheServiceResolverCompilerPass; use Unleash\Client\Bundle\DependencyInjection\Compiler\HttpServicesResolverCompilerPass; -use Unleash\Client\Bundle\DependencyInjection\UnleashEnvVarProcessor; use Unleash\Client\Strategy\StrategyHandler; final class UnleashClientBundle extends Bundle @@ -20,8 +19,6 @@ public function build(ContainerBuilder $container): void ->addTag('unleash.client.strategy_handler'); $container->registerForAutoconfiguration(BootstrapProvider::class) ->addTag('unleash.client.bootstrap_provider'); - $container->registerForAutoconfiguration(UnleashEnvVarProcessor::class) - ->addTag('container.env_var_processor'); $container->addCompilerPass( new HttpServicesResolverCompilerPass(),