Implemented partial backport of the FLOW3 Reflection Service
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Reflection / Service.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Christopher Hlubek <hlubek@networkteam.com>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 /**
26 * A backport of the FLOW3 reflection service for aquiring reflection based information.
27 * Most of the code is based on the FLOW3 reflection service.
28 *
29 * @package TYPO3
30 * @subpackage extbase
31 * @version $Id:$
32 */
33 class Tx_ExtBase_Reflection_Service implements t3lib_Singleton {
34
35 /**
36 * List of tags which are ignored while reflecting class and method annotations
37 *
38 * @var array
39 */
40 protected $ignoredTags = array('package', 'subpackage', 'license', 'copyright', 'author', 'version', 'const');
41
42 /**
43 * @var array Array of class reflections by class name
44 */
45 protected $classReflections;
46
47 /**
48 * Returns all tags and their values the specified method is tagged with
49 *
50 * @param string $className Name of the class containing the method
51 * @param string $methodName Name of the method to return the tags and values of
52 * @return array An array of tags and their values or an empty array of no tags were found
53 * @author Robert Lemke <robert@typo3.org>
54 * @author Christopher Hlubek <hlubek@networkteam.com>
55 */
56 public function getMethodTagsValues($className, $methodName) {
57 if (!isset($this->methodTagsValues[$className][$methodName])) {
58 $this->methodTagsValues[$className][$methodName] = array();
59 $class = $this->getClassReflection($className);
60 foreach ($class->getMethods() as $method) {
61 $classMethodName = $method->getName();
62 foreach ($method->getTagsValues() as $tag => $values) {
63 if (array_search($tag, $this->ignoredTags) === FALSE) {
64 $this->methodTagsValues[$className][$classMethodName][$tag] = $values;
65 }
66 }
67 }
68 }
69 return $this->methodTagsValues[$className][$methodName];
70 }
71
72
73 /**
74 * Returns an array of parameters of the given method. Each entry contains
75 * additional information about the parameter position, type hint etc.
76 *
77 * @param string $className Name of the class containing the method
78 * @param string $methodName Name of the method to return parameter information of
79 * @return array An array of parameter names and additional information or an empty array of no parameters were found
80 * @author Robert Lemke <robert@typo3.org>
81 * @author Christopher Hlubek <hlubek@networkteam.com>
82 */
83 public function getMethodParameters($className, $methodName) {
84 if (!isset($this->methodParameters[$className][$methodName])) {
85 $method = new ReflectionMethod($className, $methodName);
86 $this->methodParameters[$className][$methodName] = array();
87 foreach($method->getParameters() as $parameter) {
88 $this->methodParameters[$className][$methodName][$parameter->getName()] = $this->convertParameterReflectionToArray($parameter);
89 }
90 }
91 return $this->methodParameters[$className][$methodName];
92 }
93
94 /**
95 * Converts the given parameter reflection into an information array
96 *
97 * @param ReflectionParameter $parameter The parameter to reflect
98 * @return array Parameter information array
99 * @author Robert Lemke <robert@typo3.org>
100 * @author Sebastian Kurf├╝rst <sebastian@typo3.org>
101 */
102 protected function convertParameterReflectionToArray(ReflectionParameter $parameter, ReflectionMethod $method = NULL) {
103 $parameterInformation = array(
104 'position' => $parameter->getPosition(),
105 'byReference' => $parameter->isPassedByReference() ? TRUE : FALSE,
106 'array' => $parameter->isArray() ? TRUE : FALSE,
107 'optional' => $parameter->isOptional() ? TRUE : FALSE,
108 'allowsNull' => $parameter->allowsNull() ? TRUE : FALSE
109 );
110
111 $parameterClass = $parameter->getClass();
112 $parameterInformation['class'] = ($parameterClass !== NULL) ? $parameterClass->getName() : NULL;
113 if ($parameter->isDefaultValueAvailable()) {
114 $parameterInformation['defaultValue'] = $parameter->getDefaultValue();
115 }
116 if ($parameterClass !== NULL) {
117 $parameterInformation['type'] = $parameterClass->getName();
118 } elseif ($method !== NULL) {
119 $methodTagsAndValues = $this->getMethodTagsValues($method->getDeclaringClass()->getName(), $method->getName());
120 if (isset($methodTagsAndValues['param']) && isset($methodTagsAndValues['param'][$parameter->getPosition()])) {
121 $explodedParameters = explode(' ', $methodTagsAndValues['param'][$parameter->getPosition()]);
122 if (count($explodedParameters) >= 2) {
123 $parameterInformation['type'] = $explodedParameters[0];
124 }
125 }
126 }
127 if (isset($parameterInformation['type']) && $parameterInformation['type']{0} === '\\') {
128 $parameterInformation['type'] = substr($parameterInformation['type'], 1);
129 }
130 return $parameterInformation;
131 }
132
133 protected function getClassReflection($className) {
134 if (!isset($this->classReflections[$className])) {
135 $this->classReflections[$className] = new Tx_ExtBase_Reflection_ClassReflection($className);
136 }
137 return $this->classReflections[$className];
138 }
139 }
140 ?>