Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

InjectEntityManagerMappingsPass.php
Go to the documentation of this file.
1 <?php
2
7 declare(strict_types=1);
8
9 namespace eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Compiler;
10
11 use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
12 use Symfony\Component\DependencyInjection\ContainerBuilder;
13 use Symfony\Component\DependencyInjection\Definition;
14 use Symfony\Component\DependencyInjection\Reference;
15
20 final class InjectEntityManagerMappingsPass implements CompilerPassInterface
21 {
22 public function process(ContainerBuilder $container): void
23 {
24 $entityManagers = $container->getParameter('doctrine.entity_managers');
25 $entityMappings = $container->getParameter('ibexa.orm.entity_mappings');
26
27 $mappingDriverConfig = $this->prepareMappingDriverConfig($entityMappings, $container);
28
29 foreach ($entityManagers as $entityManagerName => $serviceName) {
30 if (strpos($entityManagerName, 'ibexa_') !== 0) {
31 continue;
32 }
33
34 $chainMetadataDriverDefinition = $container->getDefinition(
35 sprintf('doctrine.orm.%s_metadata_driver', $entityManagerName)
36 );
37
38 $ormConfigDefinition = $container->getDefinition(
39 sprintf('doctrine.orm.%s_configuration', $entityManagerName)
40 );
41
42 $entityMap = $this->getEntityMapForConfigurationService($entityMappings);
43 $ormConfigDefinition->addMethodCall('setEntityNamespaces', [$entityMap]);
44
45 foreach ($mappingDriverConfig as $driverType => $driverPaths) {
46 $metadataDriverServiceName = "doctrine.orm.{$entityManagerName}_{$driverType}_metadata_driver";
47 $metadataDriverDefinition = $this->createMetadataDriverDefinition($driverType, $driverPaths);
48
49 if (
50 false !== strpos($metadataDriverDefinition->getClass(), 'yml')
51 || false !== strpos($metadataDriverDefinition->getClass(), 'xml')
52 ) {
53 $metadataDriverDefinition->setArguments([array_flip($driverPaths)]);
54 $metadataDriverDefinition->addMethodCall('setGlobalBasename', ['mapping']);
55 }
56
57 $container->setDefinition($metadataDriverServiceName, $metadataDriverDefinition);
58
59 foreach ($driverPaths as $prefix => $driverPath) {
60 $chainMetadataDriverDefinition->addMethodCall(
61 'addDriver',
62 [new Reference($metadataDriverServiceName), $prefix]
63 );
64 }
65 }
66 }
67 }
68
69 private function createMetadataDriverDefinition($driverType, $driverPaths): Definition
70 {
71 $metadataDriver = new Definition("%doctrine.orm.metadata.{$driverType}.class%");
72 $arguments = [];
73
74 if ('annotation' === $driverType) {
75 $arguments[] = new Reference('doctrine.orm.metadata.annotation_reader');
76 }
77
78 $arguments[] = array_values($driverPaths);
79
80 $metadataDriver->setArguments($arguments);
81 $metadataDriver->setPublic(false);
82
83 return $metadataDriver;
84 }
85
86 private function prepareMappingDriverConfig(array $entityManagerConfig, ContainerBuilder $container): array
87 {
88 $bundles = $container->getParameter('kernel.bundles');
89 $driverConfig = [];
90 foreach ($entityManagerConfig as $mappingName => $config) {
91 $config = array_replace([
92 'dir' => false,
93 'type' => false,
94 'prefix' => false,
95 ], (array) $config);
96
97 $config['dir'] = $container->getParameterBag()->resolveValue($config['dir']);
98
99 if ($config['is_bundle']) {
100 $bundle = null;
101 foreach ($bundles as $bundleName => $class) {
102 if ($mappingName === $bundleName) {
103 $bundle = new \ReflectionClass($class);
104
105 break;
106 }
107 }
108
109 if (null === $bundle) {
110 throw new \InvalidArgumentException(sprintf(
111 'Bundle "%s" does not exist or it is not enabled.',
112 $mappingName)
113 );
114 }
115
116 $config = $this->getMappingDriverBundleConfigDefaults($config, $bundle);
117 }
118
119 if (!is_dir($config['dir'])) {
120 throw new \InvalidArgumentException(sprintf(
121 'Invalid Doctrine mapping path given. Cannot load Doctrine mapping/bundle named "%s".',
122 $mappingName
123 ));
124 }
125
126 $driverConfig[$config['type']][$config['prefix']] = realpath($config['dir']) ?: $config['dir'];
127 }
128
129 return $driverConfig;
130 }
131
132 private function getMappingDriverBundleConfigDefaults(
133 array $bundleConfig,
134 \ReflectionClass $bundle
135 ): array {
136 $bundleDir = \dirname($bundle->getFileName());
137
138 if (!$bundleConfig['type'] || !$bundleConfig['dir'] || !$bundleConfig['prefix']) {
139 throw new \InvalidArgumentException(
140 "Entity Mapping has invalid configuration. Please provide 'type', 'dir' and 'prefix' parameters."
141 );
142 }
143
144 $bundleConfig['dir'] = $bundleDir . '/' . $bundleConfig['dir'];
145
146 return $bundleConfig;
147 }
148
149 private function getEntityMapForConfigurationService(array $entityMappings): array
150 {
151 return array_combine(
152 array_keys($entityMappings),
153 array_column($entityMappings, 'prefix')
154 );
155 }
156 }
eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Compiler\InjectEntityManagerMappingsPass
Creating Entity Mapping drivers is based on AbstractDoctrineExtension from doctrine/doctrine-bundle.
Definition: InjectEntityManagerMappingsPass.php:21
eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Compiler\InjectEntityManagerMappingsPass\getEntityMapForConfigurationService
getEntityMapForConfigurationService(array $entityMappings)
Definition: InjectEntityManagerMappingsPass.php:149
eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Compiler\InjectEntityManagerMappingsPass\getMappingDriverBundleConfigDefaults
getMappingDriverBundleConfigDefaults(array $bundleConfig, \ReflectionClass $bundle)
Definition: InjectEntityManagerMappingsPass.php:132
eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Compiler\InjectEntityManagerMappingsPass\createMetadataDriverDefinition
createMetadataDriverDefinition($driverType, $driverPaths)
Definition: InjectEntityManagerMappingsPass.php:69
eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Compiler\InjectEntityManagerMappingsPass\process
process(ContainerBuilder $container)
Definition: InjectEntityManagerMappingsPass.php:22
eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Compiler\InjectEntityManagerMappingsPass\prepareMappingDriverConfig
prepareMappingDriverConfig(array $entityManagerConfig, ContainerBuilder $container)
Definition: InjectEntityManagerMappingsPass.php:86
eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Compiler
Definition: AsseticPass.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.