11
11
12
12
namespace Symfony \Bundle \MakerBundle \Doctrine ;
13
13
14
- use Doctrine \Common \Persistence \ManagerRegistry ;
15
- use Doctrine \Common \Persistence \Mapping \AbstractClassMetadataFactory ;
16
- use Doctrine \Common \Persistence \Mapping \ClassMetadata ;
17
- use Doctrine \Common \Persistence \Mapping \Driver \AnnotationDriver ;
18
- use Doctrine \Common \Persistence \Mapping \Driver \MappingDriver ;
19
- use Doctrine \Common \Persistence \Mapping \Driver \MappingDriverChain ;
20
- use Doctrine \Common \Persistence \Mapping \MappingException as PersistenceMappingException ;
14
+ use Doctrine \Common \Persistence \ManagerRegistry as LegacyManagerRegistry ;
15
+ use Doctrine \Common \Persistence \Mapping \ClassMetadata as LegacyClassMetadata ;
16
+ use Doctrine \Common \Persistence \Mapping \Driver \MappingDriver as LegacyMappingDriver ;
17
+ use Doctrine \Common \Persistence \Mapping \MappingException as LegacyPersistenceMappingException ;
21
18
use Doctrine \ORM \EntityManagerInterface ;
22
19
use Doctrine \ORM \Mapping \MappingException as ORMMappingException ;
23
20
use Doctrine \ORM \Tools \DisconnectedClassMetadataFactory ;
21
+ use Doctrine \Persistence \ManagerRegistry ;
22
+ use Doctrine \Persistence \Mapping \AbstractClassMetadataFactory ;
23
+ use Doctrine \Persistence \Mapping \ClassMetadata ;
24
+ use Doctrine \Persistence \Mapping \Driver \AnnotationDriver ;
25
+ use Doctrine \Persistence \Mapping \Driver \MappingDriver ;
26
+ use Doctrine \Persistence \Mapping \Driver \MappingDriverChain ;
27
+ use Doctrine \Persistence \Mapping \MappingException as PersistenceMappingException ;
24
28
use Symfony \Bundle \MakerBundle \Util \ClassNameDetails ;
25
29
26
30
/**
@@ -42,13 +46,19 @@ final class DoctrineHelper
42
46
*/
43
47
private $ registry ;
44
48
45
- public function __construct (string $ entityNamespace , ManagerRegistry $ registry = null )
49
+ /**
50
+ * @var ManagerRegistry|LegacyManagerRegistry
51
+ */
52
+ public function __construct (string $ entityNamespace , $ registry = null )
46
53
{
47
54
$ this ->entityNamespace = trim ($ entityNamespace , '\\' );
48
55
$ this ->registry = $ registry ;
49
56
}
50
57
51
- public function getRegistry (): ManagerRegistry
58
+ /**
59
+ * @return LegacyManagerRegistry|ManagerRegistry
60
+ */
61
+ public function getRegistry ()
52
62
{
53
63
// this should never happen: we will have checked for the
54
64
// DoctrineBundle dependency before calling this
@@ -70,7 +80,7 @@ public function getEntityNamespace(): string
70
80
}
71
81
72
82
/**
73
- * @return MappingDriver|null
83
+ * @return MappingDriver|LegacyMappingDriver| null
74
84
*
75
85
* @throws \Exception
76
86
*/
@@ -85,7 +95,7 @@ public function getMappingDriverForClass(string $className)
85
95
86
96
$ metadataDriver = $ em ->getConfiguration ()->getMetadataDriverImpl ();
87
97
88
- if (!$ metadataDriver instanceof MappingDriverChain) {
98
+ if (!$ this -> isInstanceOf ( $ metadataDriver, MappingDriverChain::class) ) {
89
99
return $ metadataDriver ;
90
100
}
91
101
@@ -116,7 +126,7 @@ public function getEntitiesForAutocomplete(): array
116
126
}
117
127
118
128
/**
119
- * @return array|ClassMetadata
129
+ * @return array|ClassMetadata|LegacyClassMetadata
120
130
*/
121
131
public function getMetadata (string $ classOrNamespace = null , bool $ disconnected = false )
122
132
{
@@ -130,9 +140,11 @@ public function getMetadata(string $classOrNamespace = null, bool $disconnected
130
140
try {
131
141
$ loaded = $ cmf ->getAllMetadata ();
132
142
} catch (ORMMappingException $ e ) {
133
- $ loaded = $ cmf instanceof AbstractClassMetadataFactory ? $ cmf ->getLoadedMetadata () : [];
143
+ $ loaded = $ this ->isInstanceOf ($ cmf , AbstractClassMetadataFactory::class) ? $ cmf ->getLoadedMetadata () : [];
144
+ } catch (LegacyPersistenceMappingException $ e ) {
145
+ $ loaded = $ this ->isInstanceOf ($ cmf , AbstractClassMetadataFactory::class) ? $ cmf ->getLoadedMetadata () : [];
134
146
} catch (PersistenceMappingException $ e ) {
135
- $ loaded = $ cmf instanceof AbstractClassMetadataFactory ? $ cmf ->getLoadedMetadata () : [];
147
+ $ loaded = $ this -> isInstanceOf ( $ cmf, AbstractClassMetadataFactory::class) ? $ cmf ->getLoadedMetadata () : [];
136
148
}
137
149
138
150
$ cmf = new DisconnectedClassMetadataFactory ();
@@ -144,9 +156,9 @@ public function getMetadata(string $classOrNamespace = null, bool $disconnected
144
156
145
157
// Invalidating the cached AnnotationDriver::$classNames to find new Entity classes
146
158
$ metadataDriver = $ em ->getConfiguration ()->getMetadataDriverImpl ();
147
- if ($ metadataDriver instanceof MappingDriverChain) {
159
+ if ($ this -> isInstanceOf ( $ metadataDriver, MappingDriverChain::class) ) {
148
160
foreach ($ metadataDriver ->getDrivers () as $ driver ) {
149
- if ($ driver instanceof AnnotationDriver) {
161
+ if ($ this -> isInstanceOf ( $ driver, AnnotationDriver::class) ) {
150
162
$ classNames = (new \ReflectionObject ($ driver ))->getProperty ('classNames ' );
151
163
$ classNames ->setAccessible (true );
152
164
$ classNames ->setValue ($ driver , null );
@@ -181,7 +193,7 @@ public function createDoctrineDetails(string $entityClassName)
181
193
{
182
194
$ metadata = $ this ->getMetadata ($ entityClassName );
183
195
184
- if ($ metadata instanceof ClassMetadata) {
196
+ if ($ this -> isInstanceOf ( $ metadata, ClassMetadata::class) ) {
185
197
return new EntityDetails ($ metadata );
186
198
}
187
199
@@ -196,4 +208,15 @@ public function isClassAMappedEntity(string $className): bool
196
208
197
209
return (bool ) $ this ->getMetadata ($ className );
198
210
}
211
+
212
+ private function isInstanceOf ($ object , string $ class ): bool
213
+ {
214
+ if (!\is_object ($ object )) {
215
+ return false ;
216
+ }
217
+
218
+ $ legacyClass = str_replace ('Doctrine \\Persistence \\' , 'Doctrine \\Common \\Persistence \\' , $ class );
219
+
220
+ return $ object instanceof $ class || $ object instanceof $ legacyClass ;
221
+ }
199
222
}
0 commit comments