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; |
||
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 | * 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 | * @throws \InvalidArgumentException |
||
96 | * @throws \ReflectionException |
||
97 | * @throws MappingException |
||
98 | */ |
||
99 | public function getAllMetadata() : array |
||
114 | |||
115 | /** |
||
116 | * Lazy initialization of this stuff, especially the metadata driver, |
||
117 | * since these are not needed at all when a metadata cache is active. |
||
118 | * |
||
119 | * @return void |
||
120 | */ |
||
121 | abstract protected function initialize() : void; |
||
122 | |||
123 | /** |
||
124 | * Gets the fully qualified class-name from the namespace alias. |
||
125 | * |
||
126 | * @param string $namespaceAlias |
||
127 | * @param string $simpleClassName |
||
128 | * |
||
129 | * @return string |
||
130 | */ |
||
131 | abstract protected function getFqcnFromAlias($namespaceAlias, $simpleClassName) : string; |
||
132 | |||
133 | /** |
||
134 | * Returns the mapping driver implementation. |
||
135 | * |
||
136 | * @return Driver\MappingDriver |
||
137 | */ |
||
138 | abstract protected function getDriver() : Driver\MappingDriver; |
||
139 | |||
140 | /** |
||
141 | * Wakes up reflection after ClassMetadata gets unserialized from cache. |
||
142 | * |
||
143 | * @param ClassMetadata $class |
||
144 | * @param ReflectionService $reflService |
||
145 | * |
||
146 | * @return void |
||
147 | */ |
||
148 | abstract protected function wakeupReflection(ClassMetadata $class, ReflectionService $reflService) : void; |
||
149 | |||
150 | /** |
||
151 | * Initializes Reflection after ClassMetadata was constructed. |
||
152 | * |
||
153 | * @param ClassMetadata $class |
||
154 | * @param ReflectionService $reflService |
||
155 | * |
||
156 | * @return void |
||
157 | */ |
||
158 | abstract protected function initializeReflection(ClassMetadata $class, ReflectionService $reflService) : void; |
||
159 | |||
160 | /** |
||
161 | * Checks whether the class metadata is an entity. |
||
162 | * |
||
163 | * This method should return false for mapped superclasses or embedded classes. |
||
164 | * |
||
165 | * @param ClassMetadata $class |
||
166 | * |
||
167 | * @return bool |
||
168 | */ |
||
169 | abstract protected function isEntity(ClassMetadata $class) : bool; |
||
170 | |||
171 | /** |
||
172 | * Gets the class metadata descriptor for a class. |
||
173 | * |
||
174 | * @param string $className The name of the class. |
||
175 | * |
||
176 | * @return ClassMetadata |
||
177 | * |
||
178 | * @throws \InvalidArgumentException |
||
179 | * @throws ReflectionException |
||
180 | * @throws MappingException |
||
181 | */ |
||
182 | public function getMetadataFor($className) : ClassMetadata |
||
242 | |||
243 | /** |
||
244 | * Checks whether the factory has the metadata for a class loaded already. |
||
245 | * |
||
246 | * @param string $className |
||
247 | * |
||
248 | * @return bool TRUE if the metadata of the class in question is already loaded, FALSE otherwise. |
||
249 | */ |
||
250 | public function hasMetadataFor($className) : bool |
||
254 | |||
255 | /** |
||
256 | * Sets the metadata descriptor for a specific class. |
||
257 | * |
||
258 | * NOTE: This is only useful in very special cases, like when generating proxy classes. |
||
259 | * |
||
260 | * @param string $className |
||
261 | * @param ClassMetadata $class |
||
262 | * |
||
263 | * @return void |
||
264 | */ |
||
265 | public function setMetadataFor($className, $class) : void |
||
269 | |||
270 | /** |
||
271 | * Gets an array of parent classes for the given entity class. |
||
272 | * |
||
273 | * @param string $name |
||
274 | * |
||
275 | * @return array |
||
276 | * |
||
277 | * @throws \InvalidArgumentException |
||
278 | */ |
||
279 | protected function getParentClasses($name) : array |
||
292 | |||
293 | /** |
||
294 | * Loads the metadata of the class in question and all it's ancestors whose metadata |
||
295 | * is still not loaded. |
||
296 | * |
||
297 | * Important: The class $name does not necessarily exist at this point here. |
||
298 | * Scenarios in a code-generation setup might have access to XML/YAML |
||
299 | * Mapping files without the actual PHP code existing here. That is why the |
||
300 | * {@see Doctrine\Common\Persistence\Mapping\ReflectionService} interface |
||
301 | * should be used for reflection. |
||
302 | * |
||
303 | * @param string $name The name of the class for which the metadata should |
||
304 | * get loaded. |
||
305 | * @param ClassMetadataBuildingContext $metadataBuildingContext |
||
306 | * |
||
307 | * @return array |
||
308 | * |
||
309 | * @throws \InvalidArgumentException |
||
310 | */ |
||
311 | protected function loadMetadata(string $name, ClassMetadataBuildingContext $metadataBuildingContext) : array |
||
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( |
||
371 | |||
372 | /** |
||
373 | * Actually loads the metadata from the underlying metadata. |
||
374 | * |
||
375 | * @param ClassMetadata $class |
||
376 | * @param ClassMetadataBuildingContext $metadataBuildingContext |
||
377 | * |
||
378 | * @return void |
||
379 | */ |
||
380 | abstract protected function doLoadMetadata( |
||
384 | |||
385 | /** |
||
386 | * Creates a new ClassMetadata instance for the given class name. |
||
387 | * |
||
388 | * @param string $className |
||
389 | * @param ClassMetadataBuildingContext $metadataBuildingContext |
||
390 | * |
||
391 | * @return ClassMetadata |
||
392 | */ |
||
393 | abstract protected function newClassMetadataInstance( |
||
397 | |||
398 | /** |
||
399 | * Creates a new ClassMetadataBuildingContext instance. |
||
400 | * |
||
401 | * @return ClassMetadataBuildingContext |
||
402 | */ |
||
403 | abstract protected function newClassMetadataBuildingContext() : ClassMetadataBuildingContext; |
||
404 | |||
405 | /** |
||
406 | * {@inheritDoc} |
||
407 | */ |
||
408 | public function isTransient($class) : bool |
||
423 | |||
424 | /** |
||
425 | * Sets the reflectionService. |
||
426 | * |
||
427 | * @param ReflectionService $reflectionService |
||
428 | * |
||
429 | * @return void |
||
430 | */ |
||
431 | public function setReflectionService(ReflectionService $reflectionService) : void |
||
435 | |||
436 | /** |
||
437 | * Gets the reflection service associated with this metadata factory. |
||
438 | * |
||
439 | * @return ReflectionService |
||
440 | */ |
||
441 | public function getReflectionService() : ReflectionService |
||
449 | } |
||
450 |
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: