Skip to content

Commit eda148b

Browse files
authored
Fix wiring of PHP metadata drivers on doctrine/orm 3.x (#1832)
1 parent 7bfb8e8 commit eda148b

11 files changed

+53
-94
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"symfony/console": "^5.4 || ^6.0 || ^7.0",
4040
"symfony/dependency-injection": "^5.4 || ^6.0 || ^7.0",
4141
"symfony/deprecation-contracts": "^2.1 || ^3",
42-
"symfony/doctrine-bridge": "^5.4.19 || ^6.0.7 || ^7.0",
42+
"symfony/doctrine-bridge": "^5.4.46 || ^6.4.3 || ^7.0.3",
4343
"symfony/framework-bundle": "^5.4 || ^6.0 || ^7.0",
4444
"symfony/polyfill-php80": "^1.15",
4545
"symfony/service-contracts": "^1.1.1 || ^2.0 || ^3"

psalm.xml.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
<referencedClass name="Doctrine\ORM\Mapping\Driver\YamlDriver"/>
4141
<referencedClass name="Doctrine\ORM\Mapping\Driver\SimplifiedYamlDriver"/>
4242
<referencedClass name="Doctrine\ORM\Mapping\Driver\AnnotationDriver"/>
43+
<referencedClass name="Doctrine\ORM\Mapping\Driver\PHPDriver"/>
44+
<referencedClass name="Doctrine\ORM\Mapping\Driver\StaticPHPDriver"/>
4345
<referencedClass name="Doctrine\ORM\ORMException"/>
4446
<!-- Dropped in DBAL 4 -->
4547
<referencedClass name="Doctrine\DBAL\Exception"/>

src/DependencyInjection/DoctrineExtension.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,21 @@
2222
use Doctrine\ORM\EntityManagerInterface;
2323
use Doctrine\ORM\Events;
2424
use Doctrine\ORM\Id\AbstractIdGenerator;
25+
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
26+
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
27+
use Doctrine\ORM\Mapping\Driver\PHPDriver as LegacyPHPDriver;
2528
use Doctrine\ORM\Mapping\Driver\SimplifiedXmlDriver;
29+
use Doctrine\ORM\Mapping\Driver\SimplifiedYamlDriver;
30+
use Doctrine\ORM\Mapping\Driver\StaticPHPDriver as LegacyStaticPHPDriver;
2631
use Doctrine\ORM\Proxy\Autoloader;
2732
use Doctrine\ORM\Proxy\ProxyFactory;
2833
use Doctrine\ORM\Tools\Console\Command\ConvertMappingCommand;
2934
use Doctrine\ORM\Tools\Console\Command\EnsureProductionSettingsCommand;
3035
use Doctrine\ORM\Tools\Export\ClassMetadataExporter;
3136
use Doctrine\ORM\UnitOfWork;
37+
use Doctrine\Persistence\Mapping\Driver\MappingDriverChain;
38+
use Doctrine\Persistence\Mapping\Driver\PHPDriver;
39+
use Doctrine\Persistence\Mapping\Driver\StaticPHPDriver;
3240
use Doctrine\Persistence\Reflection\RuntimeReflectionProperty;
3341
use InvalidArgumentException;
3442
use LogicException;
@@ -1163,7 +1171,35 @@ public function getConfiguration(array $config, ContainerBuilder $container): Co
11631171

11641172
protected function getMetadataDriverClass(string $driverType): string
11651173
{
1166-
return '%' . $this->getObjectManagerElementName('metadata.' . $driverType . '.class') . '%';
1174+
switch ($driverType) {
1175+
case 'driver_chain':
1176+
return MappingDriverChain::class;
1177+
1178+
case 'annotation':
1179+
if (! class_exists(AnnotationDriver::class)) {
1180+
throw new LogicException('The annotation driver is only available in doctrine/orm v2.');
1181+
}
1182+
1183+
return AnnotationDriver::class;
1184+
1185+
case 'xml':
1186+
return SimplifiedXmlDriver::class;
1187+
1188+
case 'yml':
1189+
return SimplifiedYamlDriver::class;
1190+
1191+
case 'php':
1192+
return class_exists(PHPDriver::class) ? PHPDriver::class : LegacyPHPDriver::class;
1193+
1194+
case 'staticphp':
1195+
return class_exists(StaticPHPDriver::class) ? StaticPHPDriver::class : LegacyStaticPHPDriver::class;
1196+
1197+
case 'attribute':
1198+
return AttributeDriver::class;
1199+
1200+
default:
1201+
throw new LogicException(sprintf('Unknown "%s" metadata driver type.', $driverType));
1202+
}
11671203
}
11681204

11691205
private function loadMessengerServices(ContainerBuilder $container): void

tests/DependencyInjection/AbstractDoctrineExtensionTest.php

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,11 @@
4040
use Symfony\Component\DependencyInjection\Reference;
4141
use Symfony\Component\DependencyInjection\ServiceLocator;
4242
use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler;
43-
use Symfony\Component\HttpKernel\Kernel;
4443
use Symfony\Component\Security\Core\User\UserInterface;
4544

4645
use function array_filter;
4746
use function array_intersect_key;
4847
use function array_keys;
49-
use function array_merge;
5048
use function array_values;
5149
use function assert;
5250
use function class_exists;
@@ -57,7 +55,6 @@
5755
use function sprintf;
5856
use function sys_get_temp_dir;
5957
use function uniqid;
60-
use function version_compare;
6158

6259
use const DIRECTORY_SEPARATOR;
6360

@@ -543,40 +540,30 @@ public function testSingleEntityManagerMultipleMappingBundleDefinitions(): void
543540
self::markTestSkipped('This test requires ORM');
544541
}
545542

