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