‪TYPO3CMS  10.4
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(EventDispatcher::class);
46  }
47 
48  public function ‪renderInlineJavaScript($priority = false): 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);
57  }
58 
59  public function ‪renderJavaScript($priority = false): 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);
71  }
72 
73  public function ‪renderInlineStyleSheets($priority = false): 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);
82  }
83 
84  public function ‪renderStyleSheets(bool $priority = false, string $endingSlash = ''): 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  $assetData['attributes']['type'] = $assetData['attributes']['type'] ?? 'text/css';
96  }
97  return $this->‪render($assets, $template);
98  }
99 
100  protected function ‪render(array $assets, string $template): string
101  {
102  $results = [];
103  foreach ($assets as $assetData) {
104  $attributes = $assetData['attributes'];
105  $attributesString = count($attributes) ? ' ' . GeneralUtility::implodeAttributes($attributes, true) : '';
106  $results[] = str_replace(
107  ['%attributes%', '%source%'],
108  [$attributesString, $assetData['source']],
109  $template
110  );
111  }
112  return implode(LF, $results);
113  }
114 
115  private function ‪getAbsoluteWebPath(string $file): string
116  {
117  if (strpos($file, '://') !== false || strpos($file, '//') === 0) {
118  return $file;
119  }
120  $file = ‪PathUtility::getAbsoluteWebPath(GeneralUtility::getFileAbsFileName($file));
121  return GeneralUtility::createVersionNumberedFilename($file);
122  }
123 }
‪TYPO3\CMS\Core\Page\AssetRenderer\renderInlineStyleSheets
‪renderInlineStyleSheets($priority=false)
Definition: AssetRenderer.php:71
‪TYPO3\CMS\Core\Page\AssetCollector
Definition: AssetCollector.php:44
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:24
‪TYPO3\CMS\Core\Page\AssetRenderer\__construct
‪__construct(AssetCollector $assetCollector=null, EventDispatcherInterface $eventDispatcher=null)
Definition: AssetRenderer.php:40
‪TYPO3\CMS\Core\Page
Definition: AssetCollector.php:18
‪TYPO3\CMS\Core\Page\AssetRenderer\render
‪render(array $assets, string $template)
Definition: AssetRenderer.php:98
‪TYPO3\CMS\Core\Page\AssetRenderer\getAbsoluteWebPath
‪getAbsoluteWebPath(string $file)
Definition: AssetRenderer.php:113
‪TYPO3\CMS\Core\EventDispatcher\EventDispatcher
Definition: EventDispatcher.php:30
‪TYPO3\CMS\Core\Page\AssetRenderer\renderJavaScript
‪renderJavaScript($priority=false)
Definition: AssetRenderer.php:57
‪TYPO3\CMS\Core\Page\AssetRenderer\$eventDispatcher
‪EventDispatcherInterface $eventDispatcher
Definition: AssetRenderer.php:38
‪TYPO3\CMS\Core\Page\AssetRenderer\renderInlineJavaScript
‪renderInlineJavaScript($priority=false)
Definition: AssetRenderer.php:46
‪TYPO3\CMS\Core\Page\AssetRenderer\renderStyleSheets
‪renderStyleSheets(bool $priority=false, string $endingSlash='')
Definition: AssetRenderer.php:82
‪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\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Utility\PathUtility\getAbsoluteWebPath
‪static string getAbsoluteWebPath($targetPath)
Definition: PathUtility.php:43