‪TYPO3CMS  9.5
ReflectionService.php
Go to the documentation of this file.
1 <?php
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 
21 
27 {
28  const ‪CACHE_IDENTIFIER = 'extbase_reflection';
29  const ‪CACHE_ENTRY_IDENTIFIER = 'ClassSchematas';
30 
34  protected ‪$dataCache;
35 
44  protected ‪$dataCacheNeedsUpdate = false;
45 
51  protected ‪$classSchemata = [];
52 
56  private ‪$cachingEnabled = false;
57 
64  public function ‪__construct(‪CacheManager $cacheManager = null)
65  {
66  if ($cacheManager instanceof ‪CacheManager) {
67  try {
68  $this->dataCache = $cacheManager->getCache(static::CACHE_IDENTIFIER);
69  $this->cachingEnabled = true;
70  } catch (‪NoSuchCacheException $ignoredException) {
71  $this->cachingEnabled = false;
72  }
73 
74  if ($this->cachingEnabled) {
75  if ((‪$classSchemata = $this->dataCache->get(static::CACHE_ENTRY_IDENTIFIER)) !== false) {
76  $this->classSchemata = ‪$classSchemata;
77  }
78  }
79  }
80  }
81 
82  public function ‪__destruct()
83  {
84  if ($this->dataCacheNeedsUpdate && $this->cachingEnabled) {
85  $this->dataCache->set(static::CACHE_ENTRY_IDENTIFIER, $this->classSchemata);
86  }
87  }
88 
96  public function ‪getClassTagsValues($className): array
97  {
98  trigger_error(
99  'Method ' . __METHOD__ . ' is deprecated and will be removed in TYPO3 v10.0.',
100  E_USER_DEPRECATED
101  );
102 
103  try {
104  $classSchema = $this->‪getClassSchema($className);
105  } catch (\Exception $e) {
106  return [];
107  }
108 
109  return $classSchema->getTags();
110  }
111 
120  public function ‪getClassTagValues($className, $tag): array
121  {
122  trigger_error(
123  'Method ' . __METHOD__ . ' is deprecated and will be removed in TYPO3 v10.0.',
124  E_USER_DEPRECATED
125  );
126 
127  try {
128  $classSchema = $this->‪getClassSchema($className);
129  } catch (\Exception $e) {
130  return [];
131  }
132 
133  return $classSchema->getTags()[$tag] ?? [];
134  }
135 
143  public function ‪getClassPropertyNames($className): array
144  {
145  trigger_error(
146  'Method ' . __METHOD__ . ' is deprecated and will be removed in TYPO3 v10.0.',
147  E_USER_DEPRECATED
148  );
149 
150  try {
151  $classSchema = $this->‪getClassSchema($className);
152  } catch (\Exception $e) {
153  return [];
154  }
155 
156  return array_keys($classSchema->getProperties());
157  }
158 
166  public function ‪getClassSchema($classNameOrObject): ClassSchema
167  {
168  $className = is_object($classNameOrObject) ? get_class($classNameOrObject) : $classNameOrObject;
169  if (isset($this->classSchemata[$className])) {
170  return $this->classSchemata[$className];
171  }
172 
173  return $this->‪buildClassSchema($className);
174  }
175 
184  public function ‪hasMethod($className, $methodName): bool
185  {
186  trigger_error(
187  'Method ' . __METHOD__ . ' is deprecated and will be removed in TYPO3 v10.0.',
188  E_USER_DEPRECATED
189  );
190 
191  try {
192  $classSchema = $this->‪getClassSchema($className);
193  } catch (\Exception $e) {
194  return false;
195  }
196 
197  return $classSchema->hasMethod($methodName);
198  }
199 
208  public function ‪getMethodTagsValues($className, $methodName): array
209  {
210  trigger_error(
211  'Method ' . __METHOD__ . ' is deprecated and will be removed in TYPO3 v10.0.',
212  E_USER_DEPRECATED
213  );
214 
215  try {
216  $classSchema = $this->‪getClassSchema($className);
217  } catch (\Exception $e) {
218  return [];
219  }
220 
221  return $classSchema->getMethod($methodName)['tags'] ?? [];
222  }
223 
233  public function ‪getMethodParameters($className, $methodName): array
234  {
235  trigger_error(
236  'Method ' . __METHOD__ . ' is deprecated and will be removed in TYPO3 v10.0.',
237  E_USER_DEPRECATED
238  );
239 
240  try {
241  $classSchema = $this->‪getClassSchema($className);
242  } catch (\Exception $e) {
243  return [];
244  }
245 
246  return $classSchema->getMethod($methodName)['params'] ?? [];
247  }
248 
257  public function ‪getPropertyTagsValues($className, $propertyName): array
258  {
259  trigger_error(
260  'Method ' . __METHOD__ . ' is deprecated and will be removed in TYPO3 v10.0.',
261  E_USER_DEPRECATED
262  );
263 
264  try {
265  $classSchema = $this->‪getClassSchema($className);
266  } catch (\Exception $e) {
267  return [];
268  }
269 
270  return $classSchema->getProperty($propertyName)['tags'] ?? [];
271  }
272 
282  public function ‪getPropertyTagValues($className, $propertyName, $tag): array
283  {
284  trigger_error(
285  'Method ' . __METHOD__ . ' is deprecated and will be removed in TYPO3 v10.0.',
286  E_USER_DEPRECATED
287  );
288 
289  try {
290  $classSchema = $this->‪getClassSchema($className);
291  } catch (\Exception $e) {
292  return [];
293  }
294 
295  return $classSchema->getProperty($propertyName)['tags'][$tag] ?? [];
296  }
297 
306  public function ‪isClassTaggedWith($className, $tag): bool
307  {
308  trigger_error(
309  'Method ' . __METHOD__ . ' is deprecated and will be removed in TYPO3 v10.0.',
310  E_USER_DEPRECATED
311  );
312 
313  try {
314  $classSchema = $this->‪getClassSchema($className);
315  } catch (\Exception $e) {
316  return false;
317  }
318 
319  foreach (array_keys($classSchema->getTags()) as $tagName) {
320  if ($tagName === $tag) {
321  return true;
322  }
323  }
324 
325  return false;
326  }
327 
337  public function ‪isPropertyTaggedWith($className, $propertyName, $tag): bool
338  {
339  trigger_error(
340  'Method ' . __METHOD__ . ' is deprecated and will be removed in TYPO3 v10.0.',
341  E_USER_DEPRECATED
342  );
343 
344  try {
345  $classSchema = $this->‪getClassSchema($className);
346  } catch (\Exception $e) {
347  return false;
348  }
349 
350  $property = $classSchema->getProperty($propertyName);
351 
352  if (empty($property)) {
353  return false;
354  }
355 
356  return isset($property['tags'][$tag]);
357  }
358 
366  protected function ‪buildClassSchema($className): ClassSchema
367  {
368  try {
369  $classSchema = new ClassSchema($className);
370  } catch (\ReflectionException $e) {
371  throw new Exception\UnknownClassException($e->getMessage() . '. Reflection failed.', 1278450972, $e);
372  }
373  $this->classSchemata[$className] = $classSchema;
374  $this->dataCacheNeedsUpdate = true;
375  return $classSchema;
376  }
377 
381  public function ‪__sleep(): array
382  {
383  return [];
384  }
385 
389  public function ‪__wakeup(): void
390  {
391  $this->dataCache = null;
392  $this->dataCacheNeedsUpdate = false;
393  $this->classSchemata = [];
394  $this->cachingEnabled = false;
395  }
396 }
‪TYPO3\CMS\Extbase\Reflection\ReflectionService\getClassPropertyNames
‪array getClassPropertyNames($className)
Definition: ReflectionService.php:139
‪TYPO3\CMS\Extbase\Reflection\ReflectionService\CACHE_ENTRY_IDENTIFIER
‪const CACHE_ENTRY_IDENTIFIER
Definition: ReflectionService.php:29
‪TYPO3\CMS\Extbase\Reflection\ReflectionService\__construct
‪__construct(CacheManager $cacheManager=null)
Definition: ReflectionService.php:60
‪TYPO3\CMS\Extbase\Reflection\ReflectionService\$dataCacheNeedsUpdate
‪bool $dataCacheNeedsUpdate
Definition: ReflectionService.php:42
‪TYPO3\CMS\Extbase\Reflection\Exception
Definition: Exception.php:21
‪TYPO3\CMS\Extbase\Reflection\ReflectionService\getPropertyTagValues
‪array getPropertyTagValues($className, $propertyName, $tag)
Definition: ReflectionService.php:278
‪TYPO3\CMS\Extbase\Reflection\ReflectionService\isClassTaggedWith
‪bool isClassTaggedWith($className, $tag)
Definition: ReflectionService.php:302
‪TYPO3\CMS\Extbase\Reflection\ReflectionService\getPropertyTagsValues
‪array getPropertyTagsValues($className, $propertyName)
Definition: ReflectionService.php:253
‪TYPO3\CMS\Extbase\Reflection\ReflectionService\buildClassSchema
‪ClassSchema buildClassSchema($className)
Definition: ReflectionService.php:362
‪TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
Definition: NoSuchCacheException.php:21
‪TYPO3\CMS\Extbase\Reflection\ReflectionService\getClassSchema
‪ClassSchema getClassSchema($classNameOrObject)
Definition: ReflectionService.php:162
‪TYPO3\CMS\Extbase\Reflection\ReflectionService\getClassTagValues
‪array getClassTagValues($className, $tag)
Definition: ReflectionService.php:116
‪TYPO3\CMS\Extbase\Reflection\ReflectionService\__destruct
‪__destruct()
Definition: ReflectionService.php:78
‪TYPO3\CMS\Extbase\Reflection\ReflectionService
Definition: ReflectionService.php:27
‪TYPO3\CMS\Extbase\Reflection\ReflectionService\getMethodParameters
‪array getMethodParameters($className, $methodName)
Definition: ReflectionService.php:229
‪TYPO3\CMS\Extbase\Reflection\ReflectionService\__sleep
‪__sleep()
Definition: ReflectionService.php:377
‪TYPO3\CMS\Extbase\Reflection
Definition: ClassSchema.php:2
‪TYPO3\CMS\Extbase\Reflection\ReflectionService\$classSchemata
‪array $classSchemata
Definition: ReflectionService.php:48
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:34
‪TYPO3\CMS\Extbase\Reflection\ReflectionService\$dataCache
‪FrontendInterface $dataCache
Definition: ReflectionService.php:33
‪TYPO3\CMS\Extbase\Reflection\ReflectionService\isPropertyTaggedWith
‪bool isPropertyTaggedWith($className, $propertyName, $tag)
Definition: ReflectionService.php:333
‪TYPO3\CMS\Extbase\Reflection\ReflectionService\hasMethod
‪bool hasMethod($className, $methodName)
Definition: ReflectionService.php:180
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:21
‪TYPO3\CMS\Extbase\Reflection\ReflectionService\__wakeup
‪__wakeup()
Definition: ReflectionService.php:385
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪TYPO3\CMS\Extbase\Reflection\ReflectionService\getClassTagsValues
‪array getClassTagsValues($className)
Definition: ReflectionService.php:92
‪TYPO3\CMS\Extbase\Reflection\ReflectionService\getMethodTagsValues
‪array getMethodTagsValues($className, $methodName)
Definition: ReflectionService.php:204
‪TYPO3\CMS\Extbase\Reflection\ClassSchema
Definition: ClassSchema.php:41
‪TYPO3\CMS\Extbase\Reflection\ReflectionService\CACHE_IDENTIFIER
‪const CACHE_IDENTIFIER
Definition: ReflectionService.php:28
‪TYPO3\CMS\Extbase\Reflection\ReflectionService\$cachingEnabled
‪bool $cachingEnabled
Definition: ReflectionService.php:52