Skip to content

Commit ea214eb

Browse files
committed
add compatibility with Symfony < 7.3
1 parent 9996357 commit ea214eb

2 files changed

Lines changed: 53 additions & 3 deletions

File tree

src/DependencyInjection/Compiler/RegisterDbalTypePass.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use function array_key_exists;
1313
use function is_subclass_of;
14+
use function method_exists;
1415
use function sprintf;
1516

1617
/** @internal */
@@ -20,7 +21,7 @@ public function process(ContainerBuilder $container): void
2021
{
2122
$types = $container->getParameter('doctrine.dbal.connection_factory.types');
2223

23-
foreach ($container->findTaggedResourceIds('doctrine.dbal.type') as $id => $tags) {
24+
foreach ($this->findTaggedResourceIds($container) as $id => $tags) {
2425
foreach ($tags as $tag) {
2526
if (! array_key_exists('name', $tag)) {
2627
throw new InvalidArgumentException(sprintf('The "name" attribute is mandatory for the "doctrine.dbal.type" tag on the "%s" type.', $id));
@@ -41,4 +42,41 @@ public function process(ContainerBuilder $container): void
4142

4243
$container->setParameter('doctrine.dbal.connection_factory.types', $types);
4344
}
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+
}
4482
}

src/DependencyInjection/DoctrineExtension.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
use function interface_exists;
8282
use function is_dir;
8383
use function is_string;
84+
use function method_exists;
8485
use function realpath;
8586
use function reset;
8687
use function sprintf;
@@ -552,9 +553,20 @@ private function dbalLoad(array $config, ContainerBuilder $container): void
552553
}
553554

554555
$container->registerAttributeForAutoconfiguration(AsDbalType::class, static function (ChildDefinition $definition, AsDbalType $type): void {
555-
$definition->addResourceTag('doctrine.dbal.type', [
556+
$tag = 'doctrine.dbal.type';
557+
$attributes = [
556558
'name' => $type->name,
557-
]);
559+
];
560+
561+
// Determine if the version of symfony/dependency-injection is >= 7.3
562+
/** @phpstan-ignore function.alreadyNarrowedType */
563+
if (method_exists($definition, 'addResourceTag')) {
564+
$definition->addResourceTag($tag, $attributes);
565+
} else {
566+
// Needed to keep compatibility with symfony/dependency-injection < 7.3
567+
$definition->addTag('doctrine.dbal.type', $attributes)
568+
->addTag('container.excluded', ['source' => sprintf('by tag "%s"', $tag)]);
569+
}
558570
});
559571

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

0 commit comments

Comments
 (0)