-
Notifications
You must be signed in to change notification settings - Fork 646
/
TemplatePaths.php
194 lines (172 loc) · 6.9 KB
/
TemplatePaths.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
declare(strict_types=1);
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace TYPO3\CMS\Fluid\View;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Http\ApplicationType;
use TYPO3\CMS\Core\Utility\ArrayUtility;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\PathUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
/**
* Custom implementation for template paths resolving, one which differs from the base
* implementation in that it is capable of resolving template paths based on TypoScript
* configuration when given a package name, and is aware of the Frontend/Backend contexts of TYPO3.
*
* @internal This is for internal Fluid use only.
*/
class TemplatePaths extends \TYPO3Fluid\Fluid\View\TemplatePaths
{
/**
* @var string
*/
protected $templateSource;
/**
* @var string
*/
protected $templatePathAndFilename;
protected function getExtensionPrivateResourcesPath(string $extensionKey): ?string
{
$extensionKey = trim($extensionKey);
if ($extensionKey && ExtensionManagementUtility::isLoaded($extensionKey)) {
return ExtensionManagementUtility::extPath($extensionKey) . 'Resources/Private/';
}
return null;
}
protected function getConfigurationManager(): ConfigurationManagerInterface
{
return GeneralUtility::makeInstance(ConfigurationManager::class);
}
protected function getContextSpecificViewConfiguration(string $extensionKey): array
{
if (empty($extensionKey)) {
return [];
}
$resources = $this->getExtensionPrivateResourcesPath($extensionKey);
$paths = [
self::CONFIG_TEMPLATEROOTPATHS => [$resources . 'Templates/'],
self::CONFIG_PARTIALROOTPATHS => [$resources . 'Partials/'],
self::CONFIG_LAYOUTROOTPATHS => [$resources . 'Layouts/'],
];
$configuredPaths = [];
if (!empty($this->templateRootPaths) || !empty($this->partialRootPaths) || !empty($this->layoutRootPaths)) {
// The view was configured already
$configuredPaths = [
self::CONFIG_TEMPLATEROOTPATHS => $this->templateRootPaths,
self::CONFIG_PARTIALROOTPATHS => $this->partialRootPaths,
self::CONFIG_LAYOUTROOTPATHS => $this->layoutRootPaths,
];
} else {
$typoScript = $this->getConfigurationManager()->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT);
$signature = str_replace('_', '', $extensionKey);
if ($this->isBackendMode() && isset($typoScript['module.']['tx_' . $signature . '.']['view.'])) {
$configuredPaths = (array)$typoScript['module.']['tx_' . $signature . '.']['view.'];
$configuredPaths = GeneralUtility::removeDotsFromTS($configuredPaths);
} elseif ($this->isFrontendMode() && isset($typoScript['plugin.']['tx_' . $signature . '.']['view.'])) {
$configuredPaths = (array)$typoScript['plugin.']['tx_' . $signature . '.']['view.'];
$configuredPaths = GeneralUtility::removeDotsFromTS($configuredPaths);
}
}
if (empty($configuredPaths)) {
return $paths;
}
foreach ($paths as $name => $defaultPaths) {
if (!empty($configuredPaths[$name])) {
$configured = ArrayUtility::sortArrayWithIntegerKeys((array)$configuredPaths[$name]);
// calculate implicit default paths which have not been explicitly configured
$implicitDefaultPaths = array_diff($defaultPaths, $configured);
// prepend implicit default paths (which have not been found in configured paths), as fallbacks
$paths[$name] = array_merge($implicitDefaultPaths, $configured);
}
}
return $paths;
}
/**
* Fills the path arrays with defaults, by package name.
* Reads those defaults from TypoScript if possible and
* if not defined, uses fallback paths by convention.
*
* @param string $packageName
*/
public function fillDefaultsByPackageName($packageName): void
{
$this->fillFromConfigurationArray($this->getContextSpecificViewConfiguration($packageName));
}
/**
* Overridden setter with enforced sorting behavior
*/
public function setTemplateRootPaths(array $templateRootPaths): void
{
parent::setTemplateRootPaths(
ArrayUtility::sortArrayWithIntegerKeys($templateRootPaths)
);
}
/**
* Overridden setter with enforced sorting behavior
*/
public function setLayoutRootPaths(array $layoutRootPaths): void
{
parent::setLayoutRootPaths(
ArrayUtility::sortArrayWithIntegerKeys($layoutRootPaths)
);
}
/**
* Overridden setter with enforced sorting behavior
*/
public function setPartialRootPaths(array $partialRootPaths): void
{
parent::setPartialRootPaths(
ArrayUtility::sortArrayWithIntegerKeys($partialRootPaths)
);
}
/**
* Get absolute path to template file
*
* @return string Returns the absolute path to a Fluid template file
*/
public function getTemplatePathAndFilename(): string
{
return $this->templatePathAndFilename;
}
/**
* Guarantees that $reference is turned into a
* correct, absolute path. The input can be a
* relative path or a FILE: or EXT: reference
* but cannot be a FAL resource identifier.
*
* @param string|array $reference
*/
protected function ensureAbsolutePath($reference): array|string
{
if (!is_array($reference)) {
return PathUtility::isAbsolutePath($reference) ? $reference : GeneralUtility::getFileAbsFileName($reference);
}
foreach ($reference as &$subValue) {
$subValue = $this->ensureAbsolutePath($subValue);
}
return $reference;
}
protected function isBackendMode(): bool
{
return ($GLOBALS['TYPO3_REQUEST'] ?? null) instanceof ServerRequestInterface
&& ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST'])->isBackend();
}
protected function isFrontendMode(): bool
{
return ($GLOBALS['TYPO3_REQUEST'] ?? null) instanceof ServerRequestInterface
&& ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST'])->isFrontend();
}
}