1 | <?php |
||
28 | abstract class AbstractClassMetadataFactory implements ClassMetadataFactory |
||
29 | { |
||
30 | /** |
||
31 | * Salt used by specific Object Manager implementation. |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $cacheSalt = '$CLASSMETADATA'; |
||
36 | |||
37 | /** |
||
38 | * @var \Doctrine\Common\Cache\Cache|null |
||
39 | */ |
||
40 | private $cacheDriver; |
||
41 | |||
42 | /** |
||
43 | * @var ClassMetadata[] |
||
44 | */ |
||
45 | private $loadedMetadata = []; |
||
46 | |||
47 | /** |
||
48 | * @var bool |
||
49 | */ |
||
50 | protected $initialized = false; |
||
51 | |||
52 | /** |
||
53 | * @var ReflectionService|null |
||
54 | */ |
||
55 | private $reflectionService = null; |
||
56 | |||
57 | /** |
||
58 | * Sets the cache driver used by the factory to cache ClassMetadata instances. |
||
59 | * |
||
60 | * @param \Doctrine\Common\Cache\Cache $cacheDriver |
||
61 | * |
||
62 | * @return void |
||
63 | */ |
||
64 | public function setCacheDriver(Cache $cacheDriver = null) |
||
68 | |||
69 | /** |
||
70 | * Gets the cache driver used by the factory to cache ClassMetadata instances. |
||
71 | * |
||
72 | * @return \Doctrine\Common\Cache\Cache|null |
||
73 | */ |
||
74 | public function getCacheDriver() |
||
78 | |||
79 | /** |
||
80 | * Returns an array of all the loaded metadata currently in memory. |
||
81 | * |
||
82 | * @return ClassMetadata[] |
||
83 | */ |
||
84 | public function getLoadedMetadata() |
||
88 | |||
89 | /** |
||
90 | * Forces the factory to load the metadata of all classes known to the underlying |
||
91 | * mapping driver. |
||
92 | * |
||
93 | * @return array The ClassMetadata instances of all mapped classes. |
||
94 | */ |
||
95 | public function getAllMetadata() |
||
110 | |||
111 | /** |
||
112 | * Lazy initialization of this stuff, especially the metadata driver, |
||
113 | * since these are not needed at all when a metadata cache is active. |
||
114 | * |
||
115 | * @return void |
||
116 | */ |
||
117 | abstract protected function initialize(); |
||
118 | |||
119 | /** |
||
120 | * Gets the fully qualified class-name from the namespace alias. |
||
121 | * |
||
122 | * @param string $namespaceAlias |
||
123 | * @param string $simpleClassName |
||
124 | * |
||
125 | * @return string |
||
126 | */ |
||
127 | abstract protected function getFqcnFromAlias($namespaceAlias, $simpleClassName); |
||
128 | |||
129 | /** |
||
130 | * Returns the mapping driver implementation. |
||
131 | * |
||
132 | * @return Driver\MappingDriver |
||
133 | */ |
||
134 | abstract protected function getDriver(); |
||
135 | |||
136 | /** |
||
137 | * Wakes up reflection after ClassMetadata gets unserialized from cache. |
||
138 | * |
||
139 | * @param ClassMetadata $class |
||
140 | * @param ReflectionService $reflService |
||
141 | * |
||
142 | * @return void |
||
143 | */ |
||
144 | abstract protected function wakeupReflection(ClassMetadata $class, ReflectionService $reflService); |
||
145 | |||
146 | /** |
||
147 | * Initializes Reflection after ClassMetadata was constructed. |
||
148 | * |
||
149 | * @param ClassMetadata $class |
||
150 | * @param ReflectionService $reflService |
||
151 | * |
||
152 | * @return void |
||
153 | */ |
||
154 | abstract protected function initializeReflection(ClassMetadata $class, ReflectionService $reflService); |
||
155 | |||
156 | /** |
||
157 | * Checks whether the class metadata is an entity. |
||
158 | * |
||
159 | * This method should return false for mapped superclasses or embedded classes. |
||
160 | * |
||
161 | * @param ClassMetadata $class |
||
162 | * |
||
163 | * @return boolean |
||
164 | */ |
||
165 | abstract protected function isEntity(ClassMetadata $class); |
||
166 | |||
167 | /** |
||
168 | * Gets the class metadata descriptor for a class. |
||
169 | * |
||
170 | * @param string $className The name of the class. |
||
171 | * |
||
172 | * @return ClassMetadata |
||
173 | * |
||
174 | * @throws ReflectionException |
||
175 | * @throws MappingException |
||
176 | */ |
||
177 | public function getMetadataFor($className) |
||
237 | |||
238 | /** |
||
239 | * Checks whether the factory has the metadata for a class loaded already. |
||
240 | * |
||
241 | * @param string $className |
||
242 | * |
||
243 | * @return boolean TRUE if the metadata of the class in question is already loaded, FALSE otherwise. |
||
244 | */ |
||
245 | public function hasMetadataFor($className) |
||
249 | |||
250 | /** |
||
251 | * Sets the metadata descriptor for a specific class. |
||
252 | * |
||
253 | * NOTE: This is only useful in very special cases, like when generating proxy classes. |
||
254 | * |
||
255 | * @param string $className |
||
256 | * @param ClassMetadata $class |
||
257 | * |
||
258 | * @return void |
||
259 | */ |
||
260 | public function setMetadataFor($className, $class) |
||
264 | |||
265 | /** |
||
266 | * Gets an array of parent classes for the given entity class. |
||
267 | * |
||
268 | * @param string $name |
||
269 | * |
||
270 | * @return array |
||
271 | */ |
||
272 | protected function getParentClasses($name) |
||
285 | |||
286 | /** |
||
287 | * Loads the metadata of the class in question and all it's ancestors whose metadata |
||
288 | * is still not loaded. |
||
289 | * |
||
290 | * Important: The class $name does not necessarily exist at this point here. |
||
291 | * Scenarios in a code-generation setup might have access to XML/YAML |
||
292 | * Mapping files without the actual PHP code existing here. That is why the |
||
293 | * {@see Doctrine\Common\Persistence\Mapping\ReflectionService} interface |
||
294 | * should be used for reflection. |
||
295 | * |
||
296 | * @param string $name The name of the class for which the metadata should |
||
297 | * get loaded. |
||
298 | * @param ClassMetadataBuildingContext $metadataBuildingContext |
||
299 | * |
||
300 | * @return array |
||
301 | */ |
||
302 | protected function loadMetadata(string $name, ClassMetadataBuildingContext $metadataBuildingContext) |
||
353 | |||
354 | /** |
||
355 | * Provides a fallback hook for loading metadata when loading failed due to reflection/mapping exceptions |
||
356 | * |
||
357 | * Override this method to implement a fallback strategy for failed metadata loading |
||
358 | * |
||
359 | * @param string $className |
||
360 | * @param ClassMetadataBuildingContext $metadataBuildingContext |
||
361 | * |
||
362 | * @return \Doctrine\ORM\Mapping\ClassMetadata|null |
||
363 | */ |
||
364 | protected function onNotFoundMetadata($className, ClassMetadataBuildingContext $metadataBuildingContext) |
||
368 | |||
369 | /** |
||
370 | * Actually loads the metadata from the underlying metadata. |
||
371 | * |
||
372 | * @param ClassMetadata $class |
||
373 | * @param ClassMetadataBuildingContext $metadataBuildingContext |
||
374 | * @param bool $rootEntityFound |
||
375 | * |
||
376 | * @return void |
||
377 | */ |
||
378 | abstract protected function doLoadMetadata( |
||
383 | |||
384 | /** |
||
385 | * Creates a new ClassMetadata instance for the given class name. |
||
386 | * |
||
387 | * @param string $className |
||
388 | * @param ClassMetadataBuildingContext $metadataBuildingContext |
||
389 | * |
||
390 | * @return ClassMetadata |
||
391 | */ |
||
392 | abstract protected function newClassMetadataInstance( |
||
396 | |||
397 | /** |
||
398 | * Creates a new ClassMetadataBuildingContext instance. |
||
399 | * |
||
400 | * @return ClassMetadataBuildingContext |
||
401 | */ |
||
402 | abstract protected function newClassMetadataBuildingContext() : ClassMetadataBuildingContext; |
||
403 | |||
404 | /** |
||
405 | * {@inheritDoc} |
||
406 | */ |
||
407 | public function isTransient($class) : bool |
||
422 | |||
423 | /** |
||
424 | * Sets the reflectionService. |
||
425 | * |
||
426 | * @param ReflectionService $reflectionService |
||
427 | * |
||
428 | * @return void |
||
429 | */ |
||
430 | public function setReflectionService(ReflectionService $reflectionService) |
||
434 | |||
435 | /** |
||
436 | * Gets the reflection service associated with this metadata factory. |
||
437 | * |
||
438 | * @return ReflectionService |
||
439 | */ |
||
440 | public function getReflectionService() |
||
448 | } |
||
449 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: