‪TYPO3CMS  ‪main
AssetRenderer.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
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 namespace ‪TYPO3\CMS\Core\Page;
19 
20 use Psr\EventDispatcher\EventDispatcherInterface;
26 
31 {
35  protected ‪$assetCollector;
36 
41 
42  public function ‪__construct(‪AssetCollector ‪$assetCollector = null, EventDispatcherInterface ‪$eventDispatcher = null)
43  {
44  $this->assetCollector = ‪$assetCollector ?? GeneralUtility::makeInstance(AssetCollector::class);
45  $this->eventDispatcher = ‪$eventDispatcher ?? GeneralUtility::makeInstance(EventDispatcherInterface::class);
46  }
47 
48  public function ‪renderInlineJavaScript($priority = false, ?‪ConsumableNonce $nonce = null): string
49  {
50  $this->eventDispatcher->dispatch(
51  new ‪BeforeJavaScriptsRenderingEvent($this->assetCollector, true, $priority)
52  );
53 
54  $template = '<script%attributes%>%source%</script>';
55  $assets = $this->assetCollector->getInlineJavaScripts($priority);
56  return $this->‪render($assets, $template, $nonce);
57  }
58 
59  public function ‪renderJavaScript($priority = false, ?‪ConsumableNonce $nonce = null): string
60  {
61  $this->eventDispatcher->dispatch(
62  new ‪BeforeJavaScriptsRenderingEvent($this->assetCollector, false, $priority)
63  );
64 
65  $template = '<script%attributes%></script>';
66  $assets = $this->assetCollector->getJavaScripts($priority);
67  foreach ($assets as &$assetData) {
68  $assetData['attributes']['src'] = $this->‪getAbsoluteWebPath($assetData['source']);
69  }
70  return $this->‪render($assets, $template, $nonce);
71  }
72 
73  public function ‪renderInlineStyleSheets($priority = false, ?‪ConsumableNonce $nonce = null): string
74  {
75  $this->eventDispatcher->dispatch(
76  new ‪BeforeStylesheetsRenderingEvent($this->assetCollector, true, $priority)
77  );
78 
79  $template = '<style%attributes%>%source%</style>';
80  $assets = $this->assetCollector->getInlineStyleSheets($priority);
81  return $this->‪render($assets, $template, $nonce);
82  }
83 
84  public function ‪renderStyleSheets(bool $priority = false, string $endingSlash = '', ?‪ConsumableNonce $nonce = null): string
85  {
86  $this->eventDispatcher->dispatch(
87  new ‪BeforeStylesheetsRenderingEvent($this->assetCollector, false, $priority)
88  );
89 
90  $template = '<link%attributes% ' . $endingSlash . '>';
91  $assets = $this->assetCollector->getStyleSheets($priority);
92  foreach ($assets as &$assetData) {
93  $assetData['attributes']['href'] = $this->‪getAbsoluteWebPath($assetData['source']);
94  $assetData['attributes']['rel'] = $assetData['attributes']['rel'] ?? 'stylesheet';
95  }
96  return $this->‪render($assets, $template, $nonce);
97  }
98 
99  protected function ‪render(array $assets, string $template, ?‪ConsumableNonce $nonce = null): string
100  {
101  $results = [];
102  foreach ($assets as $assetData) {
103  $attributes = $assetData['attributes'];
104  if ($nonce !== null && !empty($assetData['options']['useNonce'])) {
105  $attributes['nonce'] = $nonce->consume();
106  }
107  $attributesString = count($attributes) ? ' ' . GeneralUtility::implodeAttributes($attributes, true) : '';
108  $results[] = str_replace(
109  ['%attributes%', '%source%'],
110  [$attributesString, $assetData['source']],
111  $template
112  );
113  }
114  return implode(LF, $results);
115  }
116 
117  private function ‪getAbsoluteWebPath(string $file): string
118  {
120  return $file;
121  }
122  $file = ‪PathUtility::getAbsoluteWebPath(GeneralUtility::getFileAbsFileName($file));
123  return GeneralUtility::createVersionNumberedFilename($file);
124  }
125 }
‪TYPO3\CMS\Core\Page\AssetCollector
Definition: AssetCollector.php:42
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:27
‪TYPO3\CMS\Core\Page\AssetRenderer\renderJavaScript
‪renderJavaScript($priority=false, ?ConsumableNonce $nonce=null)
Definition: AssetRenderer.php:57
‪TYPO3\CMS\Core\Page\AssetRenderer\renderStyleSheets
‪renderStyleSheets(bool $priority=false, string $endingSlash='', ?ConsumableNonce $nonce=null)
Definition: AssetRenderer.php:82
‪TYPO3\CMS\Core\Page\AssetRenderer\__construct
‪__construct(AssetCollector $assetCollector=null, EventDispatcherInterface $eventDispatcher=null)
Definition: AssetRenderer.php:40
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\ConsumableNonce
Definition: ConsumableNonce.php:24
‪TYPO3\CMS\Core\Page
Definition: AssetCollector.php:18
‪TYPO3\CMS\Core\Page\AssetRenderer\renderInlineJavaScript
‪renderInlineJavaScript($priority=false, ?ConsumableNonce $nonce=null)
Definition: AssetRenderer.php:46
‪TYPO3\CMS\Core\Page\AssetRenderer\getAbsoluteWebPath
‪getAbsoluteWebPath(string $file)
Definition: AssetRenderer.php:115
‪TYPO3\CMS\Core\Utility\PathUtility\getAbsoluteWebPath
‪static string getAbsoluteWebPath(string $targetPath, bool $prefixWithSitePath=true)
Definition: PathUtility.php:52
‪TYPO3\CMS\Core\Page\AssetRenderer\render
‪render(array $assets, string $template, ?ConsumableNonce $nonce=null)
Definition: AssetRenderer.php:97
‪TYPO3\CMS\Core\Page\AssetRenderer\$eventDispatcher
‪EventDispatcherInterface $eventDispatcher
Definition: AssetRenderer.php:38
‪TYPO3\CMS\Core\Page\AssetRenderer\renderInlineStyleSheets
‪renderInlineStyleSheets($priority=false, ?ConsumableNonce $nonce=null)
Definition: AssetRenderer.php:71
‪TYPO3\CMS\Core\Page\AssetRenderer\$assetCollector
‪AssetCollector $assetCollector
Definition: AssetRenderer.php:34
‪TYPO3\CMS\Core\Page\Event\BeforeJavaScriptsRenderingEvent
Definition: BeforeJavaScriptsRenderingEvent.php:26
‪TYPO3\CMS\Core\Page\AssetRenderer
Definition: AssetRenderer.php:31
‪TYPO3\CMS\Core\Page\Event\BeforeStylesheetsRenderingEvent
Definition: BeforeStylesheetsRenderingEvent.php:26
‪TYPO3\CMS\Core\Utility\PathUtility\hasProtocolAndScheme
‪static hasProtocolAndScheme(string $path)
Definition: PathUtility.php:445
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52