326dd2b5f9ecd1f3dee269e9017751f4d00a3015
[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 * @deprecated
89 */
90 public function getClassTagsValues($className): array
91 {
92 trigger_error(
93 'Method ' . __METHOD__ . ' is deprecated and will be removed in TYPO3 v10.0.',
94 E_USER_DEPRECATED
95 );
96
97 try {
98 $classSchema = $this->getClassSchema($className);
99 } catch (\Exception $e) {
100 return [];
101 }
102
103 return $classSchema->getTags();
104 }
105
106 /**
107 * Returns the values of the specified class tag
108 *
109 * @param string $className Name of the class containing the property
110 * @param string $tag Tag to return the values of
111 * @return array An array of values or an empty array if the tag was not found
112 * @deprecated
113 */
114 public function getClassTagValues($className, $tag): array
115 {
116 trigger_error(
117 'Method ' . __METHOD__ . ' is deprecated and will be removed in TYPO3 v10.0.',
118 E_USER_DEPRECATED
119 );
120
121 try {
122 $classSchema = $this->getClassSchema($className);
123 } catch (\Exception $e) {
124 return [];
125 }
126
127 return $classSchema->getTags()[$tag] ?? [];
128 }
129
130 /**
131 * Returns the names of all properties of the specified class
132 *
133 * @param string $className Name of the class to return the property names of
134 * @return array An array of property names or an empty array if none exist
135 * @deprecated
136 */
137 public function getClassPropertyNames($className): array
138 {
139 trigger_error(
140 'Method ' . __METHOD__ . ' is deprecated and will be removed in TYPO3 v10.0.',
141 E_USER_DEPRECATED
142 );
143
144 try {
145 $classSchema = $this->getClassSchema($className);
146 } catch (\Exception $e) {
147 return [];
148 }
149
150 return array_keys($classSchema->getProperties());
151 }
152
153 /**
154 * Returns the class schema for the given class
155 *
156 * @param mixed $classNameOrObject The class name or an object
157 * @return ClassSchema
158 * @throws \TYPO3\CMS\Extbase\Reflection\Exception\UnknownClassException
159 */
160 public function getClassSchema($classNameOrObject): ClassSchema
161 {
162 $className = is_object($classNameOrObject) ? get_class($classNameOrObject) : $classNameOrObject;
163 if (isset($this->classSchemata[$className])) {
164 return $this->classSchemata[$className];
165 }
166
167 return $this->buildClassSchema($className);
168 }
169
170 /**
171 * Wrapper for method_exists() which tells if the given method exists.
172 *
173 * @param string $className Name of the class containing the method
174 * @param string $methodName Name of the method
175 * @return bool
176 * @deprecated
177 */
178 public function hasMethod($className, $methodName): bool
179 {
180 trigger_error(
181 'Method ' . __METHOD__ . ' is deprecated and will be removed in TYPO3 v10.0.',
182 E_USER_DEPRECATED
183 );
184
185 try {
186 $classSchema = $this->getClassSchema($className);
187 } catch (\Exception $e) {
188 return false;
189 }
190
191 return $classSchema->hasMethod($methodName);
192 }
193
194 /**
195 * Returns all tags and their values the specified method is tagged with
196 *
197 * @param string $className Name of the class containing the method
198 * @param string $methodName Name of the method to return the tags and values of
199 * @return array An array of tags and their values or an empty array of no tags were found
200 * @deprecated
201 */
202 public function getMethodTagsValues($className, $methodName): array
203 {
204 trigger_error(
205 'Method ' . __METHOD__ . ' is deprecated and will be removed in TYPO3 v10.0.',
206 E_USER_DEPRECATED
207 );
208
209 try {
210 $classSchema = $this->getClassSchema($className);
211 } catch (\Exception $e) {
212 return [];
213 }
214
215 return $classSchema->getMethod($methodName)['tags'] ?? [];
216 }
217
218 /**
219 * Returns an array of parameters of the given method. Each entry contains
220 * additional information about the parameter position, type hint etc.
221 *
222 * @param string $className Name of the class containing the method
223 * @param string $methodName Name of the method to return parameter information of
224 * @return array An array of parameter names and additional information or an empty array of no parameters were found
225 * @deprecated
226 */
227 public function getMethodParameters($className, $methodName): array
228 {
229 trigger_error(
230 'Method ' . __METHOD__ . ' is deprecated and will be removed in TYPO3 v10.0.',
231 E_USER_DEPRECATED
232 );
233
234 try {
235 $classSchema = $this->getClassSchema($className);
236 } catch (\Exception $e) {
237 return [];
238 }
239
240 return $classSchema->getMethod($methodName)['params'] ?? [];
241 }
242
243 /**
244 * Returns all tags and their values the specified class property is tagged with
245 *
246 * @param string $className Name of the class containing the property
247 * @param string $propertyName Name of the property to return the tags and values of
248 * @return array An array of tags and their values or an empty array of no tags were found
249 * @deprecated
250 */
251 public function getPropertyTagsValues($className, $propertyName): array
252 {
253 trigger_error(
254 'Method ' . __METHOD__ . ' is deprecated and will be removed in TYPO3 v10.0.',
255 E_USER_DEPRECATED
256 );
257
258 try {
259 $classSchema = $this->getClassSchema($className);
260 } catch (\Exception $e) {
261 return [];
262 }
263
264 return $classSchema->getProperty($propertyName)['tags'] ?? [];
265 }
266
267 /**
268 * Returns the values of the specified class property tag
269 *
270 * @param string $className Name of the class containing the property
271 * @param string $propertyName Name of the tagged property
272 * @param string $tag Tag to return the values of
273 * @return array An array of values or an empty array if the tag was not found
274 * @deprecated
275 */
276 public function getPropertyTagValues($className, $propertyName, $tag): array
277 {
278 trigger_error(
279 'Method ' . __METHOD__ . ' is deprecated and will be removed in TYPO3 v10.0.',
280 E_USER_DEPRECATED
281 );
282
283 try {
284 $classSchema = $this->getClassSchema($className);
285 } catch (\Exception $e) {
286 return [];
287 }
288
289 return $classSchema->getProperty($propertyName)['tags'][$tag] ?? [];
290 }
291
292 /**
293 * Tells if the specified class is tagged with the given tag
294 *
295 * @param string $className Name of the class
296 * @param string $tag Tag to check for
297 * @return bool TRUE if the class is tagged with $tag, otherwise FALSE
298 * @deprecated
299 */
300 public function isClassTaggedWith($className, $tag): bool
301 {
302 trigger_error(
303 'Method ' . __METHOD__ . ' is deprecated and will be removed in TYPO3 v10.0.',
304 E_USER_DEPRECATED
305 );
306
307 try {
308 $classSchema = $this->getClassSchema($className);
309 } catch (\Exception $e) {
310 return false;
311 }
312
313 foreach (array_keys($classSchema->getTags()) as $tagName) {
314 if ($tagName === $tag) {
315 return true;
316 }
317 }
318
319 return false;
320 }
321
322 /**
323 * Tells if the specified class property is tagged with the given tag
324 *
325 * @param string $className Name of the class
326 * @param string $propertyName Name of the property
327 * @param string $tag Tag to check for
328 * @return bool TRUE if the class property is tagged with $tag, otherwise FALSE
329 * @deprecated
330 */
331 public function isPropertyTaggedWith($className, $propertyName, $tag): bool
332 {
333 trigger_error(
334 'Method ' . __METHOD__ . ' is deprecated and will be removed in TYPO3 v10.0.',
335 E_USER_DEPRECATED
336 );
337
338 try {
339 $classSchema = $this->getClassSchema($className);
340 } catch (\Exception $e) {
341 return false;
342 }
343
344 $property = $classSchema->getProperty($propertyName);
345
346 if (empty($property)) {
347 return false;
348 }
349
350 return isset($property['tags'][$tag]);
351 }
352
353 /**
354 * Builds class schemata from classes annotated as entities or value objects
355 *
356 * @param string $className
357 * @throws Exception\UnknownClassException
358 * @return ClassSchema The class schema
359 */
360 protected function buildClassSchema($className): ClassSchema
361 {
362 try {
363 $classSchema = new ClassSchema($className);
364 } catch (\ReflectionException $e) {
365 throw new Exception\UnknownClassException('The classname "' . $className . '" was not found and thus can not be reflected.', 1278450972, $e);
366 }
367 $this->classSchemata[$className] = $classSchema;
368 $this->dataCacheNeedsUpdate = true;
369 return $classSchema;
370 }
371 }