‪TYPO3CMS  10.4
ReflectionService.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
25 
31 {
35  private static ‪$cacheEntryIdentifier;
36 
40  protected ‪$dataCache;
41 
50  protected ‪$dataCacheNeedsUpdate = false;
51 
57  protected ‪$classSchemata = [];
58 
62  private ‪$cachingEnabled = false;
63 
70  public function ‪__construct(‪CacheManager $cacheManager = null)
71  {
72  if ($cacheManager instanceof ‪CacheManager) {
73  try {
74  $this->dataCache = $cacheManager->getCache('extbase');
75  $this->cachingEnabled = true;
76  } catch (‪NoSuchCacheException $ignoredException) {
77  $this->cachingEnabled = false;
78  }
79 
80  if ($this->cachingEnabled) {
81  static::$cacheEntryIdentifier = 'ClassSchemata_' . sha1((string)(new ‪Typo3Version()) . ‪Environment::getProjectPath());
82  if ((‪$classSchemata = $this->dataCache->get(static::$cacheEntryIdentifier)) !== false) {
83  $this->classSchemata = ‪$classSchemata;
84  }
85  }
86  }
87  }
88 
89  public function ‪__destruct()
90  {
91  if ($this->dataCacheNeedsUpdate && $this->cachingEnabled) {
92  $this->dataCache->set(static::$cacheEntryIdentifier, $this->classSchemata);
93  }
94  }
95 
103  public function ‪getClassSchema($classNameOrObject): ClassSchema
104  {
105  $className = is_object($classNameOrObject) ? get_class($classNameOrObject) : $classNameOrObject;
106  if (isset($this->classSchemata[$className])) {
107  return $this->classSchemata[$className];
108  }
109 
110  return $this->‪buildClassSchema($className);
111  }
112 
120  protected function ‪buildClassSchema($className): ClassSchema
121  {
122  try {
123  $classSchema = new ClassSchema($className);
124  } catch (\ReflectionException $e) {
125  throw new UnknownClassException($e->getMessage() . '. Reflection failed.', 1278450972, $e);
126  }
127  $this->classSchemata[$className] = $classSchema;
128  $this->dataCacheNeedsUpdate = true;
129  return $classSchema;
130  }
131 
135  public function ‪__sleep(): array
136  {
137  return [];
138  }
139 
143  public function ‪__wakeup(): void
144  {
145  $this->dataCache = null;
146  $this->dataCacheNeedsUpdate = false;
147  $this->classSchemata = [];
148  $this->cachingEnabled = false;
149  }
150 }
‪TYPO3\CMS\Core\Information\Typo3Version
Definition: Typo3Version.php:21
‪TYPO3\CMS\Extbase\Reflection\ReflectionService\__construct
‪__construct(CacheManager $cacheManager=null)
Definition: ReflectionService.php:65
‪TYPO3\CMS\Extbase\Reflection\ReflectionService\$dataCacheNeedsUpdate
‪bool $dataCacheNeedsUpdate
Definition: ReflectionService.php:47
‪TYPO3\CMS\Extbase\Reflection\ReflectionService\buildClassSchema
‪ClassSchema buildClassSchema($className)
Definition: ReflectionService.php:115
‪TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
Definition: NoSuchCacheException.php:24
‪TYPO3\CMS\Extbase\Reflection\ReflectionService\getClassSchema
‪ClassSchema getClassSchema($classNameOrObject)
Definition: ReflectionService.php:98
‪TYPO3\CMS\Extbase\Reflection\ReflectionService\__destruct
‪__destruct()
Definition: ReflectionService.php:84
‪TYPO3\CMS\Extbase\Reflection\ReflectionService
Definition: ReflectionService.php:31
‪TYPO3\CMS\Core\Core\Environment\getProjectPath
‪static string getProjectPath()
Definition: Environment.php:169
‪TYPO3\CMS\Extbase\Reflection\Exception\UnknownClassException
Definition: UnknownClassException.php:26
‪TYPO3\CMS\Extbase\Reflection\ReflectionService\__sleep
‪__sleep()
Definition: ReflectionService.php:130
‪TYPO3\CMS\Extbase\Reflection
‪TYPO3\CMS\Extbase\Reflection\ReflectionService\$classSchemata
‪array $classSchemata
Definition: ReflectionService.php:53
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:35
‪TYPO3\CMS\Extbase\Reflection\ReflectionService\$dataCache
‪FrontendInterface $dataCache
Definition: ReflectionService.php:38
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:22
‪TYPO3\CMS\Extbase\Reflection\ReflectionService\__wakeup
‪__wakeup()
Definition: ReflectionService.php:138
‪TYPO3\CMS\Extbase\Reflection\ReflectionService\$cacheEntryIdentifier
‪static string $cacheEntryIdentifier
Definition: ReflectionService.php:34
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:23
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:40
‪TYPO3\CMS\Extbase\Reflection\ClassSchema
Definition: ClassSchema.php:55
‪TYPO3\CMS\Extbase\Reflection\ReflectionService\$cachingEnabled
‪bool $cachingEnabled
Definition: ReflectionService.php:57