TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
TemplatePaths.php
Go to the documentation of this file.
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 
25 
33 class TemplatePaths extends \TYPO3Fluid\Fluid\View\TemplatePaths
34 {
38  protected $typoScript = [];
39 
43  protected $templateSource;
44 
49 
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 
66  protected function getConfigurationManager()
67  {
68  $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
69  $configurationManager = $objectManager->get(ConfigurationManagerInterface::class);
70  return $configurationManager;
71  }
72 
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(
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] = (array)$configuredPaths[$name] + $defaultPaths;
121  }
122  }
123 
124  $cache->set($cacheIdentifier, $paths);
125  return $paths;
126  }
127 
131  protected function getRuntimeCache()
132  {
133  return GeneralUtility::makeInstance(CacheManager::class)->getCache('cache_runtime');
134  }
135 
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 
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 
185  public function fillDefaultsByPackageName($packageName)
186  {
187  $this->fillFromConfigurationArray($this->getContextSpecificViewConfiguration($packageName));
188  }
189 
196  public function setTemplateRootPaths(array $templateRootPaths)
197  {
198  parent::setTemplateRootPaths(
199  ArrayUtility::sortArrayWithIntegerKeys($templateRootPaths)
200  );
201  }
202 
209  public function setLayoutRootPaths(array $layoutRootPaths)
210  {
211  parent::setLayoutRootPaths(
213  );
214  }
215 
222  public function setPartialRootPaths(array $partialRootPaths)
223  {
224  parent::setPartialRootPaths(
225  ArrayUtility::sortArrayWithIntegerKeys($partialRootPaths)
226  );
227  }
228 
236  public function getPartialPathAndFilename($partialName)
237  {
238  return parent::getPartialPathAndFilename($partialName);
239  }
240 }
setPartialRootPaths(array $partialRootPaths)
setLayoutRootPaths(array $layoutRootPaths)
getExtensionPrivateResourcesPath($extensionKey)
getContextSpecificViewConfiguration($extensionKey)
setTemplateRootPaths(array $templateRootPaths)
static makeInstance($className,...$constructorArguments)
static getFileAbsFileName($filename, $_=null, $_2=null)