[BUGFIX] Revert "Improve performance of Fluid 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 (!empty($configuration)) {
86 return $configuration;
87 }
88
89 $resources = $this->getExtensionPrivateResourcesPath($extensionKey);
90 $paths = [
91 self::CONFIG_TEMPLATEROOTPATHS => [$resources . 'Templates/'],
92 self::CONFIG_PARTIALROOTPATHS => [$resources . 'Partials/'],
93 self::CONFIG_LAYOUTROOTPATHS => [$resources . 'Layouts/']
94 ];
95
96 $configuredPaths = [];
97 if (!empty($this->templateRootPaths) || !empty($this->partialRootPaths) || !empty($this->layoutRootPaths)) {
98 // The view was configured already
99 $configuredPaths = [
100 self::CONFIG_TEMPLATEROOTPATHS => $this->templateRootPaths,
101 self::CONFIG_PARTIALROOTPATHS => $this->partialRootPaths,
102 self::CONFIG_LAYOUTROOTPATHS => $this->layoutRootPaths,
103 ];
104 } else {
105 if (empty($this->typoScript)) {
106 $this->typoScript = GeneralUtility::removeDotsFromTS(
107 (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 }
117
118 if (empty($configuredPaths)) {
119 return $paths;
120 }
121
122 foreach ($paths as $name => $defaultPaths) {
123 if (!empty($configuredPaths[$name])) {
124 $paths[$name] = array_merge($defaultPaths, ArrayUtility::sortArrayWithIntegerKeys((array)$configuredPaths[$name]));
125 }
126 }
127
128 $cache->set($cacheIdentifier, $paths);
129
130 return $paths;
131 }
132
133 /**
134 * Fills the path arrays with defaults, by package name.
135 * Reads those defaults from TypoScript if possible and
136 * if not defined, uses fallback paths by convention.
137 *
138 * @param string $packageName
139 */
140 public function fillDefaultsByPackageName($packageName)
141 {
142 $this->fillFromConfigurationArray($this->getContextSpecificViewConfiguration($packageName));
143 }
144
145 /**
146 * Overridden setter with enforced sorting behavior
147 *
148 * @param array $templateRootPaths
149 */
150 public function setTemplateRootPaths(array $templateRootPaths)
151 {
152 parent::setTemplateRootPaths(
153 ArrayUtility::sortArrayWithIntegerKeys($templateRootPaths)
154 );
155 }
156
157 /**
158 * Overridden setter with enforced sorting behavior
159 *
160 * @param array $layoutRootPaths
161 */
162 public function setLayoutRootPaths(array $layoutRootPaths)
163 {
164 parent::setLayoutRootPaths(
165 ArrayUtility::sortArrayWithIntegerKeys($layoutRootPaths)
166 );
167 }
168
169 /**
170 * Overridden setter with enforced sorting behavior
171 *
172 * @param array $partialRootPaths
173 */
174 public function setPartialRootPaths(array $partialRootPaths)
175 {
176 parent::setPartialRootPaths(
177 ArrayUtility::sortArrayWithIntegerKeys($partialRootPaths)
178 );
179 }
180
181 /**
182 * Public API for currently protected method. Can be dropped when switching to
183 * Fluid 1.1.0 or above.
184 *
185 * @param string $partialName
186 * @return string
187 */
188 public function getPartialPathAndFilename($partialName)
189 {
190 return parent::getPartialPathAndFilename($partialName);
191 }
192
193 /**
194 * Get absolute path to template file
195 *
196 * @return string Returns the absolute path to a Fluid template file
197 */
198 public function getTemplatePathAndFilename()
199 {
200 return $this->templatePathAndFilename;
201 }
202
203 /**
204 * Guarantees that $reference is turned into a
205 * correct, absolute path. The input can be a
206 * relative path or a FILE: or EXT: reference
207 * but cannot be a FAL resource identifier.
208 *
209 * @param mixed $reference
210 * @return string
211 */
212 protected function ensureAbsolutePath($reference)
213 {
214 if (!is_array($reference)) {
215 return PathUtility::isAbsolutePath($reference) ? $reference : GeneralUtility::getFileAbsFileName($reference);
216 }
217 foreach ($reference as &$subValue) {
218 $subValue = $this->ensureAbsolutePath($subValue);
219 }
220 return $reference;
221 }
222
223 /**
224 * @return bool
225 */
226 protected function isBackendMode()
227 {
228 return TYPO3_MODE === 'BE';
229 }
230
231 /**
232 * @return bool
233 */
234 protected function isFrontendMode()
235 {
236 return TYPO3_MODE === 'FE';
237 }
238
239 /**
240 * @return VariableFrontend
241 */
242 protected function getRuntimeCache()
243 {
244 return GeneralUtility::makeInstance(CacheManager::class)->getCache('cache_runtime');
245 }
246 }