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..76a7501 --- /dev/null +++ b/src/DependencyInjection/UnleashEnvVarProcessor.php @@ -0,0 +1,34 @@ +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..1dc382d 100644 --- a/src/Resources/config/services.yaml +++ b/src/Resources/config/services.yaml @@ -160,3 +160,10 @@ 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' + tags: + - container.env_var_processor