13
13
14
14
use Doctrine \Common \Persistence \ManagerRegistry as LegacyManagerRegistry ;
15
15
use Doctrine \Common \Persistence \Mapping \ClassMetadata as LegacyClassMetadata ;
16
- use Doctrine \Common \Persistence \Mapping \Driver \MappingDriver as LegacyMappingDriver ;
17
16
use Doctrine \Common \Persistence \Mapping \MappingException as LegacyPersistenceMappingException ;
18
17
use Doctrine \DBAL \Connection ;
19
18
use Doctrine \ORM \EntityManagerInterface ;
24
23
use Doctrine \Persistence \Mapping \AbstractClassMetadataFactory ;
25
24
use Doctrine \Persistence \Mapping \ClassMetadata ;
26
25
use Doctrine \Persistence \Mapping \Driver \AnnotationDriver ;
27
- use Doctrine \Persistence \Mapping \Driver \MappingDriver ;
28
26
use Doctrine \Persistence \Mapping \Driver \MappingDriverChain ;
29
27
use Doctrine \Persistence \Mapping \MappingException as PersistenceMappingException ;
30
28
use Symfony \Bundle \MakerBundle \Util \ClassNameDetails ;
@@ -48,13 +46,19 @@ final class DoctrineHelper
48
46
*/
49
47
private $ registry ;
50
48
49
+ /**
50
+ * @var array|null
51
+ */
52
+ private $ annotatedPrefixes ;
53
+
51
54
/**
52
55
* @var ManagerRegistry|LegacyManagerRegistry
53
56
*/
54
- public function __construct (string $ entityNamespace , $ registry = null )
57
+ public function __construct (string $ entityNamespace , $ registry = null , array $ annotatedPrefixes = null )
55
58
{
56
59
$ this ->entityNamespace = trim ($ entityNamespace , '\\' );
57
60
$ this ->registry = $ registry ;
61
+ $ this ->annotatedPrefixes = $ annotatedPrefixes ;
58
62
}
59
63
60
64
/**
@@ -81,12 +85,7 @@ public function getEntityNamespace(): string
81
85
return $ this ->entityNamespace ;
82
86
}
83
87
84
- /**
85
- * @return MappingDriver|LegacyMappingDriver|null
86
- *
87
- * @throws \Exception
88
- */
89
- public function getMappingDriverForClass (string $ className )
88
+ public function isClassAnnotated (string $ className ): bool
90
89
{
91
90
/** @var EntityManagerInterface $em */
92
91
$ em = $ this ->getRegistry ()->getManagerForClass ($ className );
@@ -95,19 +94,32 @@ public function getMappingDriverForClass(string $className)
95
94
throw new \InvalidArgumentException (sprintf ('Cannot find the entity manager for class "%s" ' , $ className ));
96
95
}
97
96
98
- $ metadataDriver = $ em ->getConfiguration ()->getMetadataDriverImpl ();
97
+ if (null === $ this ->annotatedPrefixes ) {
98
+ // doctrine-bundle <= 2.2
99
+ $ metadataDriver = $ em ->getConfiguration ()->getMetadataDriverImpl ();
100
+
101
+ if (!$ this ->isInstanceOf ($ metadataDriver , MappingDriverChain::class)) {
102
+ return $ metadataDriver instanceof AnnotationDriver;
103
+ }
99
104
100
- if (!$ this ->isInstanceOf ($ metadataDriver , MappingDriverChain::class)) {
101
- return $ metadataDriver ;
105
+ foreach ($ metadataDriver ->getDrivers () as $ namespace => $ driver ) {
106
+ if (0 === strpos ($ className , $ namespace )) {
107
+ return $ driver instanceof AnnotationDriver;
108
+ }
109
+ }
110
+
111
+ return $ metadataDriver ->getDefaultDriver () instanceof AnnotationDriver;
102
112
}
103
113
104
- foreach ($ metadataDriver ->getDrivers () as $ namespace => $ driver ) {
105
- if (0 === strpos ($ className , $ namespace )) {
106
- return $ driver ;
114
+ $ managerName = array_search ($ em , $ this ->getRegistry ()->getManagers (), true );
115
+
116
+ foreach ($ this ->annotatedPrefixes [$ managerName ] as [$ prefix , $ annotationDriver ]) {
117
+ if (0 === strpos ($ className , $ prefix )) {
118
+ return null !== $ annotationDriver ;
107
119
}
108
120
}
109
121
110
- return $ metadataDriver -> getDefaultDriver () ;
122
+ return false ;
111
123
}
112
124
113
125
public function getEntitiesForAutocomplete (): array
@@ -134,6 +146,18 @@ public function getEntitiesForAutocomplete(): array
134
146
*/
135
147
public function getMetadata (string $ classOrNamespace = null , bool $ disconnected = false )
136
148
{
149
+ $ classNames = (new \ReflectionClass (AnnotationDriver::class))->getProperty ('classNames ' );
150
+ $ classNames ->setAccessible (true );
151
+
152
+ // Invalidating the cached AnnotationDriver::$classNames to find new Entity classes
153
+ foreach ($ this ->annotatedPrefixes ?? [] as $ managerName => $ prefixes ) {
154
+ foreach ($ prefixes as [$ prefix , $ annotationDriver ]) {
155
+ if (null !== $ annotationDriver ) {
156
+ $ classNames ->setValue ($ annotationDriver , null );
157
+ }
158
+ }
159
+ }
160
+
137
161
$ metadata = [];
138
162
139
163
/** @var EntityManagerInterface $em */
@@ -158,15 +182,14 @@ public function getMetadata(string $classOrNamespace = null, bool $disconnected
158
182
$ cmf ->setMetadataFor ($ m ->getName (), $ m );
159
183
}
160
184
161
- // Invalidating the cached AnnotationDriver::$classNames to find new Entity classes
162
- $ metadataDriver = $ em ->getConfiguration ()->getMetadataDriverImpl ();
163
- if ($ this ->isInstanceOf ($ metadataDriver , MappingDriverChain::class)) {
164
- foreach ($ metadataDriver ->getDrivers () as $ driver ) {
165
- if ($ this ->isInstanceOf ($ driver , AnnotationDriver::class)) {
166
- $ classNames = (new \ReflectionObject ($ driver ))->getProperty ('classNames ' );
167
- $ classNames ->setAccessible (true );
168
- $ classNames ->setValue ($ driver , null );
169
- $ classNames ->setAccessible (false );
185
+ if (null === $ this ->annotatedPrefixes ) {
186
+ // Invalidating the cached AnnotationDriver::$classNames to find new Entity classes
187
+ $ metadataDriver = $ em ->getConfiguration ()->getMetadataDriverImpl ();
188
+ if ($ this ->isInstanceOf ($ metadataDriver , MappingDriverChain::class)) {
189
+ foreach ($ metadataDriver ->getDrivers () as $ driver ) {
190
+ if ($ this ->isInstanceOf ($ driver , AnnotationDriver::class)) {
191
+ $ classNames ->setValue ($ driver , null );
192
+ }
170
193
}
171
194
}
172
195
}
0 commit comments