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 | protected $reflectionService; |
||
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) : void |
||
68 | |||
69 | /** |
||
70 | * Gets the cache driver used by the factory to cache ClassMetadata instances. |
||
71 | * |
||
72 | * @return Cache|null |
||
73 | */ |
||
74 | public function getCacheDriver() : ?Cache |
||
78 | |||
79 | /** |
||
80 | * Returns an array of all the loaded metadata currently in memory. |
||
81 | * |
||
82 | * @return ClassMetadata[] |
||
83 | */ |
||
84 | public function getLoadedMetadata() : array |
||
88 | |||
89 | /** |
||
90 | * Sets the reflectionService. |
||
91 | * |
||
92 | * @param ReflectionService $reflectionService |
||
93 | * |
||
94 | * @return void |
||
95 | */ |
||
96 | public function setReflectionService(ReflectionService $reflectionService) : void |
||
100 | |||
101 | /** |
||
102 | * Gets the reflection service associated with this metadata factory. |
||
103 | * |
||
104 | * @return ReflectionService |
||
105 | */ |
||
106 | public function getReflectionService() : ReflectionService |
||
114 | |||
115 | /** |
||
116 | * Checks whether the factory has the metadata for a class loaded already. |
||
117 | * |
||
118 | * @param string $className |
||
119 | * |
||
120 | * @return bool TRUE if the metadata of the class in question is already loaded, FALSE otherwise. |
||
121 | */ |
||
122 | public function hasMetadataFor($className) : bool |
||
126 | |||
127 | /** |
||
128 | * Sets the metadata descriptor for a specific class. |
||
129 | * |
||
130 | * NOTE: This is only useful in very special cases, like when generating proxy classes. |
||
131 | * |
||
132 | * @param string $className |
||
133 | * @param ClassMetadata $class |
||
134 | * |
||
135 | * @return void |
||
136 | */ |
||
137 | public function setMetadataFor($className, $class) : void |
||
141 | |||
142 | /** |
||
143 | * Forces the factory to load the metadata of all classes known to the underlying |
||
144 | * mapping driver. |
||
145 | * |
||
146 | * @return array The ClassMetadata instances of all mapped classes. |
||
147 | * |
||
148 | * @throws \InvalidArgumentException |
||
149 | * @throws \ReflectionException |
||
150 | * @throws MappingException |
||
151 | */ |
||
152 | public function getAllMetadata() : array |
||
167 | |||
168 | /** |
||
169 | * Gets the class metadata descriptor for a class. |
||
170 | * |
||
171 | * @param string $className The name of the class. |
||
172 | * |
||
173 | * @return ClassMetadata |
||
174 | * |
||
175 | * @throws \InvalidArgumentException |
||
176 | * @throws ReflectionException |
||
177 | * @throws CommonMappingException |
||
178 | */ |
||
179 | public function getMetadataFor($className) : ClassMetadata |
||
230 | |||
231 | /** |
||
232 | * Loads the metadata of the class in question and all it's ancestors whose metadata |
||
233 | * is still not loaded. |
||
234 | * |
||
235 | * Important: The class $name does not necessarily exist at this point here. |
||
236 | * Scenarios in a code-generation setup might have access to XML/YAML |
||
237 | * Mapping files without the actual PHP code existing here. That is why the |
||
238 | * {@see Doctrine\Common\Persistence\Mapping\ReflectionService} interface |
||
239 | * should be used for reflection. |
||
240 | * |
||
241 | * @param string $name The name of the class for which the metadata should |
||
242 | * get loaded. |
||
243 | * @param ClassMetadataBuildingContext $metadataBuildingContext |
||
244 | * |
||
245 | * @return array<ClassMetadata> |
||
246 | * |
||
247 | * @throws \InvalidArgumentException |
||
248 | */ |
||
249 | protected function loadMetadata(string $name, ClassMetadataBuildingContext $metadataBuildingContext) : array |
||
281 | |||
282 | /** |
||
283 | * {@inheritDoc} |
||
284 | */ |
||
285 | public function isTransient($className) : bool |
||
293 | |||
294 | /** |
||
295 | * Gets an array of parent classes for the given entity class. |
||
296 | * |
||
297 | * @param string $name |
||
298 | * |
||
299 | * @return array |
||
300 | * |
||
301 | * @throws \InvalidArgumentException |
||
302 | */ |
||
303 | protected function getParentClasses($name) : array |
||
316 | |||
317 | /** |
||
318 | * Provides a fallback hook for loading metadata when loading failed due to reflection/mapping exceptions |
||
319 | * |
||
320 | * Override this method to implement a fallback strategy for failed metadata loading |
||
321 | * |
||
322 | * @param string $className |
||
323 | * @param ClassMetadataBuildingContext $metadataBuildingContext |
||
324 | * |
||
325 | * @return \Doctrine\ORM\Mapping\ClassMetadata|null |
||
326 | */ |
||
327 | protected function onNotFoundMetadata( |
||
334 | |||
335 | /** |
||
336 | * @param string $className |
||
337 | * |
||
338 | * @return string |
||
339 | */ |
||
340 | private function normalizeClassName(string $className) : string |
||
350 | |||
351 | /** |
||
352 | * Lazy initialization of this stuff, especially the metadata driver, |
||
353 | * since these are not needed at all when a metadata cache is active. |
||
354 | * |
||
355 | * @return void |
||
356 | */ |
||
357 | abstract protected function initialize() : void; |
||
358 | |||
359 | /** |
||
360 | * Gets the fully qualified class-name from the namespace alias. |
||
361 | * |
||
362 | * @param string $namespaceAlias |
||
363 | * @param string $simpleClassName |
||
364 | * |
||
365 | * @return string |
||
366 | */ |
||
367 | abstract protected function getFqcnFromAlias($namespaceAlias, $simpleClassName) : string; |
||
368 | |||
369 | /** |
||
370 | * Returns the mapping driver implementation. |
||
371 | * |
||
372 | * @return Driver\MappingDriver |
||
373 | */ |
||
374 | abstract protected function getDriver() : Driver\MappingDriver; |
||
375 | |||
376 | /** |
||
377 | * Checks whether the class metadata is an entity. |
||
378 | * |
||
379 | * This method should return false for mapped superclasses or embedded classes. |
||
380 | * |
||
381 | * @param ClassMetadata $class |
||
382 | * |
||
383 | * @return bool |
||
384 | */ |
||
385 | abstract protected function isEntity(ClassMetadata $class) : bool; |
||
386 | |||
387 | /** |
||
388 | * Creates a new ClassMetadata instance for the given class name. |
||
389 | * |
||
390 | * @param string $className |
||
391 | * @param ClassMetadata|null $parent |
||
392 | * @param ClassMetadataBuildingContext $metadataBuildingContext |
||
393 | * |
||
394 | * @return ClassMetadata |
||
395 | */ |
||
396 | abstract protected function doLoadMetadata( |
||
401 | |||
402 | /** |
||
403 | * Creates a new ClassMetadataBuildingContext instance. |
||
404 | * |
||
405 | * @return ClassMetadataBuildingContext |
||
406 | */ |
||
407 | abstract protected function newClassMetadataBuildingContext() : ClassMetadataBuildingContext; |
||
408 | } |
||
409 |
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: