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