10d43d55a4322c0af79f56c821062b0a90d59418
[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\Cache\CacheManager;
18 use TYPO3\CMS\Core\SingletonInterface;
19
20 /**
21 * Reflection service for acquiring reflection based information.
22 * Originally based on the TYPO3.Flow reflection service.
23 *
24 * @api
25 */
26 class ReflectionService implements SingletonInterface
27 {
28 const CACHE_IDENTIFIER = 'extbase_reflection';
29 const CACHE_ENTRY_IDENTIFIER = 'ClassSchematas';
30
31 /**
32 * @var \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend
33 */
34 protected $dataCache;
35
36 /**
37 * Indicates whether the Reflection cache needs to be updated.
38 *
39 * This flag needs to be set as soon as new Reflection information was
40 * created.
41 *
42 * @var bool
43 */
44 protected $dataCacheNeedsUpdate = false;
45
46 /**
47 * Local cache for Class schemata
48 *
49 * @var array
50 */
51 protected $classSchemata = [];
52
53 /**
54 * @var bool
55 */
56 private $cachingEnabled = false;
57
58 /**
59 * If not $cacheManager is injected, the reflection service does not
60 * cache any data, useful for testing this service in unit tests.
61 *
62 * @param CacheManager $cacheManager
63 */
64 public function __construct(CacheManager $cacheManager = null)
65 {
66 if ($cacheManager instanceof CacheManager && $cacheManager->hasCache(static::CACHE_IDENTIFIER)) {
67 $this->cachingEnabled = true;
68 $this->dataCache = $cacheManager->getCache(static::CACHE_IDENTIFIER);
69
70 if (($classSchemata = $this->dataCache->get(static::CACHE_ENTRY_IDENTIFIER)) !== false) {
71 $this->classSchemata = $classSchemata;
72 }
73 }
74 }
75
76 public function __destruct()
77 {
78 if ($this->dataCacheNeedsUpdate && $this->cachingEnabled) {
79 $this->dataCache->set(static::CACHE_ENTRY_IDENTIFIER, $this->classSchemata);
80 }
81 }
82
83 /**
84 * Returns all tags and their values the specified class is tagged with
85 *
86 * @param string $className Name of the class
87 * @return array An array of tags and their values or an empty array if no tags were found
88 */
89 public function getClassTagsValues($className): array
90 {
91 try {
92 $classSchema = $this->getClassSchema($className);
93 } catch (\Exception $e) {
94 return [];
95 }
96
97 return $classSchema->getTags();
98 }
99
100 /**
101 * Returns the values of the specified class tag
102 *
103 * @param string $className Name of the class containing the property
104 * @param string $tag Tag to return the values of
105 * @return array An array of values or an empty array if the tag was not found
106 */
107 public function getClassTagValues($className, $tag): array
108 {
109 try {
110 $classSchema = $this->getClassSchema($className);
111 } catch (\Exception $e) {
112 return [];
113 }
114
115 return $classSchema->getTags()[$tag] ?? [];
116 }
117
118 /**
119 * Returns the names of all properties of the specified class
120 *
121 * @param string $className Name of the class to return the property names of
122 * @return array An array of property names or an empty array if none exist
123 */
124 public function getClassPropertyNames($className): array
125 {
126 try {
127 $classSchema = $this->getClassSchema($className);
128 } catch (\Exception $e) {
129 return [];
130 }
131
132 return array_keys($classSchema->getProperties());
133 }
134
135 /**
136 * Returns the class schema for the given class
137 *
138 * @param mixed $classNameOrObject The class name or an object
139 * @return ClassSchema
140 * @throws \TYPO3\CMS\Extbase\Reflection\Exception\UnknownClassException
141 */
142 public function getClassSchema($classNameOrObject): ClassSchema
143 {
144 $className = is_object($classNameOrObject) ? get_class($classNameOrObject) : $classNameOrObject;
145 if (isset($this->classSchemata[$className])) {
146 return $this->classSchemata[$className];
147 }
148
149 return $this->buildClassSchema($className);
150 }
151
152 /**
153 * Wrapper for method_exists() which tells if the given method exists.
154 *
155 * @param string $className Name of the class containing the method
156 * @param string $methodName Name of the method
157 * @return bool
158 */
159 public function hasMethod($className, $methodName): bool
160 {
161 try {
162 $classSchema = $this->getClassSchema($className);
163 } catch (\Exception $e) {
164 return false;
165 }
166
167 return $classSchema->hasMethod($methodName);
168 }
169
170 /**
171 * Returns all tags and their values the specified method is tagged with
172 *
173 * @param string $className Name of the class containing the method
174 * @param string $methodName Name of the method to return the tags and values of
175 * @return array An array of tags and their values or an empty array of no tags were found
176 */
177 public function getMethodTagsValues($className, $methodName): array
178 {
179 try {
180 $classSchema = $this->getClassSchema($className);
181 } catch (\Exception $e) {
182 return [];
183 }
184
185 return $classSchema->getMethod($methodName)['tags'] ?? [];
186 }
187
188 /**
189 * Returns an array of parameters of the given method. Each entry contains
190 * additional information about the parameter position, type hint etc.
191 *
192 * @param string $className Name of the class containing the method
193 * @param string $methodName Name of the method to return parameter information of
194 * @return array An array of parameter names and additional information or an empty array of no parameters were found
195 */
196 public function getMethodParameters($className, $methodName): array
197 {
198 try {
199 $classSchema = $this->getClassSchema($className);
200 } catch (\Exception $e) {
201 return [];
202 }
203
204 return $classSchema->getMethod($methodName)['params'] ?? [];
205 }
206
207 /**
208 * Returns all tags and their values the specified class property is tagged with
209 *
210 * @param string $className Name of the class containing the property
211 * @param string $propertyName Name of the property to return the tags and values of
212 * @return array An array of tags and their values or an empty array of no tags were found
213 */
214 public function getPropertyTagsValues($className, $propertyName): array
215 {
216 try {
217 $classSchema = $this->getClassSchema($className);
218 } catch (\Exception $e) {
219 return [];
220 }
221
222 return $classSchema->getProperty($propertyName)['tags'] ?? [];
223 }
224
225 /**
226 * Returns the values of the specified class property tag
227 *
228 * @param string $className Name of the class containing the property
229 * @param string $propertyName Name of the tagged property
230 * @param string $tag Tag to return the values of
231 * @return array An array of values or an empty array if the tag was not found
232 */
233 public function getPropertyTagValues($className, $propertyName, $tag): array
234 {
235 try {
236 $classSchema = $this->getClassSchema($className);
237 } catch (\Exception $e) {
238 return [];
239 }
240
241 return $classSchema->getProperty($propertyName)['tags'][$tag] ?? [];
242 }
243
244 /**
245 * Tells if the specified class is tagged with the given tag
246 *
247 * @param string $className Name of the class
248 * @param string $tag Tag to check for
249 * @return bool TRUE if the class is tagged with $tag, otherwise FALSE
250 */
251 public function isClassTaggedWith($className, $tag): bool
252 {
253 try {
254 $classSchema = $this->getClassSchema($className);
255 } catch (\Exception $e) {
256 return false;
257 }
258
259 foreach (array_keys($classSchema->getTags()) as $tagName) {
260 if ($tagName === $tag) {
261 return true;
262 }
263 }
264
265 return false;
266 }
267
268 /**
269 * Tells if the specified class property is tagged with the given tag
270 *
271 * @param string $className Name of the class
272 * @param string $propertyName Name of the property
273 * @param string $tag Tag to check for
274 * @return bool TRUE if the class property is tagged with $tag, otherwise FALSE
275 */
276 public function isPropertyTaggedWith($className, $propertyName, $tag): bool
277 {
278 try {
279 $classSchema = $this->getClassSchema($className);
280 } catch (\Exception $e) {
281 return false;
282 }
283
284 $property = $classSchema->getProperty($propertyName);
285
286 if (empty($property)) {
287 return false;
288 }
289
290 return isset($property['tags'][$tag]);
291 }
292
293 /**
294 * Builds class schemata from classes annotated as entities or value objects
295 *
296 * @param string $className
297 * @throws Exception\UnknownClassException
298 * @return ClassSchema The class schema
299 */
300 protected function buildClassSchema($className): ClassSchema
301 {
302 try {
303 $classSchema = new ClassSchema($className);
304 } catch (\ReflectionException $e) {
305 throw new Exception\UnknownClassException('The classname "' . $className . '" was not found and thus can not be reflected.', 1278450972, $e);
306 }
307 $this->classSchemata[$className] = $classSchema;
308 $this->dataCacheNeedsUpdate = true;
309 return $classSchema;
310 }
311 }