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