-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoadAssetsViewHelper.php
More file actions
244 lines (217 loc) · 7.46 KB
/
Copy pathLoadAssetsViewHelper.php
File metadata and controls
244 lines (217 loc) · 7.46 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php
namespace Fab\Formule\ViewHelpers;
/*
* This file is part of the Fab/Formule project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/
use Fab\Formule\Service\TemplateService;
use FluidTYPO3\Vhs\Asset;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Page\PageRenderer;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\PathUtility;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
/**
* View helper to load a JavaScript file
*/
class LoadAssetsViewHelper extends AbstractViewHelper
{
const TYPE_JS = 'js';
const TYPE_CSS = 'css';
/**
* @return void
*/
public function initializeArguments(): void
{
$this->registerArgument('footer', 'bool', '', false, true);
$this->registerArgument('type', 'string', '', false, self::TYPE_JS);
}
public function render(): void
{
$footer = $this->arguments['footer'];
$type = $this->arguments['type'];
// Get variables
$settings = $this->templateVariableContainer->get('settings');
// Inline code
$rawInlineCode = $this->renderChildren();
$inlineCode = $this->sanitizeInlineCode($rawInlineCode ?? '');
$name = $this->computeName($inlineCode);
if ($this->hasInlineCode($inlineCode)) {
if ($this->shouldLoadByVhs($settings)) {
$this->loadByVhsInline($name, $inlineCode, $footer, $type);
} else {
$this->loadByCorePageRenderInline($name, $inlineCode, $footer, $type);
}
} else {
$templateService = $this->getTemplateService($settings['template']);
foreach ($templateService->getAssets() as $asset) {
if ($this->shouldLoadByVhs($settings)) {
$asset['name'] = md5($asset['path']);
$this->loadByVhs($asset);
} else {
$this->loadByCorePageRender($asset);
}
}
}
}
/**
* @param string $inlineCode
* @return string
*/
protected function computeName(string $inlineCode): string
{
$contentElement = $this->templateVariableContainer->get('contentElement');
$inlineCodeExtract = substr(trim($inlineCode), 0, 100);
return md5($contentElement['uid'] . $inlineCodeExtract);
}
/**
* @param string $inlineCode
* @return bool
*/
protected function hasInlineCode(string $inlineCode): bool
{
return !empty(trim($inlineCode));
}
/**
* @param string $inlineCode
* @return string
*/
protected function sanitizeInlineCode(string $inlineCode): string
{
if (!empty($inlineCode)) {
$inlineCode = preg_replace('#<script(.*?)>(.*)</script>#is', '$2', $inlineCode);
}
return $inlineCode;
}
/**
* @param string $name
* @param string $content
* @param bool $footer
* @param string $type
*/
protected function loadByVhsInline(string $name, string $content, bool $footer = true, $type = self::TYPE_JS)
{
$configuration = [
'content' => $content,
// Asset::createFromSettings expects 'dependencies' to be an array.
// Previously this was a string which raised InvalidTypeException.
'dependencies' => ['mainJs'], # could be configurable.
#'standalone' => false,
#'rewrite' => false,
'movable' => $footer,
'type' => $type,
'name' => $name,
];
Asset::createFromSettings($configuration);
}
/**
* @param array $asset
* @return void
*/
protected function loadByVhs(array $asset)
{
if (Environment::getContext()->isDevelopment()) {
$developmentFile = $this->getDevelopmentFile($asset);
if ($developmentFile) {
$asset['path'] = str_replace('.min.', '.', $asset['path']);
}
}
// Ensure 'dependencies' is an array to satisfy Asset::createFromSettings expectations.
if (isset($asset['dependencies']) && !is_array($asset['dependencies'])) {
$asset['dependencies'] = [$asset['dependencies']];
}
Asset::createFromSettings($asset);
}
/**
* @param string $name
* @param string $content
* @param bool $footer
* @param string $type
*/
protected function loadByCorePageRenderInline(string $name, string $content, bool $footer = true, string $type = self::TYPE_JS)
{
$pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
if ($type === self::TYPE_JS) {
if ($footer) {
$pageRenderer->addJsFooterInlineCode($name, $content);
} else {
$pageRenderer->addJsInlineCode($name, $content);
}
} elseif ($content['type'] === 'css') {
$pageRenderer->addCssInlineBlock($name, $content);
}
}
/**
* @param array $asset
* @return void
*/
protected function loadByCorePageRender(array $asset)
{
$file = $this->resolveFileForApplicationContext($asset);
$fileNameAndPath = GeneralUtility::getFileAbsFileName($file);
$fileNameAndPath = PathUtility::stripPathSitePrefix($fileNameAndPath);
$pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
if ($asset['type'] === 'js') {
$pageRenderer->addJsFooterFile($fileNameAndPath);
} elseif ($asset['type'] === 'css') {
$pageRenderer->addCssFile($fileNameAndPath);
}
}
/**
* @param array $settings
* @return bool
*/
protected function shouldLoadByVhs(array $settings)
{
return ExtensionManagementUtility::isLoaded('vhs') && $settings['loadAssetWithVhsIfAvailable'];
}
/**
* @param array $asset
* @return string|NULL
*/
protected function getDevelopmentFile(array $asset): ?string
{
$possibleDevelopmentFile = str_replace('.min.', '.', $asset['path']);
$developmentFile = GeneralUtility::getFileAbsFileName($possibleDevelopmentFile);
if (!file_exists($developmentFile)) {
$developmentFile = NULL;
}
return $developmentFile;
}
/**
* @param array $asset
* @return string
*/
protected function resolveFileForApplicationContext(array $asset): string
{
$resolvedFile = $asset['path']; // default value
// check if there is a non minimized file for context Development
if (Environment::getContext()->isDevelopment()) {
$developmentFile = $this->getDevelopmentFile($asset);
if ($developmentFile) {
$resolvedFile = $developmentFile;
}
}
return $resolvedFile;
}
/**
* Returns an instance of the Frontend object.
*
* @return \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
*/
protected function getFrontendObject(): \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
{
return $GLOBALS['TSFE'];
}
/**
* @param string $templateIdentifier
* @return TemplateService
*/
protected function getTemplateService(string $templateIdentifier): TemplateService
{
return GeneralUtility::makeInstance(TemplateService::class, (int)$templateIdentifier);
}
}