1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Common\Persistence\Mapping; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Cache\Cache; |
6
|
|
|
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver; |
7
|
|
|
use Doctrine\Common\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 |
|
list($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
|
1 |
|
$this->loadedMetadata[$loadedClassName], |
187
|
2 |
|
null |
188
|
|
|
); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
} else { |
192
|
9 |
|
$this->loadMetadata($realClassName); |
193
|
|
|
} |
194
|
5 |
|
} catch (MappingException $loadingException) { |
195
|
5 |
|
$fallbackMetadataResponse = $this->onNotFoundMetadata($realClassName); |
|
|
|
|
196
|
|
|
|
197
|
5 |
|
if (! $fallbackMetadataResponse) { |
|
|
|
|
198
|
3 |
|
throw $loadingException; |
199
|
|
|
} |
200
|
|
|
|
201
|
2 |
|
$this->loadedMetadata[$realClassName] = $fallbackMetadataResponse; |
202
|
|
|
} |
203
|
|
|
|
204
|
7 |
|
if ($className !== $realClassName) { |
205
|
|
|
// We do not have the alias name in the map, include it |
206
|
1 |
|
$this->loadedMetadata[$className] = $this->loadedMetadata[$realClassName]; |
207
|
|
|
} |
208
|
|
|
|
209
|
7 |
|
return $this->loadedMetadata[$className]; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Checks whether the factory has the metadata for a class loaded already. |
214
|
|
|
* |
215
|
|
|
* @param string $className |
216
|
|
|
* |
217
|
|
|
* @return bool TRUE if the metadata of the class in question is already loaded, FALSE otherwise. |
218
|
|
|
*/ |
219
|
3 |
|
public function hasMetadataFor($className) |
220
|
|
|
{ |
221
|
3 |
|
return isset($this->loadedMetadata[$className]); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Sets the metadata descriptor for a specific class. |
226
|
|
|
* |
227
|
|
|
* NOTE: This is only useful in very special cases, like when generating proxy classes. |
228
|
|
|
* |
229
|
|
|
* @param string $className |
230
|
|
|
* @param ClassMetadata $class |
231
|
|
|
* |
232
|
|
|
* @return void |
233
|
|
|
*/ |
234
|
|
|
public function setMetadataFor($className, $class) |
235
|
|
|
{ |
236
|
|
|
$this->loadedMetadata[$className] = $class; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Gets an array of parent classes for the given entity class. |
241
|
|
|
* |
242
|
|
|
* @param string $name |
243
|
|
|
* |
244
|
|
|
* @return string[] |
245
|
|
|
*/ |
246
|
9 |
|
protected function getParentClasses($name) |
247
|
|
|
{ |
248
|
|
|
// Collect parent classes, ignoring transient (not-mapped) classes. |
249
|
9 |
|
$parentClasses = []; |
250
|
|
|
|
251
|
9 |
|
foreach (array_reverse($this->getReflectionService()->getParentClasses($name)) as $parentClass) { |
252
|
3 |
|
if ($this->getDriver()->isTransient($parentClass)) { |
253
|
|
|
continue; |
254
|
|
|
} |
255
|
|
|
|
256
|
3 |
|
$parentClasses[] = $parentClass; |
257
|
|
|
} |
258
|
|
|
|
259
|
4 |
|
return $parentClasses; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Loads the metadata of the class in question and all it's ancestors whose metadata |
264
|
|
|
* is still not loaded. |
265
|
|
|
* |
266
|
|
|
* Important: The class $name does not necessarily exist at this point here. |
267
|
|
|
* Scenarios in a code-generation setup might have access to XML/YAML |
268
|
|
|
* Mapping files without the actual PHP code existing here. That is why the |
269
|
|
|
* {@see Doctrine\Common\Persistence\Mapping\ReflectionService} interface |
270
|
|
|
* should be used for reflection. |
271
|
|
|
* |
272
|
|
|
* @param string $name The name of the class for which the metadata should get loaded. |
273
|
|
|
* |
274
|
|
|
* @return string[] |
275
|
|
|
*/ |
276
|
9 |
|
protected function loadMetadata($name) |
277
|
|
|
{ |
278
|
9 |
|
if (! $this->initialized) { |
279
|
9 |
|
$this->initialize(); |
280
|
|
|
} |
281
|
|
|
|
282
|
9 |
|
$loaded = []; |
283
|
|
|
|
284
|
9 |
|
$parentClasses = $this->getParentClasses($name); |
285
|
4 |
|
$parentClasses[] = $name; |
286
|
|
|
|
287
|
|
|
// Move down the hierarchy of parent classes, starting from the topmost class |
288
|
4 |
|
$parent = null; |
289
|
4 |
|
$rootEntityFound = false; |
290
|
4 |
|
$visited = []; |
291
|
4 |
|
$reflService = $this->getReflectionService(); |
292
|
4 |
|
foreach ($parentClasses as $className) { |
293
|
4 |
|
if (isset($this->loadedMetadata[$className])) { |
294
|
|
|
$parent = $this->loadedMetadata[$className]; |
295
|
|
|
if ($this->isEntity($parent)) { |
296
|
|
|
$rootEntityFound = true; |
297
|
|
|
array_unshift($visited, $className); |
298
|
|
|
} |
299
|
|
|
continue; |
300
|
|
|
} |
301
|
|
|
|
302
|
4 |
|
$class = $this->newClassMetadataInstance($className); |
303
|
4 |
|
$this->initializeReflection($class, $reflService); |
304
|
|
|
|
305
|
4 |
|
$this->doLoadMetadata($class, $parent, $rootEntityFound, $visited); |
306
|
|
|
|
307
|
4 |
|
$this->loadedMetadata[$className] = $class; |
308
|
|
|
|
309
|
4 |
|
$parent = $class; |
310
|
|
|
|
311
|
4 |
|
if ($this->isEntity($class)) { |
312
|
4 |
|
$rootEntityFound = true; |
313
|
4 |
|
array_unshift($visited, $className); |
314
|
|
|
} |
315
|
|
|
|
316
|
4 |
|
$this->wakeupReflection($class, $reflService); |
317
|
|
|
|
318
|
4 |
|
$loaded[] = $className; |
319
|
|
|
} |
320
|
|
|
|
321
|
4 |
|
return $loaded; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Provides a fallback hook for loading metadata when loading failed due to reflection/mapping exceptions |
326
|
|
|
* |
327
|
|
|
* Override this method to implement a fallback strategy for failed metadata loading |
328
|
|
|
* |
329
|
|
|
* @param string $className |
330
|
|
|
* |
331
|
|
|
* @return ClassMetadata|null |
332
|
|
|
*/ |
333
|
|
|
protected function onNotFoundMetadata($className) |
334
|
|
|
{ |
335
|
|
|
return null; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Actually loads the metadata from the underlying metadata. |
340
|
|
|
* |
341
|
|
|
* @param ClassMetadata $class |
342
|
|
|
* @param ClassMetadata|null $parent |
343
|
|
|
* @param bool $rootEntityFound |
344
|
|
|
* @param string[] $nonSuperclassParents All parent class names |
345
|
|
|
* that are not marked as mapped superclasses. |
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
|
|
|
list($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
|
|
|
|