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