[!!!][TASK] Consolidate extbase caches
[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\Core\Environment;
19 use TYPO3\CMS\Core\SingletonInterface;
20
21 /**
22 * Reflection service for acquiring reflection based information.
23 * Originally based on the TYPO3.Flow reflection service.
24 */
25 class ReflectionService implements SingletonInterface
26 {
27 /**
28 * @var string
29 */
30 private static $cacheEntryIdentifier;
31
32 /**
33 * @var \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend
34 */
35 protected $dataCache;
36
37 /**
38 * Indicates whether the Reflection cache needs to be updated.
39 *
40 * This flag needs to be set as soon as new Reflection information was
41 * created.
42 *
43 * @var bool
44 */
45 protected $dataCacheNeedsUpdate = false;
46
47 /**
48 * Local cache for Class schemata
49 *
50 * @var array
51 */
52 protected $classSchemata = [];
53
54 /**
55 * @var bool
56 */
57 private $cachingEnabled = false;
58
59 /**
60 * If not $cacheManager is injected, the reflection service does not
61 * cache any data, useful for testing this service in unit tests.
62 *
63 * @param CacheManager $cacheManager
64 */
65 public function __construct(CacheManager $cacheManager = null)
66 {
67 if ($cacheManager instanceof CacheManager && $cacheManager->hasCache('extbase')) {
68 $this->cachingEnabled = true;
69 $this->dataCache = $cacheManager->getCache('extbase');
70
71 static::$cacheEntryIdentifier = 'ClassSchemata_' . sha1(TYPO3_version . Environment::getProjectPath());
72 if (($classSchemata = $this->dataCache->get(static::$cacheEntryIdentifier)) !== false) {
73 $this->classSchemata = $classSchemata;
74 }
75 }
76 }
77
78 public function __destruct()
79 {
80 if ($this->dataCacheNeedsUpdate && $this->cachingEnabled) {
81 $this->dataCache->set(static::$cacheEntryIdentifier, $this->classSchemata);
82 }
83 }
84
85 /**
86 * Returns the class schema for the given class
87 *
88 * @param mixed $classNameOrObject The class name or an object
89 * @return ClassSchema
90 * @throws \TYPO3\CMS\Extbase\Reflection\Exception\UnknownClassException
91 */
92 public function getClassSchema($classNameOrObject): ClassSchema
93 {
94 $className = is_object($classNameOrObject) ? get_class($classNameOrObject) : $classNameOrObject;
95 if (isset($this->classSchemata[$className])) {
96 return $this->classSchemata[$className];
97 }
98
99 return $this->buildClassSchema($className);
100 }
101
102 /**
103 * Builds class schemata from classes annotated as entities or value objects
104 *
105 * @param string $className
106 * @throws Exception\UnknownClassException
107 * @return ClassSchema The class schema
108 */
109 protected function buildClassSchema($className): ClassSchema
110 {
111 try {
112 $classSchema = new ClassSchema($className);
113 } catch (\ReflectionException $e) {
114 throw new Exception\UnknownClassException($e->getMessage() . '. Reflection failed.', 1278450972, $e);
115 }
116 $this->classSchemata[$className] = $classSchema;
117 $this->dataCacheNeedsUpdate = true;
118 return $classSchema;
119 }
120 }