Skip to content

Ensure classes using the #[Entity] attribute are not declared as service #1868

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

Merged
merged 1 commit into from
Mar 17, 2025
Merged
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
13 changes: 13 additions & 0 deletions src/DependencyInjection/DoctrineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
use Doctrine\ORM\Mapping\Driver\SimplifiedXmlDriver;
use Doctrine\ORM\Mapping\Driver\SimplifiedYamlDriver;
use Doctrine\ORM\Mapping\Driver\StaticPHPDriver as LegacyStaticPHPDriver;
use Doctrine\ORM\Mapping\Embeddable;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\MappedSuperclass;
use Doctrine\ORM\Proxy\Autoloader;
use Doctrine\ORM\Proxy\ProxyFactory;
use Doctrine\ORM\Tools\Console\Command\ConvertMappingCommand;
Expand Down Expand Up @@ -644,6 +647,16 @@ protected function ormLoad(array $config, ContainerBuilder $container)
]);
});

$container->registerAttributeForAutoconfiguration(Embeddable::class, static function (ChildDefinition $definition) {
$definition->setAbstract(true)->addTag('container.excluded', ['source' => sprintf('with #[%s] attribute', Embeddable::class)]);
});
$container->registerAttributeForAutoconfiguration(Entity::class, static function (ChildDefinition $definition) {
$definition->setAbstract(true)->addTag('container.excluded', ['source' => sprintf('with #[%s] attribute', Entity::class)]);
});
$container->registerAttributeForAutoconfiguration(MappedSuperclass::class, static function (ChildDefinition $definition) {
$definition->setAbstract(true)->addTag('container.excluded', ['source' => sprintf('with #[%s] attribute', MappedSuperclass::class)]);
});

/** @see DoctrineBundle::boot() */
$container->getDefinition($defaultEntityManagerDefinitionId)
->addTag('container.preload', [
Expand Down
40 changes: 40 additions & 0 deletions tests/DependencyInjection/DoctrineExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use Doctrine\ORM\Mapping\Driver\SimplifiedXmlDriver;
use Doctrine\ORM\Mapping\Driver\SimplifiedYamlDriver;
use Doctrine\ORM\Mapping\Embeddable;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\MappedSuperclass;
use Doctrine\Persistence\Mapping\Driver\MappingDriverChain;
use InvalidArgumentException;
use LogicException;
Expand Down Expand Up @@ -1165,6 +1168,43 @@ public static function cacheConfigurationProvider(): array
];
}

/** @return array<array{0: class-string}> */
public static function provideAttributeExcludedFromContainer(): array
{
return [
'Embeddable' => [Embeddable::class],
'Entity' => [Entity::class],
'MappedSuperclass' => [MappedSuperclass::class],
];
}

/** @dataProvider provideAttributeExcludedFromContainer */
public function testEntityAttributeExcludesFromContainer(string $class)
{
if (! interface_exists(EntityManagerInterface::class)) {
self::markTestSkipped('This test requires ORM');
}

$container = $this->getContainer();
$extension = new DoctrineExtension();

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

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

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

$definition = new ChildDefinition('');
$attributes[$class]($definition);

$this->assertSame([['source' => sprintf('with #[%s] attribute', $class)]], $definition->getTag('container.excluded'));
$this->assertTrue($definition->isAbstract());
}

public function testAsEntityListenerAttribute()
{
if (! interface_exists(EntityManagerInterface::class)) {
Expand Down