5c4b665b80d58380ecf70f1b7f9758092c562293
[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 (TYPO3_MODE === 'BE' && isset($this->typoScript['module']['tx_' . $signature]['view'])) {
112 $configuredPaths = (array)$this->typoScript['module']['tx_' . $signature]['view'];
113 } elseif (TYPO3_MODE === 'FE' && isset($this->typoScript['plugin']['tx_' . $signature]['view'])) {
114 $configuredPaths = (array)$this->typoScript['plugin']['tx_' . $signature]['view'];
115 }
116 }
117
118 foreach ($paths as $name => $defaultPaths) {
119 if (!empty($configuredPaths[$name])) {
120 $paths[$name] = $defaultPaths + (array)$configuredPaths[$name];
121 }
122 }
123
124 $cache->set($cacheIdentifier, $paths);
125 return $paths;
126 }
127
128 /**
129 * @return VariableFrontend
130 */
131 protected function getRuntimeCache()
132 {
133 return GeneralUtility::makeInstance(CacheManager::class)->getCache('cache_runtime');
134 }
135
136 /**
137 * @param string|array $path
138 * @return string
139 */
140 protected function sanitizePath($path)
141 {
142 if (is_array($path)) {
143 $paths = array_map([$this, 'sanitizePath'], $path);
144 return array_unique($paths);
145 }
146 $path = $this->ensureAbsolutePath($path);
147 if (is_dir($path)) {
148 $path = $this->ensureSuffixedPath($path);
149 }
150 return $path;
151 }
152
153 /**
154 * Guarantees that $reference is turned into a
155 * correct, absolute path. The input can be a
156 * relative path or a FILE: or EXT: reference
157 * but cannot be a FAL resource identifier.
158 *
159 * @param mixed $reference
160 * @return string
161 */
162 protected function ensureAbsolutePath($reference)
163 {
164 if (false === is_array($reference)) {
165 $filename = PathUtility::isAbsolutePath($reference) ? $reference : GeneralUtility::getFileAbsFileName($reference);
166 } else {
167 foreach ($reference as &$subValue) {
168 $subValue = $this->ensureAbsolutePath($subValue);
169 }
170
171 return $reference;
172 }
173
174 return $filename;
175 }
176
177 /**
178 * Fills the path arrays with defaults, by package name.
179 * Reads those defaults from TypoScript if possible and
180 * if not defined, uses fallback paths by convention.
181 *
182 * @param string $packageName
183 * @return void
184 */
185 public function fillDefaultsByPackageName($packageName)
186 {
187 $this->fillFromConfigurationArray($this->getContextSpecificViewConfiguration($packageName));
188 }
189
190 /**
191 * Overridden setter with enforced sorting behavior
192 *
193 * @param array $templateRootPaths
194 * @return void
195 */
196 public function setTemplateRootPaths(array $templateRootPaths)
197 {
198 parent::setTemplateRootPaths(
199 ArrayUtility::sortArrayWithIntegerKeys($templateRootPaths)
200 );
201 }
202
203 /**
204 * Overridden setter with enforced sorting behavior
205 *
206 * @param array $layoutRootPaths
207 * @return void
208 */
209 public function setLayoutRootPaths(array $layoutRootPaths)
210 {
211 parent::setLayoutRootPaths(
212 ArrayUtility::sortArrayWithIntegerKeys($layoutRootPaths)
213 );
214 }
215
216 /**
217 * Overridden setter with enforced sorting behavior
218 *
219 * @param array $partialRootPaths
220 * @return void
221 */
222 public function setPartialRootPaths(array $partialRootPaths)
223 {
224 parent::setPartialRootPaths(
225 ArrayUtility::sortArrayWithIntegerKeys($partialRootPaths)
226 );
227 }
228
229 /**
230 * Public API for currently protected method. Can be dropped when switching to
231 * Fluid 1.1.0 or above.
232 *
233 * @param string $partialName
234 * @return string
235 */
236 public function getPartialPathAndFilename($partialName)
237 {
238 return parent::getPartialPathAndFilename($partialName);
239 }
240
241 /**
242 * Get absolute path to template file
243 *
244 * @return string Returns the absolute path to a Fluid template file
245 */
246 public function getTemplatePathAndFilename()
247 {
248 return $this->templatePathAndFilename;
249 }
250 }