2 namespace TYPO3\CMS\Fluid\View
;
5 * This file is part of the TYPO3 CMS project.
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.
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
14 * The TYPO3 project - inspiring people to share!
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
;
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.
33 class TemplatePaths
extends \TYPO3Fluid\Fluid\View\TemplatePaths
38 protected $typoScript = [];
43 protected $templateSource;
48 protected $templatePathAndFilename;
51 * @param string $extensionKey
54 protected function getExtensionPrivateResourcesPath($extensionKey)
56 $extensionKey = trim($extensionKey);
57 if ($extensionKey && ExtensionManagementUtility
::isLoaded($extensionKey)) {
58 return ExtensionManagementUtility
::extPath($extensionKey) . 'Resources/Private/';
64 * @return ConfigurationManagerInterface
66 protected function getConfigurationManager()
68 $objectManager = GeneralUtility
::makeInstance(ObjectManager
::class);
69 $configurationManager = $objectManager->get(ConfigurationManagerInterface
::class);
70 return $configurationManager;
74 * @param string $extensionKey
77 protected function getContextSpecificViewConfiguration($extensionKey)
79 if (empty($extensionKey)) {
82 $cache = $this->getRuntimeCache();
83 $cacheIdentifier = 'viewpaths_' . $extensionKey;
84 $configuration = $cache->get($cacheIdentifier);
85 if (!empty($configuration)) {
86 return $configuration;
89 $resources = $this->getExtensionPrivateResourcesPath($extensionKey);
91 self
::CONFIG_TEMPLATEROOTPATHS
=> [$resources . 'Templates/'],
92 self
::CONFIG_PARTIALROOTPATHS
=> [$resources . 'Partials/'],
93 self
::CONFIG_LAYOUTROOTPATHS
=> [$resources . 'Layouts/']
96 $configuredPaths = [];
97 if (!empty($this->templateRootPaths
) ||
!empty($this->partialRootPaths
) ||
!empty($this->layoutRootPaths
)) {
98 // The view was configured already
100 self
::CONFIG_TEMPLATEROOTPATHS
=> $this->templateRootPaths
,
101 self
::CONFIG_PARTIALROOTPATHS
=> $this->partialRootPaths
,
102 self
::CONFIG_LAYOUTROOTPATHS
=> $this->layoutRootPaths
,
105 if (empty($this->typoScript
)) {
106 $this->typoScript
= GeneralUtility
::removeDotsFromTS(
107 $this->getConfigurationManager()->getConfiguration(ConfigurationManagerInterface
::CONFIGURATION_TYPE_FULL_TYPOSCRIPT
)
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'];
118 foreach ($paths as $name => $defaultPaths) {
119 if (!empty($configuredPaths[$name])) {
120 $paths[$name] = (array)$configuredPaths[$name] +
$defaultPaths;
124 $cache->set($cacheIdentifier, $paths);
129 * @return VariableFrontend
131 protected function getRuntimeCache()
133 return GeneralUtility
::makeInstance(CacheManager
::class)->getCache('cache_runtime');
137 * @param string|array $path
140 protected function sanitizePath($path)
142 if (is_array($path)) {
143 $paths = array_map([$this, 'sanitizePath'], $path);
144 return array_unique($paths);
146 $path = $this->ensureAbsolutePath($path);
148 $path = $this->ensureSuffixedPath($path);
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.
159 * @param mixed $reference
162 protected function ensureAbsolutePath($reference)
164 if (false === is_array($reference)) {
165 $filename = PathUtility
::isAbsolutePath($reference) ?
$reference : GeneralUtility
::getFileAbsFileName($reference);
167 foreach ($reference as &$subValue) {
168 $subValue = $this->ensureAbsolutePath($subValue);
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.
182 * @param string $packageName
185 public function fillDefaultsByPackageName($packageName)
187 $this->fillFromConfigurationArray($this->getContextSpecificViewConfiguration($packageName));
191 * Overridden setter with enforced sorting behavior
193 * @param array $templateRootPaths
196 public function setTemplateRootPaths(array $templateRootPaths)
198 parent
::setTemplateRootPaths(
199 ArrayUtility
::sortArrayWithIntegerKeys($templateRootPaths)
204 * Overridden setter with enforced sorting behavior
206 * @param array $layoutRootPaths
209 public function setLayoutRootPaths(array $layoutRootPaths)
211 parent
::setLayoutRootPaths(
212 ArrayUtility
::sortArrayWithIntegerKeys($layoutRootPaths)
217 * Overridden setter with enforced sorting behavior
219 * @param array $partialRootPaths
222 public function setPartialRootPaths(array $partialRootPaths)
224 parent
::setPartialRootPaths(
225 ArrayUtility
::sortArrayWithIntegerKeys($partialRootPaths)
230 * Public API for currently protected method. Can be dropped when switching to
231 * Fluid 1.1.0 or above.
233 * @param string $partialName
236 public function getPartialPathAndFilename($partialName)
238 return parent
::getPartialPathAndFilename($partialName);
242 * Get absolute path to template file
244 * @return string Returns the absolute path to a Fluid template file
246 public function getTemplatePathAndFilename()
248 return $this->templatePathAndFilename
;