[TASK] Revert "Remove runtime cache and early return from TemplatePaths"
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / View / TemplatePaths.php
1 <?php
2 namespace TYPO3\CMS\Fluid\View;
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\Cache\Frontend\VariableFrontend;
19 use TYPO3\CMS\Core\Utility\ArrayUtility;
20 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use TYPO3\CMS\Core\Utility\PathUtility;
23 use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
24 use TYPO3\CMS\Extbase\Object\ObjectManager;
25
26 /**
27 * Class TemplatePaths
28 *
29 * Custom implementation for template paths resolving, one which differs from the base
30 * implementation in that it is capable of resolving template paths based on TypoScript
31 * configuration when given a package name, and is aware of the Frontend/Backend contexts of TYPO3.
32 */
33 class TemplatePaths extends \TYPO3Fluid\Fluid\View\TemplatePaths
34 {
35 /**
36 * @var array
37 */
38 protected $typoScript = [];
39
40 /**
41 * @var string
42 */
43 protected $templateSource;
44
45 /**
46 * @var string
47 */
48 protected $templatePathAndFilename;
49
50 /**
51 * @param string $extensionKey
52 * @return string|NULL
53 */
54 protected function getExtensionPrivateResourcesPath($extensionKey)
55 {
56 $extensionKey = trim($extensionKey);
57 if ($extensionKey && ExtensionManagementUtility::isLoaded($extensionKey)) {
58 return ExtensionManagementUtility::extPath($extensionKey) . 'Resources/Private/';
59 }
60 return null;
61 }
62
63 /**
64 * @return ConfigurationManagerInterface
65 */
66 protected function getConfigurationManager()
67 {
68 $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
69 $configurationManager = $objectManager->get(ConfigurationManagerInterface::class);
70 return $configurationManager;
71 }
72
73 /**
74 * @param string $extensionKey
75 * @return array
76 */
77 protected function getContextSpecificViewConfiguration($extensionKey)
78 {
79 if (empty($extensionKey)) {
80 return [];
81 }
82 $cache = $this->getRuntimeCache();
83 $cacheIdentifier = 'viewpaths_' . $extensionKey;
84 $configuration = $cache->get($cacheIdentifier);
85 if (is_array($configuration)) {
86 return $configuration;
87 }
88
89 $configuredPaths = [];
90 if (!empty($this->templateRootPaths) || !empty($this->partialRootPaths) || !empty($this->layoutRootPaths)) {
91 // The view was manually configured - early return to avoid caching this configuration.
92 return [
93 self::CONFIG_TEMPLATEROOTPATHS => $this->templateRootPaths,
94 self::CONFIG_PARTIALROOTPATHS => $this->partialRootPaths,
95 self::CONFIG_LAYOUTROOTPATHS => $this->layoutRootPaths,
96 ];
97 }
98
99 $resources = $this->getExtensionPrivateResourcesPath($extensionKey);
100 $paths = [
101 self::CONFIG_TEMPLATEROOTPATHS => [$resources . 'Templates/'],
102 self::CONFIG_PARTIALROOTPATHS => [$resources . 'Partials/'],
103 self::CONFIG_LAYOUTROOTPATHS => [$resources . 'Layouts/']
104 ];
105
106 if (empty($this->typoScript)) {
107 $this->typoScript = (array)$this->getConfigurationManager()->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT);
108 }
109
110 $signature = str_replace('_', '', $extensionKey);
111 if ($this->isBackendMode() && isset($this->typoScript['module.']['tx_' . $signature . '.']['view.'])) {
112 $configuredPaths = (array)$this->typoScript['module.']['tx_' . $signature . '.']['view.'];
113 } elseif ($this->isFrontendMode() && isset($this->typoScript['plugin.']['tx_' . $signature . '.']['view.'])) {
114 $configuredPaths = (array)$this->typoScript['plugin.']['tx_' . $signature . '.']['view.'];
115 }
116 $configuredPaths = GeneralUtility::removeDotsFromTS($configuredPaths);
117
118 foreach ($paths as $name => $defaultPaths) {
119 if (!empty($configuredPaths[$name])) {
120 $paths[$name] = array_merge($defaultPaths, ArrayUtility::sortArrayWithIntegerKeys((array)$configuredPaths[$name]));
121 }
122 }
123
124 $cache->set($cacheIdentifier, $paths);
125
126 return $paths;
127 }
128
129 /**
130 * Fills the path arrays with defaults, by package name.
131 * Reads those defaults from TypoScript if possible and
132 * if not defined, uses fallback paths by convention.
133 *
134 * @param string $packageName
135 */
136 public function fillDefaultsByPackageName($packageName)
137 {
138 $this->fillFromConfigurationArray($this->getContextSpecificViewConfiguration($packageName));
139 }
140
141 /**
142 * Overridden setter with enforced sorting behavior
143 *
144 * @param array $templateRootPaths
145 */
146 public function setTemplateRootPaths(array $templateRootPaths)
147 {
148 parent::setTemplateRootPaths(
149 ArrayUtility::sortArrayWithIntegerKeys($templateRootPaths)
150 );
151 }
152
153 /**
154 * Overridden setter with enforced sorting behavior
155 *
156 * @param array $layoutRootPaths
157 */
158 public function setLayoutRootPaths(array $layoutRootPaths)
159 {
160 parent::setLayoutRootPaths(
161 ArrayUtility::sortArrayWithIntegerKeys($layoutRootPaths)
162 );
163 }
164
165 /**
166 * Overridden setter with enforced sorting behavior
167 *
168 * @param array $partialRootPaths
169 */
170 public function setPartialRootPaths(array $partialRootPaths)
171 {
172 parent::setPartialRootPaths(
173 ArrayUtility::sortArrayWithIntegerKeys($partialRootPaths)
174 );
175 }
176
177 /**
178 * Public API for currently protected method. Can be dropped when switching to
179 * Fluid 1.1.0 or above.
180 *
181 * @param string $partialName
182 * @return string
183 */
184 public function getPartialPathAndFilename($partialName)
185 {
186 return parent::getPartialPathAndFilename($partialName);
187 }
188
189 /**
190 * Get absolute path to template file
191 *
192 * @return string Returns the absolute path to a Fluid template file
193 */
194 public function getTemplatePathAndFilename()
195 {
196 return $this->templatePathAndFilename;
197 }
198
199 /**
200 * Guarantees that $reference is turned into a
201 * correct, absolute path. The input can be a
202 * relative path or a FILE: or EXT: reference
203 * but cannot be a FAL resource identifier.
204 *
205 * @param mixed $reference
206 * @return string
207 */
208 protected function ensureAbsolutePath($reference)
209 {
210 if (!is_array($reference)) {
211 return PathUtility::isAbsolutePath($reference) ? $reference : GeneralUtility::getFileAbsFileName($reference);
212 }
213 foreach ($reference as &$subValue) {
214 $subValue = $this->ensureAbsolutePath($subValue);
215 }
216 return $reference;
217 }
218
219 /**
220 * @return bool
221 */
222 protected function isBackendMode()
223 {
224 return TYPO3_MODE === 'BE';
225 }
226
227 /**
228 * @return bool
229 */
230 protected function isFrontendMode()
231 {
232 return TYPO3_MODE === 'FE';
233 }
234
235 /**
236 * @return VariableFrontend
237 */
238 protected function getRuntimeCache()
239 {
240 return GeneralUtility::makeInstance(CacheManager::class)->getCache('cache_runtime');
241 }
242 }