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