‪TYPO3CMS  ‪main
JavaScriptRenderer.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 
23 
24 class JavaScriptRenderer
25 {
26  protected string $handlerUri;
27  protected JavaScriptItems $items;
28  protected ImportMap $importMap;
29  protected int $javaScriptModuleInstructionFlags = 0;
30 
31  public static function create(?string $uri = null): self
32  {
34  GeneralUtility::getFileAbsFileName('EXT:core/Resources/Public/JavaScript/java-script-item-handler.js')
35  );
36  return GeneralUtility::makeInstance(static::class, $uri);
37  }
38 
39  public function __construct(string $handlerUri)
40  {
41  $this->handlerUri = $handlerUri;
42  $this->items = GeneralUtility::makeInstance(JavaScriptItems::class);
43  $this->importMap = GeneralUtility::makeInstance(ImportMapFactory::class)->create();
44  }
45 
46  public function addGlobalAssignment(array $payload): void
47  {
48  $this->items->addGlobalAssignment($payload);
49  }
50 
51  public function addJavaScriptModuleInstruction(JavaScriptModuleInstruction $instruction): void
52  {
53  if ($instruction->shallLoadImportMap()) {
54  $this->importMap->includeImportsFor($instruction->getName());
55  }
56  $this->javaScriptModuleInstructionFlags |= $instruction->getFlags();
57  $this->items->addJavaScriptModuleInstruction($instruction);
58  }
59 
60  public function hasImportMap(): bool
61  {
63  }
64 
68  public function includeAllImports(): void
69  {
70  $this->importMap->includeAllImports();
71  }
72 
73  public function includeTaggedImports(string $tag): void
74  {
75  $this->importMap->includeTaggedImports($tag);
76  }
77 
82  public function toArray(): array
83  {
84  if ($this->isEmpty()) {
85  return [];
86  }
87  return $this->items->toArray();
88  }
89 
90  public function render(null|string|ConsumableNonce $nonce = null): string
91  {
92  if ($this->isEmpty()) {
93  return '';
94  }
95  $attributes = [
96  'src' => $this->handlerUri,
97  'async' => 'async',
98  ];
99  if ($nonce !== null) {
100  $attributes['nonce'] = (string)$nonce;
101  }
102  return $this->createScriptElement(
103  $attributes,
104  $this->jsonEncode($this->toArray())
105  );
106  }
107 
108  public function renderImportMap(string $sitePath, null|string|ConsumableNonce $nonce = null): string
109  {
110  if (!$this->isEmpty()) {
111  $this->importMap->includeImportsFor('@typo3/core/java-script-item-handler.js');
112  }
113  return $this->importMap->render($sitePath, $nonce);
114  }
115 
116  protected function isEmpty(): bool
117  {
118  return $this->items->isEmpty();
119  }
120 
121  protected function createScriptElement(array $attributes, string $textContent = ''): string
122  {
123  if (empty($attributes)) {
124  return '';
125  }
126  $attributesPart = GeneralUtility::implodeAttributes($attributes, true);
127  // actual JSON payload is stored as comment in `script.textContent`
128  return sprintf('<script %s>/* %s */</script>', $attributesPart, $textContent);
129  }
130 
131  protected function jsonEncode($value): string
132  {
133  return (string)json_encode($value, JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_TAG);
134  }
135 
139  public function updateState(array $state): void
140  {
141  foreach ($state as $var => $value) {
142  switch ($var) {
143  case 'items':
144  $this->items->updateState($value);
145  break;
146  case 'importMap':
147  $this->importMap->updateState($value);
148  break;
149  default:
150  $this->{$var} = $value;
151  break;
152  }
153  }
154  }
155 
159  public function getState(): array
160  {
161  $state = [];
162  foreach (get_object_vars($this) as $var => $value) {
163  switch ($var) {
164  case 'items':
165  $state[$var] = $this->items->getState();
166  break;
167  case 'importMap':
168  $state[$var] = $this->importMap->getState();
169  break;
170  default:
171  $state[$var] = $value;
172  break;
173  }
174  }
175  return $state;
176  }
177 }
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:27
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\ConsumableNonce
Definition: ConsumableNonce.php:24
‪TYPO3\CMS\Core\Page
Definition: AssetCollector.php:18
‪TYPO3\CMS\Core\Utility\PathUtility\getAbsoluteWebPath
‪static string getAbsoluteWebPath(string $targetPath, bool $prefixWithSitePath=true)
Definition: PathUtility.php:52
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\FLAG_LOAD_IMPORTMAP
‪const FLAG_LOAD_IMPORTMAP
Definition: JavaScriptModuleInstruction.php:27
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52