From 3feec60e87768c93966a12635d0bd8232145fcb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Fri, 29 Dec 2017 18:26:27 +0100 Subject: [PATCH 1/2] [DI] Add a simple array env var processor --- .../DependencyInjection/EnvVarProcessor.php | 5 + .../RegisterEnvVarProcessorsPassTest.php | 1 + .../Tests/Dumper/PhpDumperTest.php | 17 +++ .../Tests/Fixtures/php/services_csv_env.php | 131 ++++++++++++++++++ 4 files changed, 154 insertions(+) create mode 100644 src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_csv_env.php diff --git a/src/Symfony/Component/DependencyInjection/EnvVarProcessor.php b/src/Symfony/Component/DependencyInjection/EnvVarProcessor.php index bf34c593d638a..76f9348e1deab 100644 --- a/src/Symfony/Component/DependencyInjection/EnvVarProcessor.php +++ b/src/Symfony/Component/DependencyInjection/EnvVarProcessor.php @@ -36,6 +36,7 @@ public static function getProvidedTypes() 'base64' => 'string', 'bool' => 'bool', 'const' => 'bool|int|float|string|array', + 'csv' => 'array', 'file' => 'string', 'float' => 'float', 'int' => 'int', @@ -149,6 +150,10 @@ public function getEnv($prefix, $name, \Closure $getEnv) }, $env); } + if ('csv' === $prefix) { + return str_getcsv($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 cddb62dce9826..e330017bcd8e8 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Compiler/RegisterEnvVarProcessorsPassTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/RegisterEnvVarProcessorsPassTest.php @@ -33,6 +33,7 @@ public function testSimpleProcessor() 'base64' => array('string'), 'bool' => array('bool'), 'const' => array('bool', 'int', 'float', 'string', 'array'), + 'csv' => array('array'), 'file' => array('string'), 'float' => array('float'), 'int' => array('int'), diff --git a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php index 2025cbe943764..adf306310fb6e 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php @@ -371,6 +371,23 @@ public function testDumpedBase64EnvParameters() $this->assertSame('world', $container->getParameter('hello')); } + public function testDumpedCsvEnvParameters() + { + $container = new ContainerBuilder(); + $container->setParameter('env(foo)', 'foo,bar'); + $container->setParameter('hello', '%env(csv:foo)%'); + $container->compile(); + + $dumper = new PhpDumper($container); + $dumper->dump(); + + $this->assertStringEqualsFile(self::$fixturesPath.'/php/services_csv_env.php', $dumper->dump(array('class' => 'Symfony_DI_PhpDumper_Test_CsvParameters'))); + + require self::$fixturesPath.'/php/services_csv_env.php'; + $container = new \Symfony_DI_PhpDumper_Test_CsvParameters(); + $this->assertSame(array('foo', 'bar'), $container->getParameter('hello')); + } + public function testCustomEnvParameters() { $container = new ContainerBuilder(); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_csv_env.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_csv_env.php new file mode 100644 index 0000000000000..65cf4f7fdf984 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_csv_env.php @@ -0,0 +1,131 @@ +parameters = $this->getDefaultParameters(); + + $this->services = $this->privates = array(); + + $this->aliases = array(); + } + + public function reset() + { + $this->privates = array(); + parent::reset(); + } + + public function compile() + { + throw new LogicException('You cannot compile a dumped container that was already compiled.'); + } + + public function isCompiled() + { + return true; + } + + public function getRemovedIds() + { + return array( + 'Psr\\Container\\ContainerInterface' => true, + 'Symfony\\Component\\DependencyInjection\\ContainerInterface' => true, + ); + } + + public function getParameter($name) + { + $name = (string) $name; + + if (!(isset($this->parameters[$name]) || isset($this->loadedDynamicParameters[$name]) || array_key_exists($name, $this->parameters))) { + throw new InvalidArgumentException(sprintf('The parameter "%s" must be defined.', $name)); + } + if (isset($this->loadedDynamicParameters[$name])) { + return $this->loadedDynamicParameters[$name] ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name); + } + + return $this->parameters[$name]; + } + + public function hasParameter($name) + { + $name = (string) $name; + + return isset($this->parameters[$name]) || isset($this->loadedDynamicParameters[$name]) || array_key_exists($name, $this->parameters); + } + + public function setParameter($name, $value) + { + throw new LogicException('Impossible to call set() on a frozen ParameterBag.'); + } + + public function getParameterBag() + { + if (null === $this->parameterBag) { + $parameters = $this->parameters; + foreach ($this->loadedDynamicParameters as $name => $loaded) { + $parameters[$name] = $loaded ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name); + } + $this->parameterBag = new FrozenParameterBag($parameters); + } + + return $this->parameterBag; + } + + private $loadedDynamicParameters = array( + 'hello' => false, + ); + private $dynamicParameters = array(); + + /** + * Computes a dynamic parameter. + * + * @param string The name of the dynamic parameter to load + * + * @return mixed The value of the dynamic parameter + * + * @throws InvalidArgumentException When the dynamic parameter does not exist + */ + private function getDynamicParameter($name) + { + switch ($name) { + case 'hello': $value = $this->getEnv('csv:foo'); break; + default: throw new InvalidArgumentException(sprintf('The dynamic parameter "%s" must be defined.', $name)); + } + $this->loadedDynamicParameters[$name] = true; + + return $this->dynamicParameters[$name] = $value; + } + + /** + * Gets the default parameters. + * + * @return array An array of the default parameters + */ + protected function getDefaultParameters() + { + return array( + 'env(foo)' => 'foo,bar', + ); + } +} From b82b53ab183288e67e4d274915f86dbb4578d259 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Fri, 29 Dec 2017 21:52:54 +0100 Subject: [PATCH 2/2] Try to fix tests --- .../Tests/Fixtures/php/services_csv_env.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_csv_env.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_csv_env.php index 65cf4f7fdf984..99215f5fd685b 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_csv_env.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_csv_env.php @@ -18,7 +18,11 @@ class Symfony_DI_PhpDumper_Test_CsvParameters extends Container { private $parameters; private $targetDirs = array(); - private $privates = array(); + + /** + * @internal but protected for BC on cache:clear + */ + protected $privates = array(); public function __construct() {