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