Skip to content

Commit 8f5c6b2

Browse files
committed
Mark the definition with the new Document attribute as excluded from service
1 parent 9c2ccf0 commit 8f5c6b2

File tree

2 files changed

+47
-31
lines changed

2 files changed

+47
-31
lines changed

src/DependencyInjection/DoctrineMongoDBExtension.php

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,8 @@
1717
use Doctrine\Common\EventSubscriber;
1818
use Doctrine\ODM\MongoDB\Configuration as ODMConfiguration;
1919
use Doctrine\ODM\MongoDB\DocumentManager;
20-
use Doctrine\ODM\MongoDB\Mapping\Annotations\Document;
21-
use Doctrine\ODM\MongoDB\Mapping\Annotations\EmbeddedDocument;
22-
use Doctrine\ODM\MongoDB\Mapping\Annotations\File;
23-
use Doctrine\ODM\MongoDB\Mapping\Annotations\MappedSuperclass;
24-
use Doctrine\ODM\MongoDB\Mapping\Annotations\QueryResultDocument;
25-
use Doctrine\ODM\MongoDB\Mapping\Annotations\View;
20+
use Doctrine\ODM\MongoDB\Mapping\Annotations;
21+
use Doctrine\ODM\MongoDB\Mapping\Attribute;
2622
use Doctrine\ODM\MongoDB\Mapping\Driver\AttributeDriver;
2723
use Doctrine\Persistence\Mapping\Driver\MappingDriverChain;
2824
use Doctrine\Persistence\Proxy;
@@ -532,24 +528,30 @@ public function load(array $configs, ContainerBuilder $container): void
532528
});
533529

534530
// Document classes are excluded from the container by default
535-
$container->registerAttributeForAutoconfiguration(Document::class, static function (ChildDefinition $definition): void {
536-
$definition->addTag('container.excluded', ['source' => sprintf('with #[%s] attribute', Document::class)]);
537-
});
538-
$container->registerAttributeForAutoconfiguration(EmbeddedDocument::class, static function (ChildDefinition $definition): void {
539-
$definition->addTag('container.excluded', ['source' => sprintf('with #[%s] attribute', EmbeddedDocument::class)]);
540-
});
541-
$container->registerAttributeForAutoconfiguration(MappedSuperclass::class, static function (ChildDefinition $definition): void {
542-
$definition->addTag('container.excluded', ['source' => sprintf('with #[%s] attribute', MappedSuperclass::class)]);
543-
});
544-
$container->registerAttributeForAutoconfiguration(View::class, static function (ChildDefinition $definition): void {
545-
$definition->addTag('container.excluded', ['source' => sprintf('with #[%s] attribute', View::class)]);
546-
});
547-
$container->registerAttributeForAutoconfiguration(QueryResultDocument::class, static function (ChildDefinition $definition): void {
548-
$definition->addTag('container.excluded', ['source' => sprintf('with #[%s] attribute', QueryResultDocument::class)]);
549-
});
550-
$container->registerAttributeForAutoconfiguration(File::class, static function (ChildDefinition $definition): void {
551-
$definition->addTag('container.excluded', ['source' => sprintf('with #[%s] attribute', File::class)]);
552-
});
531+
foreach (
532+
[
533+
// Deprecated since ODM 2.16
534+
Annotations\Document::class,
535+
Annotations\EmbeddedDocument::class,
536+
Annotations\MappedSuperclass::class,
537+
Annotations\View::class,
538+
Annotations\QueryResultDocument::class,
539+
Annotations\File::class,
540+
// New in ODM 2.16
541+
Attribute\Document::class,
542+
Attribute\EmbeddedDocument::class,
543+
Attribute\MappedSuperclass::class,
544+
Attribute\View::class,
545+
Attribute\QueryResultDocument::class,
546+
Attribute\File::class,
547+
] as $class
548+
) {
549+
if (class_exists($class)) {
550+
$container->registerAttributeForAutoconfiguration($class, static function (ChildDefinition $definition, object $attribute): void {
551+
$definition->addTag('container.excluded', ['source' => sprintf('with #[%s] attribute', $attribute::class)]);
552+
});
553+
}
554+
}
553555

554556
$this->loadMessengerServices($container, $loader);
555557

tests/DependencyInjection/DoctrineMongoDBExtensionTest.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919
use Doctrine\ODM\MongoDB\Configuration;
2020
use Doctrine\ODM\MongoDB\DocumentManager;
2121
use Doctrine\ODM\MongoDB\Mapping\Annotations;
22+
use Doctrine\ODM\MongoDB\Mapping\Attribute;
2223
use InvalidArgumentException;
2324
use MongoDB\Client;
2425
use PHPUnit\Framework\Attributes\DataProvider;
2526
use PHPUnit\Framework\TestCase;
2627
use ProxyManager\Proxy\GhostObjectInterface;
28+
use ReflectionClass;
2729
use stdClass;
2830
use Symfony\Component\DependencyInjection\Alias;
2931
use Symfony\Component\DependencyInjection\ChildDefinition;
@@ -37,6 +39,7 @@
3739

3840
use function array_diff_key;
3941
use function array_merge;
42+
use function class_exists;
4043
use function interface_exists;
4144
use function is_dir;
4245
use function method_exists;
@@ -134,18 +137,28 @@ public function testAsDocumentListenerAttribute(): void
134137
public static function provideAttributeExcludedFromContainer(): array
135138
{
136139
return [
137-
'Document' => [Annotations\Document::class],
138-
'EmbeddedDocument' => [Annotations\EmbeddedDocument::class],
139-
'MappedSuperclass' => [Annotations\MappedSuperclass::class],
140-
'View' => [Annotations\View::class],
141-
'QueryResultDocument' => [Annotations\QueryResultDocument::class],
142-
'File' => [Annotations\File::class],
140+
'Annotations\Document' => [Annotations\Document::class],
141+
'Annotations\EmbeddedDocument' => [Annotations\EmbeddedDocument::class],
142+
'Annotations\MappedSuperclass' => [Annotations\MappedSuperclass::class],
143+
'Annotations\View' => [Annotations\View::class],
144+
'Annotations\QueryResultDocument' => [Annotations\QueryResultDocument::class],
145+
'Annotations\File' => [Annotations\File::class],
146+
'Attribute\Document' => [Attribute\Document::class],
147+
'Attribute\EmbeddedDocument' => [Attribute\EmbeddedDocument::class],
148+
'Attribute\MappedSuperclass' => [Attribute\MappedSuperclass::class],
149+
'Attribute\View' => [Attribute\View::class],
150+
'Attribute\QueryResultDocument' => [Attribute\QueryResultDocument::class],
151+
'Attribute\File' => [Attribute\File::class],
143152
];
144153
}
145154

146155
#[DataProvider('provideAttributeExcludedFromContainer')]
147156
public function testDocumentAttributeExcludesFromContainer(string $class): void
148157
{
158+
if (! class_exists($class)) {
159+
$this->markTestSkipped(sprintf('Class %s does not exist.', $class));
160+
}
161+
149162
$container = $this->getContainer();
150163
$extension = new DoctrineMongoDBExtension();
151164
$extension->load($this->buildConfiguration(), $container);
@@ -163,7 +176,8 @@ public function testDocumentAttributeExcludesFromContainer(string $class): void
163176
$this->assertInstanceOf(Closure::class, $autoconfigurator);
164177

165178
$definition = new ChildDefinition('');
166-
$autoconfigurator($definition);
179+
$attribute = (new ReflectionClass($class))->newInstanceWithoutConstructor();
180+
$autoconfigurator($definition, $attribute);
167181

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

0 commit comments

Comments
 (0)