Skip to content

[DependencyInjection] add #[Exclude] in order to not register as a service #46655

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/Symfony/Component/DependencyInjection/Attribute/Exclude.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <lamiraultantoine@gmail.com>
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::TARGET_FUNCTION | \Attribute::IS_REPEATABLE)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder how this can be used on methods / functions? Wouldn't it be better to only exclude services?

class Exclude
{
}
5 changes: 5 additions & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
---

Expand Down
26 changes: 17 additions & 9 deletions src/Symfony/Component/DependencyInjection/Loader/FileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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))) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

never use empty

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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -100,6 +101,10 @@ private function executeCallback(callable $callback, ContainerConfigurator $cont
$configBuilders = [];
$r = new \ReflectionFunction($callback);

if (!empty($r->getAttributes(Exclude::class))) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

never use empty

return;
}

$attribute = null;
foreach ($r->getAttributes(When::class) as $attribute) {
if ($this->env === $attribute->newInstance()->env) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Symfony\Component\DependencyInjection\Tests\Fixtures;

use Symfony\Component\DependencyInjection\Attribute\Exclude;

#[Exclude]
class ExcludedDummy
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

use Symfony\Component\DependencyInjection\Attribute\Exclude;

return #[Exclude] function () {
throw new RuntimeException('This code should not be run.');
};
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Tests\Fixtures\ExcludedDummy;
use Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\BadClasses\MissingParent;
use Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Foo;
use Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\FooInterface;
Expand Down Expand Up @@ -271,6 +272,19 @@ public function testRegisterClassesWithWhenEnv(?string $env, bool $expected)

$this->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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}