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