diff --git a/src/Symfony/Component/DependencyInjection/Attribute/Exclude.php b/src/Symfony/Component/DependencyInjection/Attribute/Exclude.php new file mode 100644 index 0000000000000..8afaf5823d27e --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Attribute/Exclude.php @@ -0,0 +1,22 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\DependencyInjection\Attribute; + +/** + * An attribute to tell class, method or function should not be registered as a service. + * + * @author Antoine Lamirault + */ +#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::TARGET_FUNCTION | \Attribute::IS_REPEATABLE)] +class Exclude +{ +} diff --git a/src/Symfony/Component/DependencyInjection/CHANGELOG.md b/src/Symfony/Component/DependencyInjection/CHANGELOG.md index ced60bd357c4c..05f9b2cd84801 100644 --- a/src/Symfony/Component/DependencyInjection/CHANGELOG.md +++ b/src/Symfony/Component/DependencyInjection/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +6.2 +--- + +* Add `#[Exclude]` attribute to tell class, method or function should not be registered as a service + 6.1 --- diff --git a/src/Symfony/Component/DependencyInjection/Loader/FileLoader.php b/src/Symfony/Component/DependencyInjection/Loader/FileLoader.php index b74d8d9bfc1e5..8fed161cebdee 100644 --- a/src/Symfony/Component/DependencyInjection/Loader/FileLoader.php +++ b/src/Symfony/Component/DependencyInjection/Loader/FileLoader.php @@ -17,6 +17,7 @@ use Symfony\Component\Config\Loader\FileLoader as BaseFileLoader; use Symfony\Component\Config\Loader\Loader; use Symfony\Component\Config\Resource\GlobResource; +use Symfony\Component\DependencyInjection\Attribute\Exclude; use Symfony\Component\DependencyInjection\Attribute\When; use Symfony\Component\DependencyInjection\ChildDefinition; use Symfony\Component\DependencyInjection\Compiler\RegisterAutoconfigureAttributesPass; @@ -109,18 +110,25 @@ public function registerClasses(Definition $prototype, string $namespace, string $serializedPrototype = serialize($prototype); foreach ($classes as $class => $errorMessage) { - if (null === $errorMessage && $autoconfigureAttributes && $this->env) { + if (null === $errorMessage && $autoconfigureAttributes) { $r = $this->container->getReflectionClass($class); - $attribute = null; - foreach ($r->getAttributes(When::class) as $attribute) { - if ($this->env === $attribute->newInstance()->env) { - $attribute = null; - break; - } - } - if (null !== $attribute) { + + if (!empty($r->getAttributes(Exclude::class))) { continue; } + + if ($this->env) { + $attribute = null; + foreach ($r->getAttributes(When::class) as $attribute) { + if ($this->env === $attribute->newInstance()->env) { + $attribute = null; + break; + } + } + if (null !== $attribute) { + continue; + } + } } if (interface_exists($class, false)) { diff --git a/src/Symfony/Component/DependencyInjection/Loader/PhpFileLoader.php b/src/Symfony/Component/DependencyInjection/Loader/PhpFileLoader.php index 629ca506845c6..cd6d5b91bea21 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\Exclude; use Symfony\Component\DependencyInjection\Attribute\When; use Symfony\Component\DependencyInjection\Container; use Symfony\Component\DependencyInjection\ContainerBuilder; @@ -100,6 +101,10 @@ private function executeCallback(callable $callback, ContainerConfigurator $cont $configBuilders = []; $r = new \ReflectionFunction($callback); + if (!empty($r->getAttributes(Exclude::class))) { + return; + } + $attribute = null; foreach ($r->getAttributes(When::class) as $attribute) { if ($this->env === $attribute->newInstance()->env) { diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/ExcludedDummy.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/ExcludedDummy.php new file mode 100644 index 0000000000000..e0357c92e5e6c --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/ExcludedDummy.php @@ -0,0 +1,10 @@ +assertSame($expected, $container->has(Foo::class)); } + + public function testNotRegisterClassesWithExclude() + { + $container = new ContainerBuilder(); + $loader = new TestFileLoader($container, new FileLocator(self::$fixturesPath.'/Fixtures'), 'prod'); + $loader->registerClasses( + (new Definition())->setAutoconfigured(true), + 'Symfony\Component\DependencyInjection\Tests\Fixtures\\', + '{ExcludedDummy.php}' + ); + + $this->assertFalse($container->has(ExcludedDummy::class)); + } } class TestFileLoader extends FileLoader diff --git a/src/Symfony/Component/DependencyInjection/Tests/Loader/PhpFileLoaderTest.php b/src/Symfony/Component/DependencyInjection/Tests/Loader/PhpFileLoaderTest.php index a16d44814fb04..d15af1e533c80 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Loader/PhpFileLoaderTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Loader/PhpFileLoaderTest.php @@ -184,4 +184,13 @@ public function testWhenEnv() $loader->load($fixtures.'/config/when_env.php'); } + + public function testExclude() + { + $fixtures = realpath(__DIR__.'/../Fixtures'); + $container = new ContainerBuilder(); + $loader = new PhpFileLoader($container, new FileLocator(), 'dev', new ConfigBuilderGenerator(sys_get_temp_dir())); + + $loader->load($fixtures.'/config/exclude.php'); + } } 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