diff --git a/src/Symfony/Component/DependencyInjection/Attribute/When.php b/src/Symfony/Component/DependencyInjection/Attribute/When.php index 8cb01c87e1b6e..3114115fd22ef 100644 --- a/src/Symfony/Component/DependencyInjection/Attribute/When.php +++ b/src/Symfony/Component/DependencyInjection/Attribute/When.php @@ -16,7 +16,7 @@ * * @author Nicolas Grekas
*/ -#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)] +#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)] class When { public function __construct( diff --git a/src/Symfony/Component/DependencyInjection/Loader/PhpFileLoader.php b/src/Symfony/Component/DependencyInjection/Loader/PhpFileLoader.php index 43a379778bd38..007ab1b248d9c 100644 --- a/src/Symfony/Component/DependencyInjection/Loader/PhpFileLoader.php +++ b/src/Symfony/Component/DependencyInjection/Loader/PhpFileLoader.php @@ -15,6 +15,7 @@ use Symfony\Component\Config\Builder\ConfigBuilderGeneratorInterface; use Symfony\Component\Config\Builder\ConfigBuilderInterface; use Symfony\Component\Config\FileLocatorInterface; +use Symfony\Component\DependencyInjection\Attribute\When; use Symfony\Component\DependencyInjection\Container; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; @@ -55,12 +56,12 @@ public function load($resource, string $type = null) $this->container->fileExists($path); // the closure forbids access to the private scope in the included file - $load = \Closure::bind(function ($path) use ($container, $loader, $resource, $type) { + $load = \Closure::bind(function ($path, $env) use ($container, $loader, $resource, $type) { return include $path; }, $this, ProtectedPhpFileLoader::class); try { - $callback = $load($path); + $callback = $load($path, $this->env); if (\is_object($callback) && \is_callable($callback)) { $this->executeCallback($callback, new ContainerConfigurator($this->container, $this, $this->instanceof, $path, $resource, $this->env), $path); @@ -98,8 +99,22 @@ private function executeCallback(callable $callback, ContainerConfigurator $cont $arguments = []; $configBuilders = []; - $parameters = (new \ReflectionFunction($callback))->getParameters(); - foreach ($parameters as $parameter) { + $r = new \ReflectionFunction($callback); + + if (\PHP_VERSION_ID >= 80000) { + $attribute = null; + foreach ($r->getAttributes(When::class) as $attribute) { + if ($this->env === $attribute->newInstance()->env) { + $attribute = null; + break; + } + } + if (null !== $attribute) { + return; + } + } + + foreach ($r->getParameters() as $parameter) { $reflectionType = $parameter->getType(); if (!$reflectionType instanceof \ReflectionNamedType) { throw new \InvalidArgumentException(sprintf('Could not resolve argument "$%s" for "%s". You must typehint it (for example with "%s" or "%s").', $parameter->getName(), $path, ContainerConfigurator::class, ContainerBuilder::class)); 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 71f5e58910be8..02772e64ccb99 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/config_builder.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/config_builder.php @@ -2,6 +2,10 @@ use Symfony\Component\DependencyInjection\Tests\Fixtures\AcmeConfig; -return static function (AcmeConfig $config) { +if ('prod' !== $env) { + return; +} + +return function (AcmeConfig $config) { $config->color('blue'); }; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/env_configurator.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/env_configurator.php index 1c6a3b9038467..15434bcfd8828 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/env_configurator.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/env_configurator.php @@ -2,7 +2,7 @@ namespace Symfony\Component\DependencyInjection\Loader\Configurator; -return static function (ContainerConfigurator $configurator): void { +return function (ContainerConfigurator $configurator): void { $services = $configurator->services(); $services 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 index ba1e5de40df32..f343eab3d6ef5 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/nested_bundle_config.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/nested_bundle_config.php @@ -2,6 +2,6 @@ use Symfony\Config\AcmeConfig\NestedConfig; -return static function (NestedConfig $config) { +return function (NestedConfig $config) { throw new RuntimeException('This code should not be run.'); }; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/when_env.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/when_env.php new file mode 100644 index 0000000000000..318e9e1cfb69b --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/when_env.php @@ -0,0 +1,7 @@ +load($fixtures.'/config/nested_bundle_config.php'); } + + /** + * @requires PHP 8 + */ + public function testWhenEnv() + { + $fixtures = realpath(__DIR__.'/../Fixtures'); + $container = new ContainerBuilder(); + $loader = new PhpFileLoader($container, new FileLocator(), 'dev', new ConfigBuilderGenerator(sys_get_temp_dir())); + + $loader->load($fixtures.'/config/when_env.php'); + } }
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: