1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Persistence\Mapping; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Cache\Cache; |
8
|
|
|
use Doctrine\Persistence\Mapping\Driver\MappingDriver; |
9
|
|
|
use Doctrine\Persistence\Proxy; |
10
|
|
|
use ReflectionException; |
11
|
|
|
use function array_reverse; |
12
|
|
|
use function array_unshift; |
13
|
|
|
use function explode; |
14
|
|
|
use function strpos; |
15
|
|
|
use function strrpos; |
16
|
|
|
use function substr; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The ClassMetadataFactory is used to create ClassMetadata objects that contain all the |
20
|
|
|
* metadata mapping informations of a class which describes how a class should be mapped |
21
|
|
|
* to a relational database. |
22
|
|
|
* |
23
|
|
|
* This class was abstracted from the ORM ClassMetadataFactory. |
24
|
|
|
*/ |
25
|
|
|
abstract class AbstractClassMetadataFactory implements ClassMetadataFactory |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Salt used by specific Object Manager implementation. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $cacheSalt = '$CLASSMETADATA'; |
33
|
|
|
|
34
|
|
|
/** @var Cache|null */ |
35
|
|
|
private $cacheDriver; |
36
|
|
|
|
37
|
|
|
/** @var ClassMetadata[] */ |
38
|
|
|
private $loadedMetadata = []; |
39
|
|
|
|
40
|
|
|
/** @var bool */ |
41
|
|
|
protected $initialized = false; |
42
|
|
|
|
43
|
|
|
/** @var ReflectionService|null */ |
44
|
|
|
private $reflectionService = null; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Sets the cache driver used by the factory to cache ClassMetadata instances. |
48
|
|
|
*/ |
49
|
4 |
|
public function setCacheDriver(?Cache $cacheDriver = null) : void |
50
|
|
|
{ |
51
|
4 |
|
$this->cacheDriver = $cacheDriver; |
52
|
4 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Gets the cache driver used by the factory to cache ClassMetadata instances. |
56
|
|
|
*/ |
57
|
1 |
|
public function getCacheDriver() : ?Cache |
58
|
|
|
{ |
59
|
1 |
|
return $this->cacheDriver; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Returns an array of all the loaded metadata currently in memory. |
64
|
|
|
* |
65
|
|
|
* @return ClassMetadata[] |
66
|
|
|
*/ |
67
|
|
|
public function getLoadedMetadata() : array |
68
|
|
|
{ |
69
|
|
|
return $this->loadedMetadata; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Forces the factory to load the metadata of all classes known to the underlying |
74
|
|
|
* mapping driver. |
75
|
|
|
* |
76
|
|
|
* @return ClassMetadata[] The ClassMetadata instances of all mapped classes. |
77
|
|
|
*/ |
78
|
|
|
public function getAllMetadata() : array |
79
|
|
|
{ |
80
|
|
|
if (! $this->initialized) { |
81
|
|
|
$this->initialize(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$driver = $this->getDriver(); |
85
|
|
|
$metadata = []; |
86
|
|
|
foreach ($driver->getAllClassNames() as $className) { |
87
|
|
|
$metadata[] = $this->getMetadataFor($className); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $metadata; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Lazy initialization of this stuff, especially the metadata driver, |
95
|
|
|
* since these are not needed at all when a metadata cache is active. |
96
|
|
|
*/ |
97
|
|
|
abstract protected function initialize() : void; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Gets the fully qualified class-name from the namespace alias. |
101
|
|
|
*/ |
102
|
|
|
abstract protected function getFqcnFromAlias( |
103
|
|
|
string $namespaceAlias, |
104
|
|
|
string $simpleClassName |
105
|
|
|
) : string; |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Returns the mapping driver implementation. |
109
|
|
|
*/ |
110
|
|
|
abstract protected function getDriver() : MappingDriver; |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Wakes up reflection after ClassMetadata gets unserialized from cache. |
114
|
|
|
*/ |
115
|
|
|
abstract protected function wakeupReflection( |
116
|
|
|
ClassMetadata $class, |
117
|
|
|
ReflectionService $reflService |
118
|
|
|
) : void; |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Initializes Reflection after ClassMetadata was constructed. |
122
|
|
|
*/ |
123
|
|
|
abstract protected function initializeReflection( |
124
|
|
|
ClassMetadata $class, |
125
|
|
|
ReflectionService $reflService |
126
|
|
|
) : void; |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Checks whether the class metadata is an entity. |
130
|
|
|
* |
131
|
|
|
* This method should return false for mapped superclasses or embedded classes. |
132
|
|
|
*/ |
133
|
|
|
abstract protected function isEntity(ClassMetadata $class) : bool; |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Gets the class metadata descriptor for a class. |
137
|
|
|
* |
138
|
|
|
* @param string $className The name of the class. |
139
|
|
|
* |
140
|
|
|
* @throws ReflectionException |
141
|
|
|
* @throws MappingException |
142
|
|
|
*/ |
143
|
10 |
|
public function getMetadataFor(string $className) : ClassMetadata |
144
|
|
|
{ |
145
|
10 |
|
if (isset($this->loadedMetadata[$className])) { |
146
|
|
|
return $this->loadedMetadata[$className]; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
// Check for namespace alias |
150
|
10 |
|
if (strpos($className, ':') !== false) { |
151
|
2 |
|
[$namespaceAlias, $simpleClassName] = explode(':', $className, 2); |
152
|
|
|
|
153
|
2 |
|
$realClassName = $this->getFqcnFromAlias($namespaceAlias, $simpleClassName); |
154
|
|
|
} else { |
155
|
8 |
|
$realClassName = $this->getRealClass($className); |
156
|
|
|
} |
157
|
|
|
|
158
|
10 |
|
if (isset($this->loadedMetadata[$realClassName])) { |
159
|
|
|
// We do not have the alias name in the map, include it |
160
|
|
|
return $this->loadedMetadata[$className] = $this->loadedMetadata[$realClassName]; |
161
|
|
|
} |
162
|
|
|
|
163
|
10 |
|
$loadingException = null; |
|
|
|
|
164
|
|
|
|
165
|
|
|
try { |
166
|
10 |
|
if ($this->cacheDriver !== null) { |
167
|
3 |
|
$cached = $this->cacheDriver->fetch($realClassName . $this->cacheSalt); |
168
|
|
|
|
169
|
3 |
|
if ($cached instanceof ClassMetadata) { |
170
|
1 |
|
$this->loadedMetadata[$realClassName] = $cached; |
171
|
|
|
|
172
|
1 |
|
$this->wakeupReflection($cached, $this->getReflectionService()); |
173
|
|
|
} else { |
174
|
3 |
|
foreach ($this->loadMetadata($realClassName) as $loadedClassName) { |
175
|
1 |
|
$this->cacheDriver->save( |
176
|
1 |
|
$loadedClassName . $this->cacheSalt, |
177
|
1 |
|
$this->loadedMetadata[$loadedClassName], |
178
|
|
|
0 |
179
|
|
|
); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
} else { |
183
|
9 |
|
$this->loadMetadata($realClassName); |
184
|
|
|
} |
185
|
5 |
|
} catch (MappingException $loadingException) { |
186
|
5 |
|
$fallbackMetadataResponse = $this->onNotFoundMetadata($realClassName); |
|
|
|
|
187
|
|
|
|
188
|
5 |
|
if ($fallbackMetadataResponse === null) { |
|
|
|
|
189
|
3 |
|
throw $loadingException; |
190
|
|
|
} |
191
|
|
|
|
192
|
2 |
|
$this->loadedMetadata[$realClassName] = $fallbackMetadataResponse; |
193
|
|
|
} |
194
|
|
|
|
195
|
7 |
|
if ($className !== $realClassName) { |
196
|
|
|
// We do not have the alias name in the map, include it |
197
|
1 |
|
$this->loadedMetadata[$className] = $this->loadedMetadata[$realClassName]; |
198
|
|
|
} |
199
|
|
|
|
200
|
7 |
|
return $this->loadedMetadata[$className]; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Checks whether the factory has the metadata for a class loaded already. |
205
|
|
|
* |
206
|
|
|
* @return bool TRUE if the metadata of the class in question is already loaded, FALSE otherwise. |
207
|
|
|
*/ |
208
|
3 |
|
public function hasMetadataFor(string $className) : bool |
209
|
|
|
{ |
210
|
3 |
|
return isset($this->loadedMetadata[$className]); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Sets the metadata descriptor for a specific class. |
215
|
|
|
* |
216
|
|
|
* NOTE: This is only useful in very special cases, like when generating proxy classes. |
217
|
|
|
*/ |
218
|
|
|
public function setMetadataFor(string $className, ClassMetadata $class) : void |
219
|
|
|
{ |
220
|
|
|
$this->loadedMetadata[$className] = $class; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Gets an array of parent classes for the given entity class. |
225
|
|
|
* |
226
|
|
|
* @return string[] |
227
|
|
|
*/ |
228
|
9 |
|
protected function getParentClasses(string $name) : array |
229
|
|
|
{ |
230
|
|
|
// Collect parent classes, ignoring transient (not-mapped) classes. |
231
|
9 |
|
$parentClasses = []; |
|
|
|
|
232
|
|
|
|
233
|
9 |
|
$parentClasses = $this->getReflectionService() |
234
|
9 |
|
->getParentClasses($name); |
235
|
|
|
|
236
|
4 |
|
foreach (array_reverse($parentClasses) as $parentClass) { |
237
|
3 |
|
if ($this->getDriver()->isTransient($parentClass)) { |
238
|
|
|
continue; |
239
|
|
|
} |
240
|
|
|
|
241
|
3 |
|
$parentClasses[] = $parentClass; |
242
|
|
|
} |
243
|
|
|
|
244
|
4 |
|
return $parentClasses; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Loads the metadata of the class in question and all it's ancestors whose metadata |
249
|
|
|
* is still not loaded. |
250
|
|
|
* |
251
|
|
|
* Important: The class $name does not necessarily exist at this point here. |
252
|
|
|
* Scenarios in a code-generation setup might have access to XML/YAML |
253
|
|
|
* Mapping files without the actual PHP code existing here. That is why the |
254
|
|
|
* {@see Doctrine\Persistence\Mapping\ReflectionService} interface |
255
|
|
|
* should be used for reflection. |
256
|
|
|
* |
257
|
|
|
* @param string $name The name of the class for which the metadata should get loaded. |
258
|
|
|
* |
259
|
|
|
* @return string[] |
260
|
|
|
*/ |
261
|
9 |
|
protected function loadMetadata(string $name) : array |
262
|
|
|
{ |
263
|
9 |
|
if (! $this->initialized) { |
264
|
9 |
|
$this->initialize(); |
265
|
|
|
} |
266
|
|
|
|
267
|
9 |
|
$loaded = []; |
268
|
|
|
|
269
|
9 |
|
$parentClasses = $this->getParentClasses($name); |
270
|
4 |
|
$parentClasses[] = $name; |
271
|
|
|
|
272
|
|
|
// Move down the hierarchy of parent classes, starting from the topmost class |
273
|
4 |
|
$parent = null; |
274
|
4 |
|
$rootEntityFound = false; |
275
|
4 |
|
$visited = []; |
276
|
4 |
|
$reflService = $this->getReflectionService(); |
277
|
|
|
|
278
|
4 |
|
foreach ($parentClasses as $className) { |
279
|
4 |
|
if (isset($this->loadedMetadata[$className])) { |
280
|
3 |
|
$parent = $this->loadedMetadata[$className]; |
281
|
|
|
|
282
|
3 |
|
if ($this->isEntity($parent)) { |
283
|
3 |
|
$rootEntityFound = true; |
284
|
|
|
|
285
|
3 |
|
array_unshift($visited, $className); |
286
|
|
|
} |
287
|
|
|
|
288
|
3 |
|
continue; |
289
|
|
|
} |
290
|
|
|
|
291
|
4 |
|
$class = $this->newClassMetadataInstance($className); |
292
|
4 |
|
$this->initializeReflection($class, $reflService); |
293
|
|
|
|
294
|
4 |
|
$this->doLoadMetadata($class, $parent, $rootEntityFound, $visited); |
295
|
|
|
|
296
|
4 |
|
$this->loadedMetadata[$className] = $class; |
297
|
|
|
|
298
|
4 |
|
$parent = $class; |
299
|
|
|
|
300
|
4 |
|
if ($this->isEntity($class)) { |
301
|
4 |
|
$rootEntityFound = true; |
302
|
|
|
|
303
|
4 |
|
array_unshift($visited, $className); |
304
|
|
|
} |
305
|
|
|
|
306
|
4 |
|
$this->wakeupReflection($class, $reflService); |
307
|
|
|
|
308
|
4 |
|
$loaded[] = $className; |
309
|
|
|
} |
310
|
|
|
|
311
|
4 |
|
return $loaded; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Provides a fallback hook for loading metadata when loading failed due to reflection/mapping exceptions |
316
|
|
|
* |
317
|
|
|
* Override this method to implement a fallback strategy for failed metadata loading |
318
|
|
|
*/ |
319
|
|
|
protected function onNotFoundMetadata(string $className) : ?ClassMetadata |
320
|
|
|
{ |
321
|
|
|
return null; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Actually loads the metadata from the underlying metadata. |
326
|
|
|
* |
327
|
|
|
* @param string[] $nonSuperclassParents All parent class names |
328
|
|
|
* that are not marked as mapped superclasses. |
329
|
|
|
*/ |
330
|
|
|
abstract protected function doLoadMetadata( |
331
|
|
|
ClassMetadata $class, |
332
|
|
|
?ClassMetadata $parent, |
333
|
|
|
bool $rootEntityFound, |
334
|
|
|
array $nonSuperclassParents |
335
|
|
|
) : void; |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* Creates a new ClassMetadata instance for the given class name. |
339
|
|
|
*/ |
340
|
|
|
abstract protected function newClassMetadataInstance(string $className) : ClassMetadata; |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* {@inheritDoc} |
344
|
|
|
*/ |
345
|
|
|
public function isTransient(string $class) : bool |
346
|
|
|
{ |
347
|
|
|
if (! $this->initialized) { |
348
|
|
|
$this->initialize(); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
// Check for namespace alias |
352
|
|
|
if (strpos($class, ':') !== false) { |
353
|
|
|
[$namespaceAlias, $simpleClassName] = explode(':', $class, 2); |
354
|
|
|
|
355
|
|
|
$class = $this->getFqcnFromAlias($namespaceAlias, $simpleClassName); |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
return $this->getDriver()->isTransient($class); |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* Sets the reflectionService. |
363
|
|
|
*/ |
364
|
|
|
public function setReflectionService(ReflectionService $reflectionService) : void |
365
|
|
|
{ |
366
|
|
|
$this->reflectionService = $reflectionService; |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
/** |
370
|
|
|
* Gets the reflection service associated with this metadata factory. |
371
|
|
|
*/ |
372
|
10 |
|
public function getReflectionService() : ReflectionService |
373
|
|
|
{ |
374
|
10 |
|
if ($this->reflectionService === null) { |
375
|
10 |
|
$this->reflectionService = new RuntimeReflectionService(); |
376
|
|
|
} |
377
|
|
|
|
378
|
10 |
|
return $this->reflectionService; |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
/** |
382
|
|
|
* Gets the real class name of a class name that could be a proxy. |
383
|
|
|
*/ |
384
|
8 |
|
private function getRealClass(string $class) : string |
385
|
|
|
{ |
386
|
8 |
|
$pos = strrpos($class, '\\' . Proxy::MARKER . '\\'); |
387
|
|
|
|
388
|
8 |
|
if ($pos === false) { |
389
|
8 |
|
return $class; |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
return substr($class, $pos + Proxy::MARKER_LENGTH + 2); |
393
|
|
|
} |
394
|
|
|
} |
395
|
|
|
|