546-
$container = $this->loadContainer('orm_single_em_bundle_mappings', ['YamlBundle', 'AnnotationsBundle', 'XmlBundle', 'AttributesBundle']);
543+
$container = $this->loadContainer('orm_single_em_bundle_mappings', ['YamlBundle', 'XmlBundle', 'AttributesBundle']);
547544

548545
$definition = $container->getDefinition('doctrine.orm.default_metadata_driver');
549546

550547
$this->assertDICDefinitionMethodCallAt(0, $definition, 'addDriver', [
551-
new Reference(version_compare(Kernel::VERSION, '7.0.0', '<') ? 'doctrine.orm.default_annotation_metadata_driver' : 'doctrine.orm.default_attribute_metadata_driver'),
552-
'Fixtures\Bundles\AnnotationsBundle\Entity',
553-
]);
554-
555-
$this->assertDICDefinitionMethodCallAt(1, $definition, 'addDriver', [
556548
new Reference('doctrine.orm.default_attribute_metadata_driver'),
557549
'Fixtures\Bundles\AttributesBundle\Entity',
558550
]);
559551

560-
$this->assertDICDefinitionMethodCallAt(2, $definition, 'addDriver', [
552+
$this->assertDICDefinitionMethodCallAt(1, $definition, 'addDriver', [
561553
new Reference('doctrine.orm.default_yml_metadata_driver'),
562554
'Fixtures\Bundles\YamlBundle\Entity',
563555
]);
564556

565-
$this->assertDICDefinitionMethodCallAt(3, $definition, 'addDriver', [
557+
$this->assertDICDefinitionMethodCallAt(2, $definition, 'addDriver', [
566558
new Reference('doctrine.orm.default_xml_metadata_driver'),
567559
'Fixtures\Bundles\XmlBundle',
568560
]);
569561

570562
$attrDef = $container->getDefinition('doctrine.orm.default_attribute_metadata_driver');
571563
$this->assertDICConstructorArguments($attrDef, [
572-
array_merge(
573-
! version_compare(Kernel::VERSION, '7.0.0', '<') ? [
574-
__DIR__ . DIRECTORY_SEPARATOR . 'Fixtures' . DIRECTORY_SEPARATOR . 'Bundles' . DIRECTORY_SEPARATOR . 'AnnotationsBundle' . DIRECTORY_SEPARATOR . 'Entity',
575-
] : [],
576-
[
577-
__DIR__ . DIRECTORY_SEPARATOR . 'Fixtures' . DIRECTORY_SEPARATOR . 'Bundles' . DIRECTORY_SEPARATOR . 'AttributesBundle' . DIRECTORY_SEPARATOR . 'Entity',
578-
],
579-
),
564+
[
565+
__DIR__ . DIRECTORY_SEPARATOR . 'Fixtures' . DIRECTORY_SEPARATOR . 'Bundles' . DIRECTORY_SEPARATOR . 'AttributesBundle' . DIRECTORY_SEPARATOR . 'Entity',
566+
],
580567
! class_exists(AnnotationDriver::class),
581568
]);
582569

@@ -600,21 +587,15 @@ public function testMultipleEntityManagersMappingBundleDefinitions(): void
600587
self::markTestSkipped('This test requires ORM');
601588
}
602589

603-
$container = $this->loadContainer('orm_multiple_em_bundle_mappings', ['YamlBundle', 'AnnotationsBundle', 'XmlBundle', 'AttributesBundle']);
590+
$container = $this->loadContainer('orm_multiple_em_bundle_mappings', ['YamlBundle', 'XmlBundle', 'AttributesBundle']);
604591

605592
$this->assertEquals(['em1' => 'doctrine.orm.em1_entity_manager', 'em2' => 'doctrine.orm.em2_entity_manager'], $container->getParameter('doctrine.entity_managers'), 'Set of the existing EntityManagers names is incorrect.');
606593
$this->assertEquals('%doctrine.entity_managers%', $container->getDefinition('doctrine')->getArgument(2), 'Set of the existing EntityManagers names is incorrect.');
607594

608-
$def1 = $container->getDefinition('doctrine.orm.em1_metadata_driver');
609-
$def2 = $container->getDefinition('doctrine.orm.em2_metadata_driver');
610-
$def1Id = version_compare(Kernel::VERSION, '7.0.0', '<') ? 'doctrine.orm.em1_annotation_metadata_driver' : 'doctrine.orm.em1_attribute_metadata_driver';
595+
$def1 = $container->getDefinition('doctrine.orm.em1_metadata_driver');
596+
$def2 = $container->getDefinition('doctrine.orm.em2_metadata_driver');
611597

612598
$this->assertDICDefinitionMethodCallAt(0, $def1, 'addDriver', [
613-
new Reference($def1Id),
614-
'Fixtures\Bundles\AnnotationsBundle\Entity',
615-
]);
616-
617-
$this->assertDICDefinitionMethodCallAt(1, $def1, 'addDriver', [
618599
new Reference('doctrine.orm.em1_attribute_metadata_driver'),
619600
'Fixtures\Bundles\AttributesBundle\Entity',
620601
]);
@@ -629,17 +610,6 @@ public function testMultipleEntityManagersMappingBundleDefinitions(): void
629610
'Fixtures\Bundles\XmlBundle',
630611
]);
631612

632-
if (version_compare(Kernel::VERSION, '7.0.0', '<')) {
633-
$annDef = $container->getDefinition($def1Id);
634-
$this->assertDICConstructorArguments($annDef, [
635-
new Reference('doctrine.orm.metadata.annotation_reader'),
636-
[
637-
__DIR__ . DIRECTORY_SEPARATOR . 'Fixtures' . DIRECTORY_SEPARATOR . 'Bundles' . DIRECTORY_SEPARATOR . 'AnnotationsBundle' . DIRECTORY_SEPARATOR . 'Entity',
638-
],
639-
! class_exists(AnnotationDriver::class),
640-
]);
641-
}
642-
643613
$ymlDef = $container->getDefinition('doctrine.orm.em2_yml_metadata_driver');
644614
$this->assertDICConstructorArguments($ymlDef, [
645615
[__DIR__ . DIRECTORY_SEPARATOR . 'Fixtures' . DIRECTORY_SEPARATOR . 'Bundles' . DIRECTORY_SEPARATOR . 'YamlBundle' . DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'doctrine' => 'Fixtures\Bundles\YamlBundle\Entity'],
@@ -659,7 +629,7 @@ public function testSingleEntityManagerDefaultTableOptions(): void
659629
self::markTestSkipped('This test requires ORM');
660630
}
661631

662-
$container = $this->loadContainer('orm_single_em_default_table_options', ['YamlBundle', 'AnnotationsBundle', 'XmlBundle', 'AttributesBundle']);
632+
$container = $this->loadContainer('orm_single_em_default_table_options', ['YamlBundle', 'XmlBundle', 'AttributesBundle']);
663633

664634
$param = $container->getDefinition('doctrine.dbal.default_connection')->getArgument(0);
665635

tests/DependencyInjection/DoctrineExtensionTest.php

Lines changed: 3 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
use Symfony\Component\DependencyInjection\Reference;
5151
use Symfony\Component\Messenger\Bridge\Doctrine\Transport\DoctrineTransportFactory;
5252
use Symfony\Component\Messenger\MessageBusInterface;
53-
use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;
5453

5554
use function array_values;
5655
use function class_exists;
@@ -823,39 +822,6 @@ public function testXmlBundleMappingDetection(): void
823822
]);
824823
}
825824

