From 475d11b6690e62cb356a447a3b97fc2451315094 Mon Sep 17 00:00:00 2001 From: Florent Blaison Date: Wed, 5 Jun 2019 12:55:06 +0200 Subject: [PATCH] [DIC] Add a split env var processor --- .../Component/DependencyInjection/CHANGELOG.md | 1 + .../DependencyInjection/EnvVarProcessor.php | 5 +++++ .../Compiler/RegisterEnvVarProcessorsPassTest.php | 1 + .../Tests/EnvVarProcessorTest.php | 13 +++++++++++++ 4 files changed, 20 insertions(+) diff --git a/src/Symfony/Component/DependencyInjection/CHANGELOG.md b/src/Symfony/Component/DependencyInjection/CHANGELOG.md index 7d30dbc646383..6d711dfbaceaa 100644 --- a/src/Symfony/Component/DependencyInjection/CHANGELOG.md +++ b/src/Symfony/Component/DependencyInjection/CHANGELOG.md @@ -6,6 +6,7 @@ CHANGELOG * deprecated support for short factories and short configurators in Yaml * deprecated `tagged` in favor of `tagged_iterator` + * added `%env(split:...)%` processor to `split` a string into an array 4.3.0 ----- diff --git a/src/Symfony/Component/DependencyInjection/EnvVarProcessor.php b/src/Symfony/Component/DependencyInjection/EnvVarProcessor.php index 568c91d4f69bd..d1644966a3654 100644 --- a/src/Symfony/Component/DependencyInjection/EnvVarProcessor.php +++ b/src/Symfony/Component/DependencyInjection/EnvVarProcessor.php @@ -48,6 +48,7 @@ public static function getProvidedTypes() 'string' => 'string', 'trim' => 'string', 'require' => 'bool|int|float|string|array', + 'split' => 'array', ]; } @@ -243,6 +244,10 @@ public function getEnv($prefix, $name, \Closure $getEnv) return trim($env); } + if ('split' === $prefix) { + return explode(',', $env); + } + throw new RuntimeException(sprintf('Unsupported env var prefix "%s".', $prefix)); } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/RegisterEnvVarProcessorsPassTest.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/RegisterEnvVarProcessorsPassTest.php index f18607914e60d..a1826acf58e6f 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Compiler/RegisterEnvVarProcessorsPassTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/RegisterEnvVarProcessorsPassTest.php @@ -46,6 +46,7 @@ public function testSimpleProcessor() 'string' => ['string'], 'trim' => ['string'], 'require' => ['bool', 'int', 'float', 'string', 'array'], + 'split' => ['array'], ]; $this->assertSame($expected, $container->getParameterBag()->getProvidedTypes()); diff --git a/src/Symfony/Component/DependencyInjection/Tests/EnvVarProcessorTest.php b/src/Symfony/Component/DependencyInjection/Tests/EnvVarProcessorTest.php index 5aa905d954b2b..1d42e69b7800e 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/EnvVarProcessorTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/EnvVarProcessorTest.php @@ -486,4 +486,17 @@ public function testRequireFile() $this->assertEquals('foo', $result); } + + public function testGetEnvSplit() + { + $processor = new EnvVarProcessor(new Container()); + + $result = $processor->getEnv('split', 'foo', function ($name) { + $this->assertSame('foo', $name); + + return 'one,two,three'; + }); + + $this->assertSame(['one', 'two', 'three'], $result); + } } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy