Skip to content

Commit af044b8

Browse files
committed
move autoconfiguration callback in the compiler pass
1 parent 5a9c2e8 commit af044b8

2 files changed

Lines changed: 34 additions & 25 deletions

File tree

src/DependencyInjection/Compiler/RegisterDbalTypePass.php

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
namespace Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler;
66

7+
use Doctrine\Bundle\DoctrineBundle\Attribute\AsDatabaseType;
78
use Doctrine\DBAL\Types\Type;
9+
use ReflectionClass;
10+
use Symfony\Component\DependencyInjection\ChildDefinition;
811
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
912
use Symfony\Component\DependencyInjection\ContainerBuilder;
1013
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
@@ -17,6 +20,30 @@
1720
/** @internal */
1821
final class RegisterDbalTypePass implements CompilerPassInterface
1922
{
23+
private const string TAG = 'doctrine.dbal.type';
24+
25+
/**
26+
* @param ReflectionClass<T> $reflector
27+
*
28+
* @template T of ReflectionClass
29+
*/
30+
public static function autoconfigureFromAttribute(ChildDefinition $definition, AsDatabaseType $type, ReflectionClass $reflector): void
31+
{
32+
$attributes = [
33+
'name' => $type->name ?? $reflector->name,
34+
];
35+
36+
// Determine if the version of symfony/dependency-injection is >= 7.3
37+
/** @phpstan-ignore function.alreadyNarrowedType */
38+
if (method_exists($definition, 'addResourceTag')) {
39+
$definition->addResourceTag(self::TAG, $attributes);
40+
} else {
41+
// Needed to keep compatibility with symfony/dependency-injection < 7.3
42+
$definition->addTag(self::TAG, $attributes)
43+
->addTag('container.excluded', ['source' => sprintf('by tag "%s"', self::TAG)]);
44+
}
45+
}
46+
2047
public function process(ContainerBuilder $container): void
2148
{
2249
$types = $container->getParameter('doctrine.dbal.connection_factory.types');
@@ -46,35 +73,33 @@ public function process(ContainerBuilder $container): void
4673
/** @return array<string, array<array{name?: string}>> */
4774
private function findTaggedResourceIds(ContainerBuilder $container): array
4875
{
49-
$tagName = 'doctrine.dbal.type';
50-
5176
// Determine if the version of symfony/dependency-injection is >= 7.3
5277
/** @phpstan-ignore function.alreadyNarrowedType */
5378
if (method_exists($container, 'findTaggedResourceIds')) {
54-
return $container->findTaggedResourceIds($tagName);
79+
return $container->findTaggedResourceIds(self::TAG);
5580
}
5681

5782
// Needed to keep compatibility with symfony/dependency-injection < 7.3
5883
$tags = [];
5984
foreach ($container->getDefinitions() as $id => $definition) {
60-
if (! $definition->hasTag($tagName)) {
85+
if (! $definition->hasTag(self::TAG)) {
6186
continue;
6287
}
6388

6489
if (! $definition->hasTag('container.excluded')) {
65-
throw new InvalidArgumentException(sprintf('The resource "%s" tagged "%s" is missing the "container.excluded" tag.', $id, $tagName));
90+
throw new InvalidArgumentException(sprintf('The resource "%s" tagged "%s" is missing the "container.excluded" tag.', $id, self::TAG));
6691
}
6792

6893
$class = $container->getParameterBag()->resolveValue($definition->getClass());
6994
if (! $class || $definition->isAbstract()) {
70-
throw new InvalidArgumentException(sprintf('The resource "%s" tagged "%s" must have a class and not be abstract.', $id, $tagName));
95+
throw new InvalidArgumentException(sprintf('The resource "%s" tagged "%s" must have a class and not be abstract.', $id, self::TAG));
7196
}
7297

7398
if ($definition->getClass() !== $class) {
7499
$definition->setClass($class);
75100
}
76101

77-
$tags[$id] = $definition->getTag($tagName);
102+
$tags[$id] = $definition->getTag(self::TAG);
78103
}
79104

80105
return $tags;

src/DependencyInjection/DoctrineExtension.php

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Doctrine\Bundle\DoctrineBundle\ConnectionFactory;
1313
use Doctrine\Bundle\DoctrineBundle\Dbal\RegexSchemaAssetFilter;
1414
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\IdGeneratorPass;
15+
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\RegisterDbalTypePass;
1516
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\ServiceRepositoryCompilerPass;
1617
use Doctrine\Bundle\DoctrineBundle\Mapping\ContainerEntityListenerResolver;
1718
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepositoryInterface;
@@ -43,7 +44,6 @@
4344
use InvalidArgumentException;
4445
use LogicException;
4546
use ReflectionClass;
46-
use Reflector;
4747
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
4848
use Symfony\Bridge\Doctrine\IdGenerator\UlidGenerator;
4949
use Symfony\Bridge\Doctrine\IdGenerator\UuidGenerator;
@@ -82,7 +82,6 @@
8282
use function interface_exists;
8383
use function is_dir;
8484
use function is_string;
85-
use function method_exists;
8685
use function realpath;
8786
use function reset;
8887
use function sprintf;
@@ -554,22 +553,7 @@ private function dbalLoad(array $config, ContainerBuilder $container): void
554553
}
555554

556555
/** @phpstan-ignore argument.type (Needed for the $reflector parameter) */
557-
$container->registerAttributeForAutoconfiguration(AsDatabaseType::class, static function (ChildDefinition $definition, AsDatabaseType $type, ReflectionClass $reflector): void {
558-
$tag = 'doctrine.dbal.type';
559-
$attributes = [
560-
'name' => $type->name ?? $reflector->name,
561-
];
562-
563-
// Determine if the version of symfony/dependency-injection is >= 7.3
564-
/** @phpstan-ignore function.alreadyNarrowedType */
565-
if (method_exists($definition, 'addResourceTag')) {
566-
$definition->addResourceTag($tag, $attributes);
567-
} else {
568-
// Needed to keep compatibility with symfony/dependency-injection < 7.3
569-
$definition->addTag('doctrine.dbal.type', $attributes)
570-
->addTag('container.excluded', ['source' => sprintf('by tag "%s"', $tag)]);
571-
}
572-
});
556+
$container->registerAttributeForAutoconfiguration(AsDatabaseType::class, RegisterDbalTypePass::autoconfigureFromAttribute(...));
573557

574558
$container->registerForAutoconfiguration(MiddlewareInterface::class)->addTag('doctrine.middleware');
575559

0 commit comments

Comments
 (0)