|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler; |
| 6 | + |
| 7 | +use Doctrine\Bundle\DoctrineBundle\Attribute\AsDbalType; |
| 8 | +use Doctrine\DBAL\Types\Type; |
| 9 | +use ReflectionClass; |
| 10 | +use Symfony\Component\DependencyInjection\ChildDefinition; |
| 11 | +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
| 12 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 13 | +use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
| 14 | + |
| 15 | +use function is_subclass_of; |
| 16 | +use function method_exists; |
| 17 | +use function sprintf; |
| 18 | + |
| 19 | +/** @internal */ |
| 20 | +final class RegisterDbalTypePass implements CompilerPassInterface |
| 21 | +{ |
| 22 | + private const string TAG = 'doctrine.dbal.type'; |
| 23 | + |
| 24 | + /** |
| 25 | + * @param ReflectionClass<T> $reflector |
| 26 | + * |
| 27 | + * @template T of ReflectionClass |
| 28 | + */ |
| 29 | + public static function autoconfigureFromAttribute(ChildDefinition $definition, AsDbalType $type, ReflectionClass $reflector): void |
| 30 | + { |
| 31 | + $attributes = [ |
| 32 | + 'type_name' => $type->name ?? $reflector->name, |
| 33 | + ]; |
| 34 | + |
| 35 | + // Determine if the version of symfony/dependency-injection is >= 7.3 |
| 36 | + /** @phpstan-ignore function.alreadyNarrowedType */ |
| 37 | + if (method_exists($definition, 'addResourceTag')) { |
| 38 | + $definition->addResourceTag(self::TAG, $attributes); |
| 39 | + } else { |
| 40 | + // Needed to keep compatibility with symfony/dependency-injection < 7.3 |
| 41 | + $definition->addTag(self::TAG, $attributes) |
| 42 | + ->addTag('container.excluded', ['source' => sprintf('by tag "%s"', self::TAG)]); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + public function process(ContainerBuilder $container): void |
| 47 | + { |
| 48 | + $types = $container->getParameter('doctrine.dbal.connection_factory.types'); |
| 49 | + |
| 50 | + foreach ($this->findTaggedResourceIds($container) as $id => $tags) { |
| 51 | + foreach ($tags as $tag) { |
| 52 | + $class = $container->getDefinition($id)->getClass(); |
| 53 | + if (! $class) { |
| 54 | + throw new InvalidArgumentException(sprintf('The definition of "%s" must define its class.', $id)); |
| 55 | + } |
| 56 | + |
| 57 | + if (! is_subclass_of($class, Type::class)) { |
| 58 | + throw new InvalidArgumentException(sprintf('The "%s" class must extends "%s".', $class, Type::class)); |
| 59 | + } |
| 60 | + |
| 61 | + $types[$tag['type_name'] ?? $id] = ['class' => $class]; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + $container->setParameter('doctrine.dbal.connection_factory.types', $types); |
| 66 | + } |
| 67 | + |
| 68 | + /** @return array<string, array<array{type_name?: string}>> */ |
| 69 | + private function findTaggedResourceIds(ContainerBuilder $container): array |
| 70 | + { |
| 71 | + // Determine if the version of symfony/dependency-injection is >= 7.3 |
| 72 | + /** @phpstan-ignore function.alreadyNarrowedType */ |
| 73 | + if (method_exists($container, 'findTaggedResourceIds')) { |
| 74 | + return $container->findTaggedResourceIds(self::TAG); |
| 75 | + } |
| 76 | + |
| 77 | + // Needed to keep compatibility with symfony/dependency-injection < 7.3 |
| 78 | + $tags = []; |
| 79 | + foreach ($container->getDefinitions() as $id => $definition) { |
| 80 | + if (! $definition->hasTag(self::TAG)) { |
| 81 | + continue; |
| 82 | + } |
| 83 | + |
| 84 | + if (! $definition->hasTag('container.excluded')) { |
| 85 | + throw new InvalidArgumentException(sprintf('The resource "%s" tagged "%s" is missing the "container.excluded" tag.', $id, self::TAG)); |
| 86 | + } |
| 87 | + |
| 88 | + $class = $container->getParameterBag()->resolveValue($definition->getClass()); |
| 89 | + if (! $class || $definition->isAbstract()) { |
| 90 | + throw new InvalidArgumentException(sprintf('The resource "%s" tagged "%s" must have a class and not be abstract.', $id, self::TAG)); |
| 91 | + } |
| 92 | + |
| 93 | + if ($definition->getClass() !== $class) { |
| 94 | + $definition->setClass($class); |
| 95 | + } |
| 96 | + |
| 97 | + $tags[$id] = $definition->getTag(self::TAG); |
| 98 | + } |
| 99 | + |
| 100 | + return $tags; |
| 101 | + } |
| 102 | +} |
0 commit comments