From 0501ecc2d82b05b05dd20b599c4071b60bc7f49f Mon Sep 17 00:00:00 2001 From: Alexander Menshchikov Date: Thu, 29 Apr 2021 23:06:38 +0300 Subject: [PATCH] More accurate message on invalid config builder Throw exception when try to autowire nested bundle config instead of ConfigBuilder. Also renamed test class AcmeConfigBuilder to AcmeConfig according config builders auto naming. --- .../Loader/PhpFileLoader.php | 6 ++- .../Tests/Fixtures/AcmeConfig.php | 42 +++++++++++++++++++ .../Fixtures/AcmeConfig/NestedConfig.php | 10 +++++ .../Tests/Fixtures/AcmeConfigBuilder.php | 27 ------------ .../Tests/Fixtures/config/config_builder.php | 4 +- .../Fixtures/config/nested_bundle_config.php | 7 ++++ .../Tests/Loader/PhpFileLoaderTest.php | 12 ++++++ 7 files changed, 78 insertions(+), 30 deletions(-) create mode 100644 src/Symfony/Component/DependencyInjection/Tests/Fixtures/AcmeConfig.php create mode 100644 src/Symfony/Component/DependencyInjection/Tests/Fixtures/AcmeConfig/NestedConfig.php delete mode 100644 src/Symfony/Component/DependencyInjection/Tests/Fixtures/AcmeConfigBuilder.php create mode 100644 src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/nested_bundle_config.php diff --git a/src/Symfony/Component/DependencyInjection/Loader/PhpFileLoader.php b/src/Symfony/Component/DependencyInjection/Loader/PhpFileLoader.php index a777653140..1bf87cd59e 100644 --- a/src/Symfony/Component/DependencyInjection/Loader/PhpFileLoader.php +++ b/src/Symfony/Component/DependencyInjection/Loader/PhpFileLoader.php @@ -159,12 +159,16 @@ class PhpFileLoader extends FileLoader // If it does not start with Symfony\Config\ we dont know how to handle this if ('Symfony\\Config\\' !== substr($namespace, 0, 15)) { - throw new InvalidargumentException(sprintf('Could not find or generate class "%s".', $namespace)); + throw new InvalidArgumentException(sprintf('Could not find or generate class "%s".', $namespace)); } // Try to get the extension alias $alias = Container::underscore(substr($namespace, 15, -6)); + if (false !== strpos($alias, '\\')) { + throw new InvalidArgumentException('You can only use "root" ConfigBuilders from "Symfony\\Config\\" namespace. Nested classes like "Symfony\\Config\\Framework\\CacheConfig" cannot be used.'); + } + if (!$this->container->hasExtension($alias)) { $extensions = array_filter(array_map(function (ExtensionInterface $ext) { return $ext->getAlias(); }, $this->container->getExtensions())); throw new InvalidArgumentException(sprintf('There is no extension able to load the configuration for "%s". Looked for namespace "%s", found "%s".', $namespace, $alias, $extensions ? implode('", "', $extensions) : 'none')); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/AcmeConfig.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/AcmeConfig.php new file mode 100644 index 0000000000..c6bb3d3244 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/AcmeConfig.php @@ -0,0 +1,42 @@ +color = $value; + } + + public function nested(array $value) + { + if (null === $this->nested) { + $this->nested = new \Symfony\Config\AcmeConfig\NestedConfig(); + } elseif ([] !== $value) { + throw new \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException(sprintf('The node created by "nested()" has already been initialized. You cannot pass values the second time you call nested().')); + } + + return $this->nested; + } + + public function toArray(): array + { + return [ + 'color' => $this->color + ]; + } + + public function getExtensionAlias(): string + { + return 'acme'; + } +} + +class_alias(AcmeConfig::class, '\\Symfony\\Config\\AcmeConfig'); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/AcmeConfig/NestedConfig.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/AcmeConfig/NestedConfig.php new file mode 100644 index 0000000000..0920fa0b26 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/AcmeConfig/NestedConfig.php @@ -0,0 +1,10 @@ +color = $value; - } - - public function toArray(): array - { - return [ - 'color' => $this->color - ]; - } - - public function getExtensionAlias(): string - { - return 'acme'; - } -} diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/config_builder.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/config_builder.php index 3dcacf032f..71f5e58910 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/config_builder.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/config_builder.php @@ -1,7 +1,7 @@ color('blue'); }; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/nested_bundle_config.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/nested_bundle_config.php new file mode 100644 index 0000000000..ba1e5de40d --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/nested_bundle_config.php @@ -0,0 +1,7 @@ +load($fixtures.'/config/deprecated_without_package_version.php'); } + + public function testNestedBundleConfigNotAllowed() + { + $fixtures = realpath(__DIR__.'/../Fixtures'); + $container = new ContainerBuilder(); + $loader = new PhpFileLoader($container, new FileLocator(), 'prod', new ConfigBuilderGenerator(sys_get_temp_dir())); + + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessageMatches('/^'.preg_quote('Could not resolve argument "Symfony\\Config\\AcmeConfig\\NestedConfig $config"', '/').'/'); + + $loader->load($fixtures.'/config/nested_bundle_config.php'); + } }