‪TYPO3CMS  ‪main
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 
75  public function ‪addJavaScript(string ‪$identifier, string $source, array $attributes = [], array $options = []): self
76  {
77  $existingAttributes = $this->javaScripts[‪$identifier]['attributes'] ?? [];
78  ArrayUtility::mergeRecursiveWithOverrule($existingAttributes, $attributes);
79  $existingOptions = $this->javaScripts[‪$identifier]['options'] ?? [];
80  ArrayUtility::mergeRecursiveWithOverrule($existingOptions, $options);
81  $this->javaScripts[‪$identifier] = [
82  'source' => $source,
83  'attributes' => $existingAttributes,
84  'options' => $existingOptions,
85  ];
86  return $this;
87  }
88 
94  public function ‪addInlineJavaScript(string ‪$identifier, string $source, array $attributes = [], array $options = []): self
95  {
96  $existingAttributes = $this->inlineJavaScripts[‪$identifier]['attributes'] ?? [];
97  ArrayUtility::mergeRecursiveWithOverrule($existingAttributes, $attributes);
98  $existingOptions = $this->inlineJavaScripts[‪$identifier]['options'] ?? [];
99  ArrayUtility::mergeRecursiveWithOverrule($existingOptions, $options);
100  $this->inlineJavaScripts[‪$identifier] = [
101  'source' => $source,
102  'attributes' => $existingAttributes,
103  'options' => $existingOptions,
104  ];
105  return $this;
106  }
107 
113  public function ‪addStyleSheet(string ‪$identifier, string $source, array $attributes = [], array $options = []): self
114  {
115  $existingAttributes = $this->styleSheets[‪$identifier]['attributes'] ?? [];
116  ArrayUtility::mergeRecursiveWithOverrule($existingAttributes, $attributes);
117  $existingOptions = $this->styleSheets[‪$identifier]['options'] ?? [];
118  ArrayUtility::mergeRecursiveWithOverrule($existingOptions, $options);
119  $this->styleSheets[‪$identifier] = [
120  'source' => $source,
121  'attributes' => $existingAttributes,
122  'options' => $existingOptions,
123  ];
124  return $this;
125  }
126 
132  public function ‪addInlineStyleSheet(string ‪$identifier, string $source, array $attributes = [], array $options = []): self
133  {
134  $existingAttributes = $this->inlineStyleSheets[‪$identifier]['attributes'] ?? [];
135  ArrayUtility::mergeRecursiveWithOverrule($existingAttributes, $attributes);
136  $existingOptions = $this->inlineStyleSheets[‪$identifier]['options'] ?? [];
137  ArrayUtility::mergeRecursiveWithOverrule($existingOptions, $options);
138  $this->inlineStyleSheets[‪$identifier] = [
139  'source' => $source,
140  'attributes' => $existingAttributes,
141  'options' => $existingOptions,
142  ];
143  return $this;
144  }
145 
149  public function ‪addMedia(string $fileName, array $additionalInformation): self
150  {
151  $existingAdditionalInformation = $this->media[$fileName] ?? [];
152  ArrayUtility::mergeRecursiveWithOverrule($existingAdditionalInformation, $this->‪ensureAllValuesAreSerializable($additionalInformation));
153  $this->media[$fileName] = $existingAdditionalInformation;
154  return $this;
155  }
156 
157  private function ‪ensureAllValuesAreSerializable(array $additionalInformation): array
158  {
159  // Currently just filtering all non scalar values
160  return array_filter($additionalInformation, 'is_scalar');
161  }
162 
163  public function ‪removeJavaScript(string ‪$identifier): self
164  {
165  unset($this->javaScripts[‪$identifier]);
166  return $this;
167  }
168 
169  public function ‪removeInlineJavaScript(string ‪$identifier): self
170  {
171  unset($this->inlineJavaScripts[‪$identifier]);
172  return $this;
173  }
174 
175  public function ‪removeStyleSheet(string ‪$identifier): self
176  {
177  unset($this->styleSheets[‪$identifier]);
178  return $this;
179  }
180 
181  public function ‪removeInlineStyleSheet(string ‪$identifier): self
182  {
183  unset($this->inlineStyleSheets[‪$identifier]);
184  return $this;
185  }
186 
187  public function ‪removeMedia(string ‪$identifier): self
188  {
189  unset($this->media[‪$identifier]);
190  return $this;
191  }
192 
193  public function ‪getMedia(): array
194  {
195  return ‪$this->media;
196  }
197 
198  public function ‪getJavaScripts(?bool $priority = null): array
199  {
200  return $this->‪filterAssetsPriority($this->javaScripts, $priority);
201  }
202 
203  public function ‪getInlineJavaScripts(?bool $priority = null): array
204  {
205  return $this->‪filterAssetsPriority($this->inlineJavaScripts, $priority);
206  }
207 
208  public function ‪getStyleSheets(?bool $priority = null): array
209  {
210  return $this->‪filterAssetsPriority($this->styleSheets, $priority);
211  }
212 
213  public function ‪getInlineStyleSheets(?bool $priority = null): array
214  {
215  return $this->‪filterAssetsPriority($this->inlineStyleSheets, $priority);
216  }
217 
218  public function ‪hasJavaScript(string ‪$identifier): bool
219  {
220  return isset($this->javaScripts[‪$identifier]);
221  }
222 
223  public function ‪hasInlineJavaScript(string ‪$identifier): bool
224  {
225  return isset($this->inlineJavaScripts[‪$identifier]);
226  }
227 
228  public function ‪hasStyleSheet(string ‪$identifier): bool
229  {
230  return isset($this->styleSheets[‪$identifier]);
231  }
232 
233  public function ‪hasInlineStyleSheet(string ‪$identifier): bool
234  {
235  return isset($this->inlineStyleSheets[‪$identifier]);
236  }
237 
238  public function ‪hasMedia(string $fileName): bool
239  {
240  return isset($this->media[$fileName]);
241  }
242 
247  protected function ‪filterAssetsPriority(array &$assets, ?bool $priority): array
248  {
249  if ($priority === null) {
250  return $assets;
251  }
252  $currentPriorityAssets = [];
253  foreach ($assets as ‪$identifier => $asset) {
254  if ($priority === ($asset['options']['priority'] ?? false)) {
255  $currentPriorityAssets[‪$identifier] = $asset;
256  }
257  }
258  return $currentPriorityAssets;
259  }
260 
264  public function ‪updateState(array $newState): void
265  {
266  foreach ($newState as $var => $value) {
267  $this->{$var} = $value;
268  }
269  }
270 
274  public function ‪getState(): array
275  {
276  $state = [];
277  foreach (get_object_vars($this) as $var => $value) {
278  $state[$var] = $value;
279  }
280  return $state;
281  }
282 }
‪TYPO3\CMS\Core\Page\AssetCollector
Definition: AssetCollector.php:44
‪TYPO3\CMS\Core\Page\AssetCollector\getInlineJavaScripts
‪getInlineJavaScripts(?bool $priority=null)
Definition: AssetCollector.php:198
‪TYPO3\CMS\Core\Page\AssetCollector\getMedia
‪getMedia()
Definition: AssetCollector.php:188
‪TYPO3\CMS\Core\Page\AssetCollector\removeJavaScript
‪removeJavaScript(string $identifier)
Definition: AssetCollector.php:158
‪TYPO3\CMS\Core\Page\AssetCollector\removeStyleSheet
‪removeStyleSheet(string $identifier)
Definition: AssetCollector.php:170
‪TYPO3\CMS\Core\Page
Definition: AssetCollector.php:18
‪TYPO3\CMS\Core\Page\AssetCollector\removeMedia
‪removeMedia(string $identifier)
Definition: AssetCollector.php:182
‪TYPO3\CMS\Core\Page\AssetCollector\hasStyleSheet
‪hasStyleSheet(string $identifier)
Definition: AssetCollector.php:223
‪TYPO3\CMS\Core\Page\AssetCollector\$javaScripts
‪array $javaScripts
Definition: AssetCollector.php:47
‪TYPO3\CMS\Core\Page\AssetCollector\getState
‪getState()
Definition: AssetCollector.php:269
‪TYPO3\CMS\Core\Page\AssetCollector\ensureAllValuesAreSerializable
‪ensureAllValuesAreSerializable(array $additionalInformation)
Definition: AssetCollector.php:152
‪TYPO3\CMS\Core\Page\AssetCollector\removeInlineJavaScript
‪removeInlineJavaScript(string $identifier)
Definition: AssetCollector.php:164
‪TYPO3\CMS\Core\Page\AssetCollector\hasInlineJavaScript
‪hasInlineJavaScript(string $identifier)
Definition: AssetCollector.php:218
‪TYPO3\CMS\Core\Page\AssetCollector\getStyleSheets
‪getStyleSheets(?bool $priority=null)
Definition: AssetCollector.php:203
‪TYPO3\CMS\Core\Page\AssetCollector\addMedia
‪addMedia(string $fileName, array $additionalInformation)
Definition: AssetCollector.php:144
‪TYPO3\CMS\Core\Page\AssetCollector\addInlineJavaScript
‪addInlineJavaScript(string $identifier, string $source, array $attributes=[], array $options=[])
Definition: AssetCollector.php:89
‪TYPO3\CMS\Core\Page\AssetCollector\addStyleSheet
‪addStyleSheet(string $identifier, string $source, array $attributes=[], array $options=[])
Definition: AssetCollector.php:108
‪TYPO3\CMS\Core\Page\AssetCollector\$inlineStyleSheets
‪array $inlineStyleSheets
Definition: AssetCollector.php:59
‪TYPO3\CMS\Core\Page\AssetCollector\updateState
‪updateState(array $newState)
Definition: AssetCollector.php:259
‪TYPO3\CMS\Core\Page\AssetCollector\removeInlineStyleSheet
‪removeInlineStyleSheet(string $identifier)
Definition: AssetCollector.php:176
‪TYPO3\CMS\Core\Page\AssetCollector\hasInlineStyleSheet
‪hasInlineStyleSheet(string $identifier)
Definition: AssetCollector.php:228
‪TYPO3\CMS\Core\Page\AssetCollector\addInlineStyleSheet
‪addInlineStyleSheet(string $identifier, string $source, array $attributes=[], array $options=[])
Definition: AssetCollector.php:127
‪TYPO3\CMS\Core\Page\AssetCollector\hasJavaScript
‪hasJavaScript(string $identifier)
Definition: AssetCollector.php:213
‪TYPO3\CMS\Core\Page\AssetCollector\getInlineStyleSheets
‪getInlineStyleSheets(?bool $priority=null)
Definition: AssetCollector.php:208
‪TYPO3\CMS\Core\Page\AssetCollector\hasMedia
‪hasMedia(string $fileName)
Definition: AssetCollector.php:233
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:26
‪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:193
‪TYPO3\CMS\Core\Page\AssetCollector\$media
‪array $media
Definition: AssetCollector.php:63
‪TYPO3\CMS\Core\Page\AssetCollector\addJavaScript
‪addJavaScript(string $identifier, string $source, array $attributes=[], array $options=[])
Definition: AssetCollector.php:70
‪TYPO3\CMS\Core\Page\AssetCollector\$inlineJavaScripts
‪array $inlineJavaScripts
Definition: AssetCollector.php:51
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37
‪TYPO3\CMS\Core\Page\AssetCollector\filterAssetsPriority
‪filterAssetsPriority(array &$assets, ?bool $priority)
Definition: AssetCollector.php:242