826-
public function testAnnotationsBundleMappingDetection(): void
827-
{
828-
if (! interface_exists(EntityManagerInterface::class)) {
829-
self::markTestSkipped('This test requires ORM');
830-
}
831-
832-
$container = $this->getContainer(['AnnotationsBundle']);
833-
$extension = new DoctrineExtension();
834-
835-
$config = BundleConfigurationBuilder::createBuilder()
836-
->addBaseConnection()
837-
->addEntityManager([
838-
'default_entity_manager' => 'default',
839-
'entity_managers' => [
840-
'default' => [
841-
'mappings' => [
842-
'AnnotationsBundle' => [],
843-
],
844-
],
845-
],
846-
])
847-
->build();
848-
$extension->load([$config], $container);
849-
850-
$definition = $container->getDefinition('doctrine.orm.default_metadata_driver');
851-
$this->assertDICDefinitionMethodCallOnce($definition, 'addDriver', [
852-
new Reference(class_exists(AnnotationLoader::class)
853-
? 'doctrine.orm.default_annotation_metadata_driver'
854-
: 'doctrine.orm.default_attribute_metadata_driver'),
855-
'Fixtures\Bundles\AnnotationsBundle\Entity',
856-
]);
857-
}
858-
859825
/** @requires PHP 8 */
860826
public function testAttributesBundleMappingDetection(): void
861827
{
@@ -898,7 +864,7 @@ public function testOrmMergeConfigs(): void
898864
self::markTestSkipped('This test requires ORM');
899865
}
900866

901-
$container = $this->getContainer(['XmlBundle', 'AnnotationsBundle', 'AttributesBundle']);
867+
$container = $this->getContainer(['XmlBundle', 'AttributesBundle']);
902868
$extension = new DoctrineExtension();
903869

904870
$config1 = BundleConfigurationBuilder::createBuilder()
@@ -908,10 +874,7 @@ public function testOrmMergeConfigs(): void
908874
'default_entity_manager' => 'default',
909875
'entity_managers' => [
910876
'default' => [
911-
'mappings' => [
912-
'AnnotationsBundle' => [],
913-
'AttributesBundle' => ['type' => 'attribute'],
914-
],
877+
'mappings' => ['AttributesBundle' => ['type' => 'attribute']],
915878
],
916879
],
917880
])
@@ -934,16 +897,10 @@ public function testOrmMergeConfigs(): void
934897

935898
$definition = $container->getDefinition('doctrine.orm.default_metadata_driver');
936899
$this->assertDICDefinitionMethodCallAt(0, $definition, 'addDriver', [
937-
new Reference(class_exists(AnnotationLoader::class)
938-
? 'doctrine.orm.default_annotation_metadata_driver'
939-
: 'doctrine.orm.default_attribute_metadata_driver'),
940-
'Fixtures\Bundles\AnnotationsBundle\Entity',
941-
]);
942-
$this->assertDICDefinitionMethodCallAt(1, $definition, 'addDriver', [
943900
new Reference('doctrine.orm.default_attribute_metadata_driver'),
944901
'Fixtures\Bundles\AttributesBundle\Entity',
945902
]);
946-
$this->assertDICDefinitionMethodCallAt(2, $definition, 'addDriver', [
903+
$this->assertDICDefinitionMethodCallAt(1, $definition, 'addDriver', [
947904
new Reference('doctrine.orm.default_xml_metadata_driver'),
948905
'Fixtures\Bundles\XmlBundle\Entity',
949906
]);

tests/DependencyInjection/Fixtures/config/xml/orm_multiple_em_bundle_mappings.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
<orm default-entity-manager="em2">
1515
<entity-manager name="em1">
16-
<mapping name="AnnotationsBundle" />
1716
<mapping name="AttributesBundle" type="attribute" />
1817
</entity-manager>
1918
<entity-manager name="em2" validate-xml-mapping="true">

tests/DependencyInjection/Fixtures/config/xml/orm_single_em_bundle_mappings.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
</dbal>
1313

1414
<orm validate-xml-mapping="true">
15-
<mapping name="AnnotationsBundle" />
1615
<mapping name="AttributesBundle" type="attribute" />
1716
<mapping name="YamlBundle" dir="Resources/config/doctrine" alias="yml" />
1817
<mapping name="manual" type="xml" prefix="Fixtures\Bundles\XmlBundle"

tests/DependencyInjection/Fixtures/config/xml/orm_single_em_default_table_options.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
</dbal>
1717

1818
<orm>
19-
<mapping name="AnnotationsBundle" />
2019
<mapping name="AttributesBundle" type="attribute" />
2120
<mapping name="YamlBundle" dir="Resources/config/doctrine" alias="yml" />
2221
<mapping name="manual" type="xml" prefix="Fixtures\Bundles\XmlBundle"

tests/DependencyInjection/Fixtures/config/yml/orm_multiple_em_bundle_mappings.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ doctrine:
1010
entity_managers:
1111
em1:
1212
mappings:
13-
AnnotationsBundle: ~
1413
AttributesBundle:
1514
type: attribute
1615
em2:

tests/DependencyInjection/Fixtures/config/yml/orm_single_em_bundle_mappings.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ doctrine:
88
orm:
99
validate_xml_mapping: true
1010
mappings:
11-
AnnotationsBundle: ~
1211
AttributesBundle:
1312
type: attribute
1413
YamlBundle:

0 commit comments

Comments
 (0)