|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
namespace TYPO3\CMS\Core\Page; |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the TYPO3 CMS project. |
|
7
|
|
|
* |
|
8
|
|
|
* It is free software; you can redistribute it and/or modify it under |
|
9
|
|
|
* the terms of the GNU General Public License, either version 2 |
|
10
|
|
|
* of the License, or any later version. |
|
11
|
|
|
* |
|
12
|
|
|
* For the full copyright and license information, please read the |
|
13
|
|
|
* LICENSE.txt file that was distributed with this source code. |
|
14
|
|
|
* |
|
15
|
|
|
* The TYPO3 project - inspiring people to share! |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
19
|
|
|
use TYPO3\CMS\Core\Utility\PathUtility; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class AssetRenderer |
|
23
|
|
|
* @internal The AssetRenderer is used for the asset rendering and is not public API |
|
24
|
|
|
*/ |
|
25
|
|
|
class AssetRenderer |
|
26
|
|
|
{ |
|
27
|
|
|
protected $assetCollector; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct(AssetCollector $assetCollector = null) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->assetCollector = $assetCollector ?? GeneralUtility::makeInstance(AssetCollector::class); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function renderInlineJavaScript($priority = false): string |
|
35
|
|
|
{ |
|
36
|
|
|
$template = '<script%attributes%>%source%</script>'; |
|
37
|
|
|
$assets = $this->assetCollector->getInlineJavaScripts(); |
|
38
|
|
|
foreach ($assets as &$assetData) { |
|
39
|
|
|
$assetData['attributes']['type'] = $assetData['attributes']['type'] ?? 'text/javascript'; |
|
40
|
|
|
} |
|
41
|
|
|
return $this->render($assets, $template, $priority); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function renderJavaScript($priority = false): string |
|
45
|
|
|
{ |
|
46
|
|
|
$template = '<script%attributes%></script>'; |
|
47
|
|
|
$assets = $this->assetCollector->getJavaScripts(); |
|
48
|
|
|
foreach ($assets as &$assetData) { |
|
49
|
|
|
$assetData['attributes']['src'] = $this->getAbsoluteWebPath($assetData['source']); |
|
50
|
|
|
$assetData['attributes']['type'] = $assetData['attributes']['type'] ?? 'text/javascript'; |
|
51
|
|
|
} |
|
52
|
|
|
return $this->render($assets, $template, $priority); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function renderInlineStyleSheets($priority = false): string |
|
56
|
|
|
{ |
|
57
|
|
|
$template = '<style%attributes%>%source%</style>'; |
|
58
|
|
|
$assets = $this->assetCollector->getInlineStyleSheets(); |
|
59
|
|
|
return $this->render($assets, $template, $priority); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function renderStyleSheets(bool $priority = false, string $endingSlash = ''): string |
|
63
|
|
|
{ |
|
64
|
|
|
$template = '<link%attributes% ' . $endingSlash . '>'; |
|
65
|
|
|
$assets = $this->assetCollector->getStyleSheets(); |
|
66
|
|
|
foreach ($assets as &$assetData) { |
|
67
|
|
|
$assetData['attributes']['href'] = $this->getAbsoluteWebPath($assetData['source']); |
|
68
|
|
|
$assetData['attributes']['rel'] = $assetData['attributes']['rel'] ?? 'stylesheet'; |
|
69
|
|
|
$assetData['attributes']['type'] = $assetData['attributes']['type'] ?? 'text/css'; |
|
70
|
|
|
} |
|
71
|
|
|
return $this->render($assets, $template, $priority); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
protected function render(array $assets, string $template, bool $priority = false): string |
|
75
|
|
|
{ |
|
76
|
|
|
$results = []; |
|
77
|
|
|
foreach ($assets as $assetData) { |
|
78
|
|
|
$attributes = $assetData['attributes']; |
|
79
|
|
|
$attributesString = count($attributes) ? ' ' . GeneralUtility::implodeAttributes($attributes, true) : ''; |
|
80
|
|
|
$code = str_replace(['%attributes%', '%source%'], [$attributesString, $assetData['source']], $template); |
|
81
|
|
|
$hasPriority = $assetData['options']['priority'] ?? false; |
|
82
|
|
|
if ($hasPriority === $priority) { |
|
83
|
|
|
$results[] = $code; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
return implode(LF, $results); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
private function getAbsoluteWebPath(string $file): string |
|
90
|
|
|
{ |
|
91
|
|
|
return PathUtility::getAbsoluteWebPath(GeneralUtility::getFileAbsFileName($file)); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|