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