From 1e65e6e6a7b29aff443ad706a2275bdd829eed62 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Mon, 31 Jan 2022 18:35:43 +0100 Subject: [PATCH 1/5] [DependencyInjection] Don't dump polyfilled classes in preload script --- Dumper/PhpDumper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dumper/PhpDumper.php b/Dumper/PhpDumper.php index f76daf03a..4bc1d3cb9 100644 --- a/Dumper/PhpDumper.php +++ b/Dumper/PhpDumper.php @@ -316,7 +316,7 @@ public function dump(array $options = []) if (!$class || str_contains($class, '$')) { continue; } - if (!(class_exists($class, false) || interface_exists($class, false) || trait_exists($class, false)) || (new \ReflectionClass($class))->isUserDefined()) { + if (!(class_exists($class, false) || interface_exists($class, false) || trait_exists($class, false)) || ((new \ReflectionClass($class))->isUserDefined() && !\in_array($class, ['Attribute', 'JsonException', 'ReturnTypeWillChange', 'Stringable', 'UnhandledMatchError', 'ValueError'], true))) { $code[$options['class'].'.preload.php'] .= sprintf("\$classes[] = '%s';\n", $class); } } From 8d76a7063956e539fed02e3db2a0a5d95f415903 Mon Sep 17 00:00:00 2001 From: Maxime Steinhausser Date: Mon, 31 Jan 2022 17:33:23 +0100 Subject: [PATCH 2/5] [DependencyInjection][FrameworkBundle] Fix using PHP 8.1 enum as parameters --- Container.php | 6 ++--- ContainerInterface.php | 6 ++--- Dumper/PhpDumper.php | 14 +++++++---- ParameterBag/ContainerBag.php | 2 +- ParameterBag/ParameterBagInterface.php | 6 ++--- Tests/Dumper/PhpDumperTest.php | 24 ++++++++++++++++++- Tests/Dumper/XmlDumperTest.php | 3 +++ Tests/Dumper/YamlDumperTest.php | 3 +++ Tests/Fixtures/FooUnitEnum.php | 1 + Tests/Fixtures/php/services10.php | 2 +- Tests/Fixtures/php/services12.php | 2 +- Tests/Fixtures/php/services19.php | 2 +- Tests/Fixtures/php/services26.php | 2 +- Tests/Fixtures/php/services8.php | 2 +- Tests/Fixtures/php/services9_as_files.txt | 2 +- Tests/Fixtures/php/services9_compiled.php | 2 +- .../php/services9_inlined_factories.txt | 2 +- .../php/services9_lazy_inlined_factories.txt | 2 +- Tests/Fixtures/php/services_array_params.php | 2 +- Tests/Fixtures/php/services_base64_env.php | 2 +- Tests/Fixtures/php/services_csv_env.php | 2 +- Tests/Fixtures/php/services_default_env.php | 2 +- Tests/Fixtures/php/services_env_in_id.php | 2 +- .../php/services_errored_definition.php | 2 +- .../Fixtures/php/services_inline_requires.php | 2 +- Tests/Fixtures/php/services_json_env.php | 2 +- .../php/services_query_string_env.php | 2 +- Tests/Fixtures/php/services_rot13_env.php | 2 +- .../php/services_unsupported_characters.php | 2 +- Tests/Fixtures/php/services_url_env.php | 2 +- .../xml/services_with_enumeration.xml | 7 ++++++ .../yaml/services_with_enumeration.yml | 3 +++ composer.json | 4 ++-- 33 files changed, 82 insertions(+), 39 deletions(-) diff --git a/Container.php b/Container.php index 789c8f727..24a90fbd9 100644 --- a/Container.php +++ b/Container.php @@ -109,7 +109,7 @@ public function getParameterBag() * * @param string $name The parameter name * - * @return array|bool|string|int|float|null + * @return array|bool|string|int|float|\UnitEnum|null * * @throws InvalidArgumentException if the parameter is not defined */ @@ -133,8 +133,8 @@ public function hasParameter($name) /** * Sets a parameter. * - * @param string $name The parameter name - * @param array|bool|string|int|float|null $value The parameter value + * @param string $name The parameter name + * @param array|bool|string|int|float|\UnitEnum|null $value The parameter value */ public function setParameter($name, $value) { diff --git a/ContainerInterface.php b/ContainerInterface.php index a3acbbde2..e47e7f9fb 100644 --- a/ContainerInterface.php +++ b/ContainerInterface.php @@ -74,7 +74,7 @@ public function initialized($id); * * @param string $name The parameter name * - * @return array|bool|string|int|float|null + * @return array|bool|string|int|float|\UnitEnum|null * * @throws InvalidArgumentException if the parameter is not defined */ @@ -92,8 +92,8 @@ public function hasParameter($name); /** * Sets a parameter. * - * @param string $name The parameter name - * @param array|bool|string|int|float|null $value The parameter value + * @param string $name The parameter name + * @param array|bool|string|int|float|\UnitEnum|null $value The parameter value */ public function setParameter($name, $value); } diff --git a/Dumper/PhpDumper.php b/Dumper/PhpDumper.php index 4bc1d3cb9..e2abcc2b9 100644 --- a/Dumper/PhpDumper.php +++ b/Dumper/PhpDumper.php @@ -1436,10 +1436,11 @@ private function addDefaultParametersMethod(): string if ($key !== $resolvedKey = $this->container->resolveEnvPlaceholders($key)) { throw new InvalidArgumentException(sprintf('Parameter name cannot use env parameters: "%s".', $resolvedKey)); } - $export = $this->exportParameters([$value]); + $hasEnum = false; + $export = $this->exportParameters([$value], '', 12, $hasEnum); $export = explode('0 => ', substr(rtrim($export, " ]\n"), 2, -1), 2); - if (preg_match("/\\\$this->(?:getEnv\('(?:[-.\w]*+:)*+\w++'\)|targetDir\.'')/", $export[1])) { + if ($hasEnum || preg_match("/\\\$this->(?:getEnv\('(?:[-.\w]*+:)*+\w++'\)|targetDir\.'')/", $export[1])) { $dynamicPhp[$key] = sprintf('%scase %s: $value = %s; break;', $export[0], $this->export($key), $export[1]); } else { $php[] = sprintf('%s%s => %s,', $export[0], $this->export($key), $export[1]); @@ -1450,7 +1451,7 @@ private function addDefaultParametersMethod(): string $code = <<<'EOF' /** - * @return array|bool|float|int|string|null + * @return array|bool|float|int|string|\UnitEnum|null */ public function getParameter($name) { @@ -1545,12 +1546,12 @@ protected function getDefaultParameters(): array /** * @throws InvalidArgumentException */ - private function exportParameters(array $parameters, string $path = '', int $indent = 12): string + private function exportParameters(array $parameters, string $path = '', int $indent = 12, bool &$hasEnum = false): string { $php = []; foreach ($parameters as $key => $value) { if (\is_array($value)) { - $value = $this->exportParameters($value, $path.'/'.$key, $indent + 4); + $value = $this->exportParameters($value, $path.'/'.$key, $indent + 4, $hasEnum); } elseif ($value instanceof ArgumentInterface) { throw new InvalidArgumentException(sprintf('You cannot dump a container with parameters that contain special arguments. "%s" found in "%s".', \get_class($value), $path.'/'.$key)); } elseif ($value instanceof Variable) { @@ -1561,6 +1562,9 @@ private function exportParameters(array $parameters, string $path = '', int $ind throw new InvalidArgumentException(sprintf('You cannot dump a container with parameters that contain references to other services (reference to service "%s" found in "%s").', $value, $path.'/'.$key)); } elseif ($value instanceof Expression) { throw new InvalidArgumentException(sprintf('You cannot dump a container with parameters that contain expressions. Expression "%s" found in "%s".', $value, $path.'/'.$key)); + } elseif ($value instanceof \UnitEnum) { + $hasEnum = true; + $value = sprintf('\%s::%s', \get_class($value), $value->name); } else { $value = $this->export($value); } diff --git a/ParameterBag/ContainerBag.php b/ParameterBag/ContainerBag.php index 724a94e6d..966122765 100644 --- a/ParameterBag/ContainerBag.php +++ b/ParameterBag/ContainerBag.php @@ -36,7 +36,7 @@ public function all() /** * {@inheritdoc} * - * @return array|bool|string|int|float|null + * @return array|bool|string|int|float|\UnitEnum|null */ public function get($name) { diff --git a/ParameterBag/ParameterBagInterface.php b/ParameterBag/ParameterBagInterface.php index eb033bf4f..cbba67cd3 100644 --- a/ParameterBag/ParameterBagInterface.php +++ b/ParameterBag/ParameterBagInterface.php @@ -47,7 +47,7 @@ public function all(); * * @param string $name The parameter name * - * @return array|bool|string|int|float|null + * @return array|bool|string|int|float|\UnitEnum|null * * @throws ParameterNotFoundException if the parameter is not defined */ @@ -63,8 +63,8 @@ public function remove($name); /** * Sets a service container parameter. * - * @param string $name The parameter name - * @param array|bool|string|int|float|null $value The parameter value + * @param string $name The parameter name + * @param array|bool|string|int|float|\UnitEnum|null $value The parameter value * * @throws LogicException if the parameter can not be set */ diff --git a/Tests/Dumper/PhpDumperTest.php b/Tests/Dumper/PhpDumperTest.php index 61040d325..d5b0056ef 100644 --- a/Tests/Dumper/PhpDumperTest.php +++ b/Tests/Dumper/PhpDumperTest.php @@ -22,6 +22,7 @@ use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\Compiler\PassConfig; +use Symfony\Component\DependencyInjection\Container; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\ContainerInterface as SymfonyContainerInterface; use Symfony\Component\DependencyInjection\Definition; @@ -1217,16 +1218,37 @@ public function testDumpHandlesEnumeration() ->setPublic(true) ->addArgument(FooUnitEnum::BAR); + $container->setParameter('unit_enum', FooUnitEnum::BAR); + $container->setParameter('enum_array', [FooUnitEnum::BAR, FooUnitEnum::FOO]); $container->compile(); $dumper = new PhpDumper($container); - eval('?>'.$dumper->dump([ + eval('?>'.$dumpedContainer = $dumper->dump([ 'class' => 'Symfony_DI_PhpDumper_Test_Enumeration', ])); + /** @var Container $container */ $container = new \Symfony_DI_PhpDumper_Test_Enumeration(); $this->assertSame(FooUnitEnum::BAR, $container->get('foo')->getBar()); + $this->assertSame(FooUnitEnum::BAR, $container->getParameter('unit_enum')); + $this->assertSame([FooUnitEnum::BAR, FooUnitEnum::FOO], $container->getParameter('enum_array')); + $this->assertStringMatchesFormat(<<<'PHP' +%A + private function getDynamicParameter(string $name) + { + switch ($name) { + case 'unit_enum': $value = \Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum::BAR; break; + case 'enum_array': $value = [ + 0 => \Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum::BAR, + 1 => \Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum::FOO, + ]; break; + default: throw new InvalidArgumentException(sprintf('The dynamic parameter "%s" must be defined.', $name)); + } +%A +PHP + , $dumpedContainer + ); } public function testUninitializedSyntheticReference() diff --git a/Tests/Dumper/XmlDumperTest.php b/Tests/Dumper/XmlDumperTest.php index 18caa150f..34878972a 100644 --- a/Tests/Dumper/XmlDumperTest.php +++ b/Tests/Dumper/XmlDumperTest.php @@ -263,6 +263,9 @@ public function testDumpHandlesEnumeration() ->setPublic(true) ->addArgument(FooUnitEnum::BAR); + $container->setParameter('unit_enum', FooUnitEnum::BAR); + $container->setParameter('enum_array', [FooUnitEnum::BAR, FooUnitEnum::FOO]); + $container->compile(); $dumper = new XmlDumper($container); diff --git a/Tests/Dumper/YamlDumperTest.php b/Tests/Dumper/YamlDumperTest.php index 9a973afe6..4632835ba 100644 --- a/Tests/Dumper/YamlDumperTest.php +++ b/Tests/Dumper/YamlDumperTest.php @@ -142,6 +142,9 @@ public function testDumpHandlesEnumeration() ->setPublic(true) ->addArgument(FooUnitEnum::BAR); + $container->setParameter('unit_enum', FooUnitEnum::BAR); + $container->setParameter('enum_array', [FooUnitEnum::BAR, FooUnitEnum::FOO]); + $container->compile(); $dumper = new YamlDumper($container); diff --git a/Tests/Fixtures/FooUnitEnum.php b/Tests/Fixtures/FooUnitEnum.php index d51cf9c99..e6ce96391 100644 --- a/Tests/Fixtures/FooUnitEnum.php +++ b/Tests/Fixtures/FooUnitEnum.php @@ -5,4 +5,5 @@ enum FooUnitEnum { case BAR; + case FOO; } diff --git a/Tests/Fixtures/php/services10.php b/Tests/Fixtures/php/services10.php index ce5353921..76cbe12ae 100644 --- a/Tests/Fixtures/php/services10.php +++ b/Tests/Fixtures/php/services10.php @@ -60,7 +60,7 @@ protected function getTestService() } /** - * @return array|bool|float|int|string|null + * @return array|bool|float|int|string|\UnitEnum|null */ public function getParameter($name) { diff --git a/Tests/Fixtures/php/services12.php b/Tests/Fixtures/php/services12.php index 36f55be00..129671604 100644 --- a/Tests/Fixtures/php/services12.php +++ b/Tests/Fixtures/php/services12.php @@ -60,7 +60,7 @@ protected function getTestService() } /** - * @return array|bool|float|int|string|null + * @return array|bool|float|int|string|\UnitEnum|null */ public function getParameter($name) { diff --git a/Tests/Fixtures/php/services19.php b/Tests/Fixtures/php/services19.php index 234454714..efe226574 100644 --- a/Tests/Fixtures/php/services19.php +++ b/Tests/Fixtures/php/services19.php @@ -75,7 +75,7 @@ protected function getServiceWithMethodCallAndFactoryService() } /** - * @return array|bool|float|int|string|null + * @return array|bool|float|int|string|\UnitEnum|null */ public function getParameter($name) { diff --git a/Tests/Fixtures/php/services26.php b/Tests/Fixtures/php/services26.php index 5b6d6d7ba..1028cb4fe 100644 --- a/Tests/Fixtures/php/services26.php +++ b/Tests/Fixtures/php/services26.php @@ -71,7 +71,7 @@ protected function getTestService() } /** - * @return array|bool|float|int|string|null + * @return array|bool|float|int|string|\UnitEnum|null */ public function getParameter($name) { diff --git a/Tests/Fixtures/php/services8.php b/Tests/Fixtures/php/services8.php index a2473e1c3..c99cdb5ec 100644 --- a/Tests/Fixtures/php/services8.php +++ b/Tests/Fixtures/php/services8.php @@ -47,7 +47,7 @@ public function getRemovedIds(): array } /** - * @return array|bool|float|int|string|null + * @return array|bool|float|int|string|\UnitEnum|null */ public function getParameter($name) { diff --git a/Tests/Fixtures/php/services9_as_files.txt b/Tests/Fixtures/php/services9_as_files.txt index d3ef7465d..bafa3d43a 100644 --- a/Tests/Fixtures/php/services9_as_files.txt +++ b/Tests/Fixtures/php/services9_as_files.txt @@ -461,7 +461,7 @@ class ProjectServiceContainer extends Container } /** - * @return array|bool|float|int|string|null + * @return array|bool|float|int|string|\UnitEnum|null */ public function getParameter($name) { diff --git a/Tests/Fixtures/php/services9_compiled.php b/Tests/Fixtures/php/services9_compiled.php index ddc9228b6..53b89d9e0 100644 --- a/Tests/Fixtures/php/services9_compiled.php +++ b/Tests/Fixtures/php/services9_compiled.php @@ -410,7 +410,7 @@ protected function getFactorySimpleService() } /** - * @return array|bool|float|int|string|null + * @return array|bool|float|int|string|\UnitEnum|null */ public function getParameter($name) { diff --git a/Tests/Fixtures/php/services9_inlined_factories.txt b/Tests/Fixtures/php/services9_inlined_factories.txt index 2ce28b8a3..e3b6921da 100644 --- a/Tests/Fixtures/php/services9_inlined_factories.txt +++ b/Tests/Fixtures/php/services9_inlined_factories.txt @@ -459,7 +459,7 @@ class ProjectServiceContainer extends Container } /** - * @return array|bool|float|int|string|null + * @return array|bool|float|int|string|\UnitEnum|null */ public function getParameter($name) { diff --git a/Tests/Fixtures/php/services9_lazy_inlined_factories.txt b/Tests/Fixtures/php/services9_lazy_inlined_factories.txt index 6c50daf2a..3332884c2 100644 --- a/Tests/Fixtures/php/services9_lazy_inlined_factories.txt +++ b/Tests/Fixtures/php/services9_lazy_inlined_factories.txt @@ -95,7 +95,7 @@ class ProjectServiceContainer extends Container } /** - * @return array|bool|float|int|string|null + * @return array|bool|float|int|string|\UnitEnum|null */ public function getParameter($name) { diff --git a/Tests/Fixtures/php/services_array_params.php b/Tests/Fixtures/php/services_array_params.php index 044e4e9f8..13de0aa45 100644 --- a/Tests/Fixtures/php/services_array_params.php +++ b/Tests/Fixtures/php/services_array_params.php @@ -64,7 +64,7 @@ protected function getBarService() } /** - * @return array|bool|float|int|string|null + * @return array|bool|float|int|string|\UnitEnum|null */ public function getParameter($name) { diff --git a/Tests/Fixtures/php/services_base64_env.php b/Tests/Fixtures/php/services_base64_env.php index fd7da6241..08dba3ff3 100644 --- a/Tests/Fixtures/php/services_base64_env.php +++ b/Tests/Fixtures/php/services_base64_env.php @@ -47,7 +47,7 @@ public function getRemovedIds(): array } /** - * @return array|bool|float|int|string|null + * @return array|bool|float|int|string|\UnitEnum|null */ public function getParameter($name) { diff --git a/Tests/Fixtures/php/services_csv_env.php b/Tests/Fixtures/php/services_csv_env.php index 1f89a77b7..b272f1e89 100644 --- a/Tests/Fixtures/php/services_csv_env.php +++ b/Tests/Fixtures/php/services_csv_env.php @@ -47,7 +47,7 @@ public function getRemovedIds(): array } /** - * @return array|bool|float|int|string|null + * @return array|bool|float|int|string|\UnitEnum|null */ public function getParameter($name) { diff --git a/Tests/Fixtures/php/services_default_env.php b/Tests/Fixtures/php/services_default_env.php index 8d30fcae9..5ff213f31 100644 --- a/Tests/Fixtures/php/services_default_env.php +++ b/Tests/Fixtures/php/services_default_env.php @@ -47,7 +47,7 @@ public function getRemovedIds(): array } /** - * @return array|bool|float|int|string|null + * @return array|bool|float|int|string|\UnitEnum|null */ public function getParameter($name) { diff --git a/Tests/Fixtures/php/services_env_in_id.php b/Tests/Fixtures/php/services_env_in_id.php index 716a52a3f..620601845 100644 --- a/Tests/Fixtures/php/services_env_in_id.php +++ b/Tests/Fixtures/php/services_env_in_id.php @@ -73,7 +73,7 @@ protected function getFooService() } /** - * @return array|bool|float|int|string|null + * @return array|bool|float|int|string|\UnitEnum|null */ public function getParameter($name) { diff --git a/Tests/Fixtures/php/services_errored_definition.php b/Tests/Fixtures/php/services_errored_definition.php index 015949d4f..c69fcf53f 100644 --- a/Tests/Fixtures/php/services_errored_definition.php +++ b/Tests/Fixtures/php/services_errored_definition.php @@ -410,7 +410,7 @@ protected function getFactorySimpleService() } /** - * @return array|bool|float|int|string|null + * @return array|bool|float|int|string|\UnitEnum|null */ public function getParameter($name) { diff --git a/Tests/Fixtures/php/services_inline_requires.php b/Tests/Fixtures/php/services_inline_requires.php index ba3c0cba8..d3d9674e5 100644 --- a/Tests/Fixtures/php/services_inline_requires.php +++ b/Tests/Fixtures/php/services_inline_requires.php @@ -93,7 +93,7 @@ protected function getC2Service() } /** - * @return array|bool|float|int|string|null + * @return array|bool|float|int|string|\UnitEnum|null */ public function getParameter($name) { diff --git a/Tests/Fixtures/php/services_json_env.php b/Tests/Fixtures/php/services_json_env.php index f2368dc21..e58d91d73 100644 --- a/Tests/Fixtures/php/services_json_env.php +++ b/Tests/Fixtures/php/services_json_env.php @@ -47,7 +47,7 @@ public function getRemovedIds(): array } /** - * @return array|bool|float|int|string|null + * @return array|bool|float|int|string|\UnitEnum|null */ public function getParameter($name) { diff --git a/Tests/Fixtures/php/services_query_string_env.php b/Tests/Fixtures/php/services_query_string_env.php index c62cd98e3..32a59c514 100644 --- a/Tests/Fixtures/php/services_query_string_env.php +++ b/Tests/Fixtures/php/services_query_string_env.php @@ -47,7 +47,7 @@ public function getRemovedIds(): array } /** - * @return array|bool|float|int|string|null + * @return array|bool|float|int|string|\UnitEnum|null */ public function getParameter($name) { diff --git a/Tests/Fixtures/php/services_rot13_env.php b/Tests/Fixtures/php/services_rot13_env.php index 8b3fe88c4..d282e0481 100644 --- a/Tests/Fixtures/php/services_rot13_env.php +++ b/Tests/Fixtures/php/services_rot13_env.php @@ -78,7 +78,7 @@ protected function getContainer_EnvVarProcessorsLocatorService() } /** - * @return array|bool|float|int|string|null + * @return array|bool|float|int|string|\UnitEnum|null */ public function getParameter($name) { diff --git a/Tests/Fixtures/php/services_unsupported_characters.php b/Tests/Fixtures/php/services_unsupported_characters.php index 77d28cda7..d60ba7b6a 100644 --- a/Tests/Fixtures/php/services_unsupported_characters.php +++ b/Tests/Fixtures/php/services_unsupported_characters.php @@ -82,7 +82,7 @@ protected function getFooohnoService() } /** - * @return array|bool|float|int|string|null + * @return array|bool|float|int|string|\UnitEnum|null */ public function getParameter($name) { diff --git a/Tests/Fixtures/php/services_url_env.php b/Tests/Fixtures/php/services_url_env.php index 347724324..d49c03fd9 100644 --- a/Tests/Fixtures/php/services_url_env.php +++ b/Tests/Fixtures/php/services_url_env.php @@ -47,7 +47,7 @@ public function getRemovedIds(): array } /** - * @return array|bool|float|int|string|null + * @return array|bool|float|int|string|\UnitEnum|null */ public function getParameter($name) { diff --git a/Tests/Fixtures/xml/services_with_enumeration.xml b/Tests/Fixtures/xml/services_with_enumeration.xml index 30e80f005..3f26d968a 100644 --- a/Tests/Fixtures/xml/services_with_enumeration.xml +++ b/Tests/Fixtures/xml/services_with_enumeration.xml @@ -1,5 +1,12 @@ + + Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum::BAR + + Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum::BAR + Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum::FOO + + diff --git a/Tests/Fixtures/yaml/services_with_enumeration.yml b/Tests/Fixtures/yaml/services_with_enumeration.yml index 46bf505d4..0d3357033 100644 --- a/Tests/Fixtures/yaml/services_with_enumeration.yml +++ b/Tests/Fixtures/yaml/services_with_enumeration.yml @@ -1,3 +1,6 @@ +parameters: + unit_enum: !php/const Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum::BAR + enum_array: [!php/const Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum::BAR, !php/const Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum::FOO] services: service_container: diff --git a/composer.json b/composer.json index 15c015087..ab08cb1cd 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ "symfony/service-contracts": "^1.1.6|^2" }, "require-dev": { - "symfony/yaml": "^4.4|^5.0", + "symfony/yaml": "^4.4.26|^5.0", "symfony/config": "^4.3", "symfony/expression-language": "^3.4|^4.0|^5.0" }, @@ -37,7 +37,7 @@ "symfony/config": "<4.3|>=5.0", "symfony/finder": "<3.4", "symfony/proxy-manager-bridge": "<3.4", - "symfony/yaml": "<3.4" + "symfony/yaml": "<4.4.26" }, "provide": { "psr/container-implementation": "1.0", From ae8d53384900acec1baaa3d64808fbf4b23f6c60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Vasseur?= Date: Tue, 15 Feb 2022 18:04:52 +0100 Subject: [PATCH 3/5] Remove duplicated test --- Tests/Dumper/XmlDumperTest.php | 7 ------- 1 file changed, 7 deletions(-) diff --git a/Tests/Dumper/XmlDumperTest.php b/Tests/Dumper/XmlDumperTest.php index 34878972a..91d0d7d90 100644 --- a/Tests/Dumper/XmlDumperTest.php +++ b/Tests/Dumper/XmlDumperTest.php @@ -47,13 +47,6 @@ public function testExportParameters() $this->assertXmlStringEqualsXmlFile(self::$fixturesPath.'/xml/services8.xml', $dumper->dump(), '->dump() dumps parameters'); } - public function testAddParameters() - { - $container = include self::$fixturesPath.'//containers/container8.php'; - $dumper = new XmlDumper($container); - $this->assertXmlStringEqualsXmlFile(self::$fixturesPath.'/xml/services8.xml', $dumper->dump(), '->dump() dumps parameters'); - } - public function testAddService() { $container = include self::$fixturesPath.'/containers/container9.php'; From 9fd93fa1642ed64b0df3a797f9269bd1f7a00086 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Wed, 23 Feb 2022 14:18:10 +0100 Subject: [PATCH 4/5] [DependencyInjection] Clarify that using expressions in parameters is not allowed --- Loader/Configurator/ParametersConfigurator.php | 6 ++++++ Loader/YamlFileLoader.php | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/Loader/Configurator/ParametersConfigurator.php b/Loader/Configurator/ParametersConfigurator.php index 244da04fb..f0cf177d7 100644 --- a/Loader/Configurator/ParametersConfigurator.php +++ b/Loader/Configurator/ParametersConfigurator.php @@ -12,6 +12,8 @@ namespace Symfony\Component\DependencyInjection\Loader\Configurator; use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; +use Symfony\Component\ExpressionLanguage\Expression; /** * @author Nicolas Grekas @@ -34,6 +36,10 @@ public function __construct(ContainerBuilder $container) */ final public function set(string $name, $value): self { + if ($value instanceof Expression) { + throw new InvalidArgumentException(sprintf('Using an expression in parameter "%s" is not allowed.', $name)); + } + $this->container->setParameter($name, static::processValue($value, true)); return $this; diff --git a/Loader/YamlFileLoader.php b/Loader/YamlFileLoader.php index 66d033526..f15fc3492 100644 --- a/Loader/YamlFileLoader.php +++ b/Loader/YamlFileLoader.php @@ -822,6 +822,10 @@ private function resolveServices($value, string $file, bool $isParameter = false $value[$k] = $this->resolveServices($v, $file, $isParameter); } } elseif (\is_string($value) && str_starts_with($value, '@=')) { + if ($isParameter) { + throw new InvalidArgumentException(sprintf('Using expressions in parameters is not allowed in "%s".', $file)); + } + if (!class_exists(Expression::class)) { throw new \LogicException('The "@=" expression syntax cannot be used without the ExpressionLanguage component. Try running "composer require symfony/expression-language".'); } From 149e305ec2f745e1083b795589bf1461dbce022a Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Wed, 23 Feb 2022 17:48:39 +0100 Subject: [PATCH 5/5] [DependencyInjection] Don't reset env placeholders during compilation --- Compiler/MergeExtensionConfigurationPass.php | 8 --- Compiler/ValidateEnvPlaceholdersPass.php | 58 +++++++++----------- 2 files changed, 27 insertions(+), 39 deletions(-) diff --git a/Compiler/MergeExtensionConfigurationPass.php b/Compiler/MergeExtensionConfigurationPass.php index 5e4dd3996..0d20899e9 100644 --- a/Compiler/MergeExtensionConfigurationPass.php +++ b/Compiler/MergeExtensionConfigurationPass.php @@ -79,10 +79,6 @@ public function process(ContainerBuilder $container) $container->getParameterBag()->mergeEnvPlaceholders($resolvingBag); } - if ($configAvailable) { - BaseNode::resetPlaceholders(); - } - throw $e; } @@ -95,10 +91,6 @@ public function process(ContainerBuilder $container) $container->getParameterBag()->add($parameters); } - if ($configAvailable) { - BaseNode::resetPlaceholders(); - } - $container->addDefinitions($definitions); $container->addAliases($aliases); } diff --git a/Compiler/ValidateEnvPlaceholdersPass.php b/Compiler/ValidateEnvPlaceholdersPass.php index cae01c61d..3fb2af1bf 100644 --- a/Compiler/ValidateEnvPlaceholdersPass.php +++ b/Compiler/ValidateEnvPlaceholdersPass.php @@ -48,45 +48,41 @@ public function process(ContainerBuilder $container) $defaultBag = new ParameterBag($resolvingBag->all()); $envTypes = $resolvingBag->getProvidedTypes(); - try { - foreach ($resolvingBag->getEnvPlaceholders() + $resolvingBag->getUnusedEnvPlaceholders() as $env => $placeholders) { - $values = []; - if (false === $i = strpos($env, ':')) { - $default = $defaultBag->has("env($env)") ? $defaultBag->get("env($env)") : self::TYPE_FIXTURES['string']; - $defaultType = null !== $default ? self::getType($default) : 'string'; - $values[$defaultType] = $default; - } else { - $prefix = substr($env, 0, $i); - foreach ($envTypes[$prefix] ?? ['string'] as $type) { - $values[$type] = self::TYPE_FIXTURES[$type] ?? null; - } - } - foreach ($placeholders as $placeholder) { - BaseNode::setPlaceholder($placeholder, $values); + foreach ($resolvingBag->getEnvPlaceholders() + $resolvingBag->getUnusedEnvPlaceholders() as $env => $placeholders) { + $values = []; + if (false === $i = strpos($env, ':')) { + $default = $defaultBag->has("env($env)") ? $defaultBag->get("env($env)") : self::TYPE_FIXTURES['string']; + $defaultType = null !== $default ? self::getType($default) : 'string'; + $values[$defaultType] = $default; + } else { + $prefix = substr($env, 0, $i); + foreach ($envTypes[$prefix] ?? ['string'] as $type) { + $values[$type] = self::TYPE_FIXTURES[$type] ?? null; } } + foreach ($placeholders as $placeholder) { + BaseNode::setPlaceholder($placeholder, $values); + } + } - $processor = new Processor(); + $processor = new Processor(); - foreach ($extensions as $name => $extension) { - if (!$extension instanceof ConfigurationExtensionInterface || !$config = array_filter($container->getExtensionConfig($name))) { - // this extension has no semantic configuration or was not called - continue; - } + foreach ($extensions as $name => $extension) { + if (!$extension instanceof ConfigurationExtensionInterface || !$config = array_filter($container->getExtensionConfig($name))) { + // this extension has no semantic configuration or was not called + continue; + } - $config = $resolvingBag->resolveValue($config); + $config = $resolvingBag->resolveValue($config); - if (null === $configuration = $extension->getConfiguration($config, $container)) { - continue; - } + if (null === $configuration = $extension->getConfiguration($config, $container)) { + continue; + } - try { - $this->extensionConfig[$name] = $processor->processConfiguration($configuration, $config); - } catch (TreeWithoutRootNodeException $e) { - } + try { + $this->extensionConfig[$name] = $processor->processConfiguration($configuration, $config); + } catch (TreeWithoutRootNodeException $e) { } - } finally { - BaseNode::resetPlaceholders(); } $resolvingBag->clearUnusedEnvPlaceholders();