[TASK] Update php-cs-fixer to 2.5.0
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Reflection / ReflectionService.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Reflection;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Utility\ClassNamingUtility;
18 use TYPO3\CMS\Extbase\Utility\TypeHandlingUtility;
19
20 /**
21 * A backport of the TYPO3.Flow reflection service for acquiring reflection based information.
22 * Most of the code is based on the TYPO3.Flow reflection service.
23 *
24 * @api
25 */
26 class ReflectionService implements \TYPO3\CMS\Core\SingletonInterface
27 {
28 /**
29 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
30 */
31 protected $objectManager;
32
33 /**
34 * Whether this service has been initialized.
35 *
36 * @var bool
37 */
38 protected $initialized = false;
39
40 /**
41 * @var \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend
42 */
43 protected $dataCache;
44
45 /**
46 * Whether class alterations should be detected on each initialization.
47 *
48 * @var bool
49 */
50 protected $detectClassChanges = false;
51
52 /**
53 * All available class names to consider. Class name = key, value is the
54 * UNIX timestamp the class was reflected.
55 *
56 * @var array
57 */
58 protected $reflectedClassNames = [];
59
60 /**
61 * Array of tags and the names of classes which are tagged with them.
62 *
63 * @var array
64 */
65 protected $taggedClasses = [];
66
67 /**
68 * Array of class names and their tags and values.
69 *
70 * @var array
71 */
72 protected $classTagsValues = [];
73
74 /**
75 * Array of class names, method names and their tags and values.
76 *
77 * @var array
78 */
79 protected $methodTagsValues = [];
80
81 /**
82 * Array of class names, method names, their parameters and additional
83 * information about the parameters.
84 *
85 * @var array
86 */
87 protected $methodParameters = [];
88
89 /**
90 * Array of class names and names of their properties.
91 *
92 * @var array
93 */
94 protected $classPropertyNames = [];
95
96 /**
97 * Array of class names and names of their methods.
98 *
99 * @var array
100 */
101 protected $classMethodNames = [];
102
103 /**
104 * Array of class names, property names and their tags and values.
105 *
106 * @var array
107 */
108 protected $propertyTagsValues = [];
109
110 /**
111 * List of tags which are ignored while reflecting class and method annotations.
112 *
113 * @var array
114 */
115 protected $ignoredTags = ['package', 'subpackage', 'license', 'copyright', 'author', 'version', 'const'];
116
117 /**
118 * Indicates whether the Reflection cache needs to be updated.
119 *
120 * This flag needs to be set as soon as new Reflection information was
121 * created.
122 *
123 * @see reflectClass()
124 * @see getMethodReflection()
125 * @var bool
126 */
127 protected $dataCacheNeedsUpdate = false;
128
129 /**
130 * Local cache for Class schemata
131 *
132 * @var array
133 */
134 protected $classSchemata = [];
135
136 /**
137 * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
138 */
139 protected $configurationManager;
140
141 /**
142 * @var string
143 */
144 protected $cacheIdentifier;
145
146 /**
147 * Internal runtime cache of method reflection objects
148 *
149 * @var array
150 */
151 protected $methodReflections = [];
152
153 /**
154 * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
155 */
156 public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
157 {
158 $this->objectManager = $objectManager;
159 }
160
161 /**
162 * @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager
163 */
164 public function injectConfigurationManager(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager)
165 {
166 $this->configurationManager = $configurationManager;
167 }
168
169 /**
170 * Sets the data cache.
171 *
172 * The cache must be set before initializing the Reflection Service.
173 *
174 * @param \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend $dataCache Cache for the Reflection service
175 */
176 public function setDataCache(\TYPO3\CMS\Core\Cache\Frontend\VariableFrontend $dataCache)
177 {
178 $this->dataCache = $dataCache;
179 }
180
181 /**
182 * Initializes this service
183 *
184 * @throws Exception
185 */
186 public function initialize()
187 {
188 if ($this->initialized) {
189 throw new Exception('The Reflection Service can only be initialized once.', 1232044696);
190 }
191 $frameworkConfiguration = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
192 $this->cacheIdentifier = 'ReflectionData_' . $frameworkConfiguration['extensionName'];
193 $this->loadFromCache();
194 $this->initialized = true;
195 }
196
197 /**
198 * Returns whether the Reflection Service is initialized.
199 *
200 * @return bool true if the Reflection Service is initialized, otherwise false
201 */
202 public function isInitialized()
203 {
204 return $this->initialized;
205 }
206
207 /**
208 * Shuts the Reflection Service down.
209 */
210 public function shutdown()
211 {
212 if ($this->dataCacheNeedsUpdate) {
213 $this->saveToCache();
214 }
215 $this->initialized = false;
216 }
217
218 /**
219 * Returns all tags and their values the specified class is tagged with
220 *
221 * @param string $className Name of the class
222 * @return array An array of tags and their values or an empty array if no tags were found
223 */
224 public function getClassTagsValues($className)
225 {
226 if (!isset($this->reflectedClassNames[$className])) {
227 $this->reflectClass($className);
228 }
229 if (!isset($this->classTagsValues[$className])) {
230 return [];
231 }
232 return isset($this->classTagsValues[$className]) ? $this->classTagsValues[$className] : [];
233 }
234
235 /**
236 * Returns the values of the specified class tag
237 *
238 * @param string $className Name of the class containing the property
239 * @param string $tag Tag to return the values of
240 * @return array An array of values or an empty array if the tag was not found
241 */
242 public function getClassTagValues($className, $tag)
243 {
244 if (!isset($this->reflectedClassNames[$className])) {
245 $this->reflectClass($className);
246 }
247 if (!isset($this->classTagsValues[$className])) {
248 return [];
249 }
250 return isset($this->classTagsValues[$className][$tag]) ? $this->classTagsValues[$className][$tag] : [];
251 }
252
253 /**
254 * Returns the names of all properties of the specified class
255 *
256 * @param string $className Name of the class to return the property names of
257 * @return array An array of property names or an empty array if none exist
258 */
259 public function getClassPropertyNames($className)
260 {
261 if (!isset($this->reflectedClassNames[$className])) {
262 $this->reflectClass($className);
263 }
264 return isset($this->classPropertyNames[$className]) ? $this->classPropertyNames[$className] : [];
265 }
266
267 /**
268 * Returns the class schema for the given class
269 *
270 * @param mixed $classNameOrObject The class name or an object
271 * @return ClassSchema
272 */
273 public function getClassSchema($classNameOrObject)
274 {
275 $className = is_object($classNameOrObject) ? get_class($classNameOrObject) : $classNameOrObject;
276 if (isset($this->classSchemata[$className])) {
277 return $this->classSchemata[$className];
278 }
279 return $this->buildClassSchema($className);
280 }
281
282 /**
283 * Wrapper for method_exists() which tells if the given method exists.
284 *
285 * @param string $className Name of the class containing the method
286 * @param string $methodName Name of the method
287 * @return bool
288 */
289 public function hasMethod($className, $methodName)
290 {
291 try {
292 if (!array_key_exists($className, $this->classMethodNames) || !array_key_exists($methodName, $this->classMethodNames[$className])) {
293 $this->getMethodReflection($className, $methodName);
294 $this->classMethodNames[$className][$methodName] = true;
295 }
296 } catch (\ReflectionException $e) {
297 // Method does not exist. Store this information in cache.
298 $this->classMethodNames[$className][$methodName] = null;
299 }
300 return isset($this->classMethodNames[$className][$methodName]);
301 }
302
303 /**
304 * Returns all tags and their values the specified method is tagged with
305 *
306 * @param string $className Name of the class containing the method
307 * @param string $methodName Name of the method to return the tags and values of
308 * @return array An array of tags and their values or an empty array of no tags were found
309 */
310 public function getMethodTagsValues($className, $methodName)
311 {
312 if (!isset($this->methodTagsValues[$className][$methodName])) {
313 $method = $this->getMethodReflection($className, $methodName);
314 $this->methodTagsValues[$className][$methodName] = [];
315 foreach ($method->getTagsValues() as $tag => $values) {
316 if (array_search($tag, $this->ignoredTags) === false) {
317 $this->methodTagsValues[$className][$methodName][$tag] = $values;
318 }
319 }
320 }
321 return $this->methodTagsValues[$className][$methodName];
322 }
323
324 /**
325 * Returns an array of parameters of the given method. Each entry contains
326 * additional information about the parameter position, type hint etc.
327 *
328 * @param string $className Name of the class containing the method
329 * @param string $methodName Name of the method to return parameter information of
330 * @return array An array of parameter names and additional information or an empty array of no parameters were found
331 */
332 public function getMethodParameters($className, $methodName)
333 {
334 if (!isset($this->methodParameters[$className][$methodName])) {
335 $method = $this->getMethodReflection($className, $methodName);
336 $this->methodParameters[$className][$methodName] = [];
337 foreach ($method->getParameters() as $parameterPosition => $parameter) {
338 $this->methodParameters[$className][$methodName][$parameter->getName()] = $this->convertParameterReflectionToArray($parameter, $parameterPosition, $method);
339 }
340 }
341 return $this->methodParameters[$className][$methodName];
342 }
343
344 /**
345 * Returns all tags and their values the specified class property is tagged with
346 *
347 * @param string $className Name of the class containing the property
348 * @param string $propertyName Name of the property to return the tags and values of
349 * @return array An array of tags and their values or an empty array of no tags were found
350 */
351 public function getPropertyTagsValues($className, $propertyName)
352 {
353 if (!isset($this->reflectedClassNames[$className])) {
354 $this->reflectClass($className);
355 }
356 if (!isset($this->propertyTagsValues[$className])) {
357 return [];
358 }
359 return isset($this->propertyTagsValues[$className][$propertyName]) ? $this->propertyTagsValues[$className][$propertyName] : [];
360 }
361
362 /**
363 * Returns the values of the specified class property tag
364 *
365 * @param string $className Name of the class containing the property
366 * @param string $propertyName Name of the tagged property
367 * @param string $tag Tag to return the values of
368 * @return array An array of values or an empty array if the tag was not found
369 */
370 public function getPropertyTagValues($className, $propertyName, $tag)
371 {
372 if (!isset($this->reflectedClassNames[$className])) {
373 $this->reflectClass($className);
374 }
375 if (!isset($this->propertyTagsValues[$className][$propertyName])) {
376 return [];
377 }
378 return isset($this->propertyTagsValues[$className][$propertyName][$tag]) ? $this->propertyTagsValues[$className][$propertyName][$tag] : [];
379 }
380
381 /**
382 * Tells if the specified class is known to this reflection service and
383 * reflection information is available.
384 *
385 * @param string $className Name of the class
386 * @return bool If the class is reflected by this service
387 */
388 public function isClassReflected($className)
389 {
390 return isset($this->reflectedClassNames[$className]);
391 }
392
393 /**
394 * Tells if the specified class is tagged with the given tag
395 *
396 * @param string $className Name of the class
397 * @param string $tag Tag to check for
398 * @return bool TRUE if the class is tagged with $tag, otherwise FALSE
399 */
400 public function isClassTaggedWith($className, $tag)
401 {
402 if ($this->initialized === false) {
403 return false;
404 }
405 if (!isset($this->reflectedClassNames[$className])) {
406 $this->reflectClass($className);
407 }
408 if (!isset($this->classTagsValues[$className])) {
409 return false;
410 }
411 return isset($this->classTagsValues[$className][$tag]);
412 }
413
414 /**
415 * Tells if the specified class property is tagged with the given tag
416 *
417 * @param string $className Name of the class
418 * @param string $propertyName Name of the property
419 * @param string $tag Tag to check for
420 * @return bool TRUE if the class property is tagged with $tag, otherwise FALSE
421 */
422 public function isPropertyTaggedWith($className, $propertyName, $tag)
423 {
424 if (!isset($this->reflectedClassNames[$className])) {
425 $this->reflectClass($className);
426 }
427 if (!isset($this->propertyTagsValues[$className])) {
428 return false;
429 }
430 if (!isset($this->propertyTagsValues[$className][$propertyName])) {
431 return false;
432 }
433 return isset($this->propertyTagsValues[$className][$propertyName][$tag]);
434 }
435
436 /**
437 * Reflects the given class and stores the results in this service's properties.
438 *
439 * @param string $className Full qualified name of the class to reflect
440 */
441 protected function reflectClass($className)
442 {
443 $class = new ClassReflection($className);
444 $this->reflectedClassNames[$className] = time();
445 foreach ($class->getTagsValues() as $tag => $values) {
446 if (array_search($tag, $this->ignoredTags) === false) {
447 $this->taggedClasses[$tag][] = $className;
448 $this->classTagsValues[$className][$tag] = $values;
449 }
450 }
451 foreach ($class->getProperties() as $property) {
452 $propertyName = $property->getName();
453 $this->classPropertyNames[$className][] = $propertyName;
454 foreach ($property->getTagsValues() as $tag => $values) {
455 if (array_search($tag, $this->ignoredTags) === false) {
456 $this->propertyTagsValues[$className][$propertyName][$tag] = $values;
457 }
458 }
459 }
460 foreach ($class->getMethods() as $method) {
461 $methodName = $method->getName();
462 foreach ($method->getTagsValues() as $tag => $values) {
463 if (array_search($tag, $this->ignoredTags) === false) {
464 $this->methodTagsValues[$className][$methodName][$tag] = $values;
465 }
466 }
467 foreach ($method->getParameters() as $parameterPosition => $parameter) {
468 $this->methodParameters[$className][$methodName][$parameter->getName()] = $this->convertParameterReflectionToArray($parameter, $parameterPosition, $method);
469 }
470 }
471 ksort($this->reflectedClassNames);
472 $this->dataCacheNeedsUpdate = true;
473 }
474
475 /**
476 * Builds class schemata from classes annotated as entities or value objects
477 *
478 * @param string $className
479 * @throws Exception\UnknownClassException
480 * @return ClassSchema The class schema
481 */
482 protected function buildClassSchema($className)
483 {
484 if (!class_exists($className)) {
485 throw new Exception\UnknownClassException('The classname "' . $className . '" was not found and thus can not be reflected.', 1278450972);
486 }
487 $classSchema = $this->objectManager->get(\TYPO3\CMS\Extbase\Reflection\ClassSchema::class, $className);
488 if (is_subclass_of($className, \TYPO3\CMS\Extbase\DomainObject\AbstractEntity::class)) {
489 $classSchema->setModelType(ClassSchema::MODELTYPE_ENTITY);
490 $possibleRepositoryClassName = ClassNamingUtility::translateModelNameToRepositoryName($className);
491 if (class_exists($possibleRepositoryClassName)) {
492 $classSchema->setAggregateRoot(true);
493 }
494 } elseif (is_subclass_of($className, \TYPO3\CMS\Extbase\DomainObject\AbstractValueObject::class)) {
495 $classSchema->setModelType(ClassSchema::MODELTYPE_VALUEOBJECT);
496 }
497 foreach ($this->getClassPropertyNames($className) as $propertyName) {
498 if (!$this->isPropertyTaggedWith($className, $propertyName, 'transient') && $this->isPropertyTaggedWith($className, $propertyName, 'var')) {
499 $cascadeTagValues = $this->getPropertyTagValues($className, $propertyName, 'cascade');
500 $classSchema->addProperty($propertyName, implode(' ', $this->getPropertyTagValues($className, $propertyName, 'var')), $this->isPropertyTaggedWith($className, $propertyName, 'lazy'), $cascadeTagValues[0]);
501 }
502 if ($this->isPropertyTaggedWith($className, $propertyName, 'uuid')) {
503 $classSchema->setUuidPropertyName($propertyName);
504 }
505 if ($this->isPropertyTaggedWith($className, $propertyName, 'identity')) {
506 $classSchema->markAsIdentityProperty($propertyName);
507 }
508 }
509 $this->classSchemata[$className] = $classSchema;
510 $this->dataCacheNeedsUpdate = true;
511 return $classSchema;
512 }
513
514 /**
515 * Converts the given parameter reflection into an information array
516 *
517 * @param ParameterReflection $parameter The parameter to reflect
518 * @param int $parameterPosition
519 * @param MethodReflection|NULL $method
520 * @return array Parameter information array
521 */
522 protected function convertParameterReflectionToArray(ParameterReflection $parameter, $parameterPosition, MethodReflection $method = null)
523 {
524 $parameterInformation = [
525 'position' => $parameterPosition,
526 'byReference' => $parameter->isPassedByReference(),
527 'array' => $parameter->isArray(),
528 'optional' => $parameter->isOptional(),
529 'allowsNull' => $parameter->allowsNull()
530 ];
531 $parameterClass = $parameter->getClass();
532 $parameterInformation['class'] = $parameterClass !== null ? $parameterClass->getName() : null;
533 if ($parameter->isDefaultValueAvailable()) {
534 $parameterInformation['defaultValue'] = $parameter->getDefaultValue();
535 }
536 if ($parameterClass !== null) {
537 $parameterInformation['type'] = $parameterClass->getName();
538 } elseif ($method !== null) {
539 $methodTagsAndValues = $this->getMethodTagsValues($method->getDeclaringClass()->getName(), $method->getName());
540 if (isset($methodTagsAndValues['param']) && isset($methodTagsAndValues['param'][$parameterPosition])) {
541 $explodedParameters = explode(' ', $methodTagsAndValues['param'][$parameterPosition]);
542 if (count($explodedParameters) >= 2) {
543 if (TypeHandlingUtility::isSimpleType($explodedParameters[0])) {
544 // ensure that short names of simple types are resolved correctly to the long form
545 // this is important for all kinds of type checks later on
546 $typeInfo = TypeHandlingUtility::parseType($explodedParameters[0]);
547 $parameterInformation['type'] = $typeInfo['type'];
548 } else {
549 $parameterInformation['type'] = $explodedParameters[0];
550 }
551 }
552 }
553 }
554 if (isset($parameterInformation['type']) && $parameterInformation['type'][0] === '\\') {
555 $parameterInformation['type'] = substr($parameterInformation['type'], 1);
556 }
557 return $parameterInformation;
558 }
559
560 /**
561 * Returns the Reflection of a method.
562 *
563 * @param string $className Name of the class containing the method
564 * @param string $methodName Name of the method to return the Reflection for
565 * @return MethodReflection the method Reflection object
566 */
567 protected function getMethodReflection($className, $methodName)
568 {
569 $this->dataCacheNeedsUpdate = true;
570 if (!isset($this->methodReflections[$className][$methodName])) {
571 $this->methodReflections[$className][$methodName] = new MethodReflection($className, $methodName);
572 }
573 return $this->methodReflections[$className][$methodName];
574 }
575
576 /**
577 * Tries to load the reflection data from this service's cache.
578 */
579 protected function loadFromCache()
580 {
581 $data = $this->dataCache->get($this->cacheIdentifier);
582 if ($data !== false) {
583 foreach ($data as $propertyName => $propertyValue) {
584 $this->{$propertyName} = $propertyValue;
585 }
586 }
587 }
588
589 /**
590 * Exports the internal reflection data into the ReflectionData cache.
591 *
592 * @throws Exception
593 */
594 protected function saveToCache()
595 {
596 if (!is_object($this->dataCache)) {
597 throw new Exception('A cache must be injected before initializing the Reflection Service.', 1232044697);
598 }
599 $data = [];
600 $propertyNames = [
601 'reflectedClassNames',
602 'classPropertyNames',
603 'classMethodNames',
604 'classTagsValues',
605 'methodTagsValues',
606 'methodParameters',
607 'propertyTagsValues',
608 'taggedClasses',
609 'classSchemata'
610 ];
611 foreach ($propertyNames as $propertyName) {
612 $data[$propertyName] = $this->{$propertyName};
613 }
614 $this->dataCache->set($this->cacheIdentifier, $data);
615 $this->dataCacheNeedsUpdate = false;
616 }
617 }