[TASK] Remove redundant toArray method on Fluid TemplatePaths
[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
23 /**
24 * Class TemplatePaths
25 *
26 * Custom implementation for template paths resolving, one which differs from the base
27 * implementation in that it is capable of resolving template paths based on TypoScript
28 * configuration when given a package name, and is aware of the Frontend/Backend contexts of TYPO3.
29 */
30 class TemplatePaths extends \TYPO3Fluid\Fluid\View\TemplatePaths
31 {
32 /**
33 * @var array
34 */
35 protected $typoScript = array();
36
37 /**
38 * @var string
39 */
40 protected $templateSource;
41
42 /**
43 * @var string
44 */
45 protected $templatePathAndFilename;
46
47 /**
48 * @param string $extensionKey
49 * @return string|NULL
50 */
51 protected function getExtensionPrivateResourcesPath($extensionKey)
52 {
53 $extensionKey = trim($extensionKey);
54 if ($extensionKey && ExtensionManagementUtility::isLoaded($extensionKey)) {
55 return ExtensionManagementUtility::extPath($extensionKey) . 'Resources/Private/';
56 }
57 return null;
58 }
59
60 /**
61 * @return ConfigurationManagerInterface
62 */
63 protected function getConfigurationManager()
64 {
65 $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
66 $configurationManager = $objectManager->get(ConfigurationManagerInterface::class);
67 return $configurationManager;
68 }
69
70 /**
71 * @param string $extensionKey
72 * @return array
73 */
74 protected function getContextSpecificViewConfiguration($extensionKey)
75 {
76 if (empty($extensionKey)) {
77 return array();
78 }
79 if (empty($this->typoScript)) {
80 $this->typoScript = GeneralUtility::removeDotsFromTS(
81 $this->getConfigurationManager()->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT)
82 );
83 }
84 $signature = str_replace('_', '', $extensionKey);
85 $resources = $this->getExtensionPrivateResourcesPath($extensionKey);
86 $configuration = array();
87 $paths = array(
88 self::CONFIG_TEMPLATEROOTPATHS => array($resources . 'Templates/'),
89 self::CONFIG_PARTIALROOTPATHS => array($resources . 'Partials/'),
90 self::CONFIG_LAYOUTROOTPATHS => array($resources . 'Layouts/')
91 );
92 if (TYPO3_MODE === 'BE' && isset($this->typoScript['module']['tx_' . $signature]['view'])) {
93 $configuration = (array) $this->typoScript['module']['tx_' . $signature]['view'];
94 } elseif (TYPO3_MODE === 'FE' && isset($this->typoScript['plugin']['tx_' . $signature]['view'])) {
95 $configuration = (array) $this->typoScript['plugin']['tx_' . $signature]['view'];
96 }
97 foreach ($paths as $name => $values) {
98 $paths[$name] = $values + (array) $configuration[$name];
99 }
100 return $paths;
101 }
102
103 /**
104 * @param string|array $path
105 * @return string
106 */
107 protected function sanitizePath($path)
108 {
109 if (is_array($path)) {
110 $paths = array_map(array($this, 'sanitizePath'), $path);
111 return array_unique($paths);
112 }
113 $path = $this->ensureAbsolutePath($path);
114 if (is_dir($path)) {
115 $path = $this->ensureSuffixedPath($path);
116 }
117 return $path;
118 }
119
120 /**
121 * Guarantees that $reference is turned into a
122 * correct, absolute path. The input can be a
123 * relative path or a FILE: or EXT: reference
124 * but cannot be a FAL resource identifier.
125 *
126 * @param mixed $reference
127 * @return string
128 */
129 protected function ensureAbsolutePath($reference)
130 {
131 if (false === is_array($reference)) {
132 $filename = PathUtility::isAbsolutePath($reference) ? $reference : GeneralUtility::getFileAbsFileName($reference);
133 } else {
134 foreach ($reference as &$subValue) {
135 $subValue = $this->ensureAbsolutePath($subValue);
136 }
137
138 return $reference;
139 }
140
141 return $filename;
142 }
143
144 /**
145 * Fills the path arrays with defaults, by package name.
146 * Reads those defaults from TypoScript if possible and
147 * if not defined, uses fallback paths by convention.
148 *
149 * @param string $packageName
150 * @return void
151 */
152 public function fillDefaultsByPackageName($packageName)
153 {
154 $this->fillFromConfigurationArray($this->getContextSpecificViewConfiguration($packageName));
155 }
156 }