[CLEANUP] The correct case must be used for standard PHP types in phpdoc
[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\ArrayUtility;
18 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Core\Utility\PathUtility;
21 use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
22 use TYPO3\CMS\Extbase\Object\ObjectManager;
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 (array)$this->getConfigurationManager()->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT)
100 );
101 }
102 $signature = str_replace('_', '', $extensionKey);
103 if ($this->isBackendMode() && isset($this->typoScript['module']['tx_' . $signature]['view'])) {
104 $configuredPaths = (array)$this->typoScript['module']['tx_' . $signature]['view'];
105 } elseif ($this->isFrontendMode() && isset($this->typoScript['plugin']['tx_' . $signature]['view'])) {
106 $configuredPaths = (array)$this->typoScript['plugin']['tx_' . $signature]['view'];
107 }
108 }
109
110 if (empty($configuredPaths)) {
111 return $paths;
112 }
113
114 foreach ($paths as $name => $defaultPaths) {
115 if (!empty($configuredPaths[$name])) {
116 $paths[$name] = array_merge($defaultPaths, ArrayUtility::sortArrayWithIntegerKeys((array)$configuredPaths[$name]));
117 }
118 }
119
120 return $paths;
121 }
122
123 /**
124 * Fills the path arrays with defaults, by package name.
125 * Reads those defaults from TypoScript if possible and
126 * if not defined, uses fallback paths by convention.
127 *
128 * @param string $packageName
129 */
130 public function fillDefaultsByPackageName($packageName)
131 {
132 $this->fillFromConfigurationArray($this->getContextSpecificViewConfiguration($packageName));
133 }
134
135 /**
136 * Overridden setter with enforced sorting behavior
137 *
138 * @param array $templateRootPaths
139 */
140 public function setTemplateRootPaths(array $templateRootPaths)
141 {
142 parent::setTemplateRootPaths(
143 ArrayUtility::sortArrayWithIntegerKeys($templateRootPaths)
144 );
145 }
146
147 /**
148 * Overridden setter with enforced sorting behavior
149 *
150 * @param array $layoutRootPaths
151 */
152 public function setLayoutRootPaths(array $layoutRootPaths)
153 {
154 parent::setLayoutRootPaths(
155 ArrayUtility::sortArrayWithIntegerKeys($layoutRootPaths)
156 );
157 }
158
159 /**
160 * Overridden setter with enforced sorting behavior
161 *
162 * @param array $partialRootPaths
163 */
164 public function setPartialRootPaths(array $partialRootPaths)
165 {
166 parent::setPartialRootPaths(
167 ArrayUtility::sortArrayWithIntegerKeys($partialRootPaths)
168 );
169 }
170
171 /**
172 * Public API for currently protected method. Can be dropped when switching to
173 * Fluid 1.1.0 or above.
174 *
175 * @param string $partialName
176 * @return string
177 */
178 public function getPartialPathAndFilename($partialName)
179 {
180 return parent::getPartialPathAndFilename($partialName);
181 }
182
183 /**
184 * Get absolute path to template file
185 *
186 * @return string Returns the absolute path to a Fluid template file
187 */
188 public function getTemplatePathAndFilename()
189 {
190 return $this->templatePathAndFilename;
191 }
192
193 /**
194 * Guarantees that $reference is turned into a
195 * correct, absolute path. The input can be a
196 * relative path or a FILE: or EXT: reference
197 * but cannot be a FAL resource identifier.
198 *
199 * @param mixed $reference
200 * @return string
201 */
202 protected function ensureAbsolutePath($reference)
203 {
204 if (!is_array($reference)) {
205 return PathUtility::isAbsolutePath($reference) ? $reference : GeneralUtility::getFileAbsFileName($reference);
206 }
207 foreach ($reference as &$subValue) {
208 $subValue = $this->ensureAbsolutePath($subValue);
209 }
210 return $reference;
211 }
212
213 /**
214 * @return bool
215 */
216 protected function isBackendMode()
217 {
218 return TYPO3_MODE === 'BE';
219 }
220
221 /**
222 * @return bool
223 */
224 protected function isFrontendMode()
225 {
226 return TYPO3_MODE === 'FE';
227 }
228 }