‪TYPO3CMS  10.4
AssetCollector.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 
19 
22 
44 {
48  protected ‪$javaScripts = [];
49 
53  protected ‪$inlineJavaScripts = [];
54 
58  protected ‪$styleSheets = [];
59 
63  protected ‪$inlineStyleSheets = [];
64 
68  protected ‪$media = [];
69 
77  public function ‪addJavaScript(string $identifier, string $source, array $attributes = [], array $options = []): self
78  {
79  $existingAttributes = $this->javaScripts[$identifier]['attributes'] ?? [];
80  ‪ArrayUtility::mergeRecursiveWithOverrule($existingAttributes, $attributes);
81  $existingOptions = $this->javaScripts[$identifier]['options'] ?? [];
82  ‪ArrayUtility::mergeRecursiveWithOverrule($existingOptions, $options);
83  $this->javaScripts[$identifier] = [
84  'source' => $source,
85  'attributes' => $existingAttributes,
86  'options' => $existingOptions
87  ];
88  return $this;
89  }
90 
98  public function ‪addInlineJavaScript(string $identifier, string $source, array $attributes = [], array $options = []): self
99  {
100  $existingAttributes = $this->inlineJavaScripts[$identifier]['attributes'] ?? [];
101  ‪ArrayUtility::mergeRecursiveWithOverrule($existingAttributes, $attributes);
102  $existingOptions = $this->inlineJavaScripts[$identifier]['options'] ?? [];
103  ‪ArrayUtility::mergeRecursiveWithOverrule($existingOptions, $options);
104  $this->inlineJavaScripts[$identifier] = [
105  'source' => $source,
106  'attributes' => $existingAttributes,
107  'options' => $existingOptions
108  ];
109  return $this;
110  }
111 
119  public function ‪addStyleSheet(string $identifier, string $source, array $attributes = [], array $options = []): self
120  {
121  $existingAttributes = $this->styleSheets[$identifier]['attributes'] ?? [];
122  ‪ArrayUtility::mergeRecursiveWithOverrule($existingAttributes, $attributes);
123  $existingOptions = $this->styleSheets[$identifier]['options'] ?? [];
124  ‪ArrayUtility::mergeRecursiveWithOverrule($existingOptions, $options);
125  $this->styleSheets[$identifier] = [
126  'source' => $source,
127  'attributes' => $existingAttributes,
128  'options' => $existingOptions
129  ];
130  return $this;
131  }
132 
140  public function ‪addInlineStyleSheet(string $identifier, string $source, array $attributes = [], array $options = []): self
141  {
142  $existingAttributes = $this->inlineStyleSheets[$identifier]['attributes'] ?? [];
143  ‪ArrayUtility::mergeRecursiveWithOverrule($existingAttributes, $attributes);
144  $existingOptions = $this->inlineStyleSheets[$identifier]['options'] ?? [];
145  ‪ArrayUtility::mergeRecursiveWithOverrule($existingOptions, $options);
146  $this->inlineStyleSheets[$identifier] = [
147  'source' => $source,
148  'attributes' => $existingAttributes,
149  'options' => $existingOptions
150  ];
151  return $this;
152  }
153 
159  public function ‪addMedia(string $fileName, array $additionalInformation): self
160  {
161  $existingAdditionalInformation = $this->media[$fileName] ?? [];
162  ‪ArrayUtility::mergeRecursiveWithOverrule($existingAdditionalInformation, $this->‪ensureAllValuesAreSerializable($additionalInformation));
163  $this->media[$fileName] = $existingAdditionalInformation;
164  return $this;
165  }
166 
167  private function ‪ensureAllValuesAreSerializable(array $additionalInformation): array
168  {
169  // Currently just filtering all non scalar values
170  return array_filter($additionalInformation, 'is_scalar');
171  }
172 
173  public function ‪removeJavaScript(string $identifier): self
174  {
175  unset($this->javaScripts[$identifier]);
176  return $this;
177  }
178 
179  public function ‪removeInlineJavaScript(string $identifier): self
180  {
181  unset($this->inlineJavaScripts[$identifier]);
182  return $this;
183  }
184 
185  public function ‪removeStyleSheet(string $identifier): self
186  {
187  unset($this->styleSheets[$identifier]);
188  return $this;
189  }
190 
191  public function ‪removeInlineStyleSheet(string $identifier): self
192  {
193  unset($this->inlineStyleSheets[$identifier]);
194  return $this;
195  }
196 
197  public function ‪removeMedia(string $identifier): self
198  {
199  unset($this->media[$identifier]);
200  return $this;
201  }
202 
203  public function ‪getMedia(): array
204  {
205  return ‪$this->media;
206  }
207 
208  public function ‪getJavaScripts(?bool $priority = null): array
209  {
210  return $this->‪filterAssetsPriority($this->javaScripts, $priority);
211  }
212 
213  public function ‪getInlineJavaScripts(?bool $priority = null): array
214  {
215  return $this->‪filterAssetsPriority($this->inlineJavaScripts, $priority);
216  }
217 
218  public function ‪getStyleSheets(?bool $priority = null): array
219  {
220  return $this->‪filterAssetsPriority($this->styleSheets, $priority);
221  }
222 
223  public function ‪getInlineStyleSheets(?bool $priority = null): array
224  {
225  return $this->‪filterAssetsPriority($this->inlineStyleSheets, $priority);
226  }
227 
233  protected function ‪filterAssetsPriority(array &$assets, ?bool $priority): array
234  {
235  if ($priority === null) {
236  return $assets;
237  }
238  $currentPriorityAssets = [];
239  foreach ($assets as $identifier => $asset) {
240  if ($priority === ($asset['options']['priority'] ?? false)) {
241  $currentPriorityAssets[$identifier] = $asset;
242  }
243  }
244  return $currentPriorityAssets;
245  }
246 
251  public function ‪updateState(array $newState): void
252  {
253  foreach ($newState as $var => $value) {
254  $this->{$var} = $value;
255  }
256  }
257 
262  public function ‪getState(): array
263  {
264  $state = [];
265  foreach (get_object_vars($this) as $var => $value) {
266  $state[$var] = $value;
267  }
268  return $state;
269  }
270 }
‪TYPO3\CMS\Core\Page\AssetCollector\filterAssetsPriority
‪array filterAssetsPriority(array &$assets, ?bool $priority)
Definition: AssetCollector.php:228
‪TYPO3\CMS\Core\Page\AssetCollector
Definition: AssetCollector.php:44
‪TYPO3\CMS\Core\Page\AssetCollector\getInlineJavaScripts
‪getInlineJavaScripts(?bool $priority=null)
Definition: AssetCollector.php:208
‪TYPO3\CMS\Core\Page\AssetCollector\getMedia
‪getMedia()
Definition: AssetCollector.php:198
‪TYPO3\CMS\Core\Page\AssetCollector\removeJavaScript
‪removeJavaScript(string $identifier)
Definition: AssetCollector.php:168
‪TYPO3\CMS\Core\Page\AssetCollector\addMedia
‪AssetCollector addMedia(string $fileName, array $additionalInformation)
Definition: AssetCollector.php:154
‪TYPO3\CMS\Core\Page\AssetCollector\addJavaScript
‪AssetCollector addJavaScript(string $identifier, string $source, array $attributes=[], array $options=[])
Definition: AssetCollector.php:72
‪TYPO3\CMS\Core\Page\AssetCollector\removeStyleSheet
‪removeStyleSheet(string $identifier)
Definition: AssetCollector.php:180
‪TYPO3\CMS\Core\Page
Definition: AssetCollector.php:18
‪TYPO3\CMS\Core\Utility\ArrayUtility\mergeRecursiveWithOverrule
‪static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)
Definition: ArrayUtility.php:654
‪TYPO3\CMS\Core\Page\AssetCollector\addInlineStyleSheet
‪AssetCollector addInlineStyleSheet(string $identifier, string $source, array $attributes=[], array $options=[])
Definition: AssetCollector.php:135
‪TYPO3\CMS\Core\Page\AssetCollector\removeMedia
‪removeMedia(string $identifier)
Definition: AssetCollector.php:192
‪TYPO3\CMS\Core\Page\AssetCollector\$javaScripts
‪array $javaScripts
Definition: AssetCollector.php:47
‪TYPO3\CMS\Core\Page\AssetCollector\getState
‪array getState()
Definition: AssetCollector.php:257
‪TYPO3\CMS\Core\Page\AssetCollector\ensureAllValuesAreSerializable
‪ensureAllValuesAreSerializable(array $additionalInformation)
Definition: AssetCollector.php:162
‪TYPO3\CMS\Core\Page\AssetCollector\removeInlineJavaScript
‪removeInlineJavaScript(string $identifier)
Definition: AssetCollector.php:174
‪TYPO3\CMS\Core\Page\AssetCollector\getStyleSheets
‪getStyleSheets(?bool $priority=null)
Definition: AssetCollector.php:213
‪TYPO3\CMS\Core\Page\AssetCollector\$inlineStyleSheets
‪array $inlineStyleSheets
Definition: AssetCollector.php:59
‪TYPO3\CMS\Core\Page\AssetCollector\updateState
‪updateState(array $newState)
Definition: AssetCollector.php:246
‪TYPO3\CMS\Core\Page\AssetCollector\removeInlineStyleSheet
‪removeInlineStyleSheet(string $identifier)
Definition: AssetCollector.php:186
‪TYPO3\CMS\Core\Page\AssetCollector\getInlineStyleSheets
‪getInlineStyleSheets(?bool $priority=null)
Definition: AssetCollector.php:218
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:24
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:23
‪TYPO3\CMS\Core\Page\AssetCollector\$styleSheets
‪array $styleSheets
Definition: AssetCollector.php:55
‪TYPO3\CMS\Core\Page\AssetCollector\getJavaScripts
‪getJavaScripts(?bool $priority=null)
Definition: AssetCollector.php:203
‪TYPO3\CMS\Core\Page\AssetCollector\addInlineJavaScript
‪AssetCollector addInlineJavaScript(string $identifier, string $source, array $attributes=[], array $options=[])
Definition: AssetCollector.php:93
‪TYPO3\CMS\Core\Page\AssetCollector\$media
‪array $media
Definition: AssetCollector.php:63
‪TYPO3\CMS\Core\Page\AssetCollector\$inlineJavaScripts
‪array $inlineJavaScripts
Definition: AssetCollector.php:51
‪TYPO3\CMS\Core\Page\AssetCollector\addStyleSheet
‪AssetCollector addStyleSheet(string $identifier, string $source, array $attributes=[], array $options=[])
Definition: AssetCollector.php:114