Skip to content

Commit

Permalink
Ensure classes using the #[Entity] attribute are not declared as serv…
Browse files Browse the repository at this point in the history
…ices
  • Loading branch information
GromNaN committed Feb 6, 2025
1 parent ed97813 commit b1f7454
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/DependencyInjection/DoctrineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Doctrine\ORM\Mapping\Driver\SimplifiedXmlDriver;
use Doctrine\ORM\Mapping\Driver\SimplifiedYamlDriver;
use Doctrine\ORM\Mapping\Driver\StaticPHPDriver as LegacyStaticPHPDriver;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Proxy\Autoloader;
use Doctrine\ORM\Proxy\ProxyFactory;
use Doctrine\ORM\Tools\Console\Command\ConvertMappingCommand;
Expand Down Expand Up @@ -643,6 +644,9 @@ protected function ormLoad(array $config, ContainerBuilder $container)
'connection' => $attribute->connection,
]);
});
$container->registerAttributeForAutoconfiguration(Entity::class, static function (ChildDefinition $definition) {
$definition->setAbstract(true)->addTag('container.excluded', ['source' => sprintf('with #[%s] attribute', Entity::class)]);
});

/** @see DoctrineBundle::boot() */
$container->getDefinition($defaultEntityManagerDefinitionId)
Expand Down
27 changes: 27 additions & 0 deletions tests/DependencyInjection/DoctrineExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use Doctrine\ORM\Mapping\Driver\SimplifiedXmlDriver;
use Doctrine\ORM\Mapping\Driver\SimplifiedYamlDriver;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\Persistence\Mapping\Driver\MappingDriverChain;
use Fixtures\Bundles\AttributesBundle\Entity\TestCustomIdGeneratorEntity;
use InvalidArgumentException;
use LogicException;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -1166,6 +1168,31 @@ public static function cacheConfigurationProvider(): array
];
}

public function testEntityAttributeExcludesFromContainer()
{
$container = $this->getContainer();
$extension = new DoctrineExtension();

$config = BundleConfigurationBuilder::createBuilder()
->addBaseConnection()
->addBaseEntityManager()
->build();

$extension->load([$config], $container);

$attributes = $container->getAutoconfiguredAttributes();
$this->assertInstanceOf(Closure::class, $attributes[Entity::class]);

$reflector = new ReflectionClass(TestCustomIdGeneratorEntity::class);
$definition = new ChildDefinition('');
$attribute = $reflector->getAttributes(Entity::class)[0]->newInstance();

$attributes[Entity::class]($definition, $attribute);

$this->assertSame([['source' => 'with #[Doctrine\ORM\Mapping\Entity] attribute']], $definition->getTag('container.excluded'));
$this->assertTrue($definition->isAbstract());
}

/** @requires PHP 8 */
public function testAsEntityListenerAttribute()
{
Expand Down

0 comments on commit b1f7454

Please sign in to comment.