‪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 
42 {
43  protected array ‪$javaScripts = [];
44  protected array ‪$inlineJavaScripts = [];
45  protected array ‪$styleSheets = [];
46  protected array ‪$inlineStyleSheets = [];
47  protected array ‪$media = [];
48 
54  public function ‪addJavaScript(string ‪$identifier, string $source, array $attributes = [], array $options = []): self
55  {
56  $existingAttributes = $this->javaScripts[‪$identifier]['attributes'] ?? [];
57  ArrayUtility::mergeRecursiveWithOverrule($existingAttributes, $attributes);
58  $existingOptions = $this->javaScripts[‪$identifier]['options'] ?? [];
59  ArrayUtility::mergeRecursiveWithOverrule($existingOptions, $options);
60  $this->javaScripts[‪$identifier] = [
61  'source' => $source,
62  'attributes' => $existingAttributes,
63  'options' => $existingOptions,
64  ];
65  return $this;
66  }
67 
73  public function ‪addInlineJavaScript(string ‪$identifier, string $source, array $attributes = [], array $options = []): self
74  {
75  $existingAttributes = $this->inlineJavaScripts[‪$identifier]['attributes'] ?? [];
76  ArrayUtility::mergeRecursiveWithOverrule($existingAttributes, $attributes);
77  $existingOptions = $this->inlineJavaScripts[‪$identifier]['options'] ?? [];
78  ArrayUtility::mergeRecursiveWithOverrule($existingOptions, $options);
79  $this->inlineJavaScripts[‪$identifier] = [
80  'source' => $source,
81  'attributes' => $existingAttributes,
82  'options' => $existingOptions,
83  ];
84  return $this;
85  }
86 
92  public function ‪addStyleSheet(string ‪$identifier, string $source, array $attributes = [], array $options = []): self
93  {
94  $existingAttributes = $this->styleSheets[‪$identifier]['attributes'] ?? [];
95  ArrayUtility::mergeRecursiveWithOverrule($existingAttributes, $attributes);
96  $existingOptions = $this->styleSheets[‪$identifier]['options'] ?? [];
97  ArrayUtility::mergeRecursiveWithOverrule($existingOptions, $options);
98  $this->styleSheets[‪$identifier] = [
99  'source' => $source,
100  'attributes' => $existingAttributes,
101  'options' => $existingOptions,
102  ];
103  return $this;
104  }
105 
111  public function ‪addInlineStyleSheet(string ‪$identifier, string $source, array $attributes = [], array $options = []): self
112  {
113  $existingAttributes = $this->inlineStyleSheets[‪$identifier]['attributes'] ?? [];
114  ArrayUtility::mergeRecursiveWithOverrule($existingAttributes, $attributes);
115  $existingOptions = $this->inlineStyleSheets[‪$identifier]['options'] ?? [];
116  ArrayUtility::mergeRecursiveWithOverrule($existingOptions, $options);
117  $this->inlineStyleSheets[‪$identifier] = [
118  'source' => $source,
119  'attributes' => $existingAttributes,
120  'options' => $existingOptions,
121  ];
122  return $this;
123  }
124 
128  public function ‪addMedia(string $fileName, array $additionalInformation): self
129  {
130  $existingAdditionalInformation = $this->media[$fileName] ?? [];
131  ArrayUtility::mergeRecursiveWithOverrule($existingAdditionalInformation, $this->‪ensureAllValuesAreSerializable($additionalInformation));
132  $this->media[$fileName] = $existingAdditionalInformation;
133  return $this;
134  }
135 
136  private function ‪ensureAllValuesAreSerializable(array $additionalInformation): array
137  {
138  // Currently just filtering all non-scalar values
139  return array_filter($additionalInformation, is_scalar(...));
140  }
141 
142  public function ‪removeJavaScript(string ‪$identifier): self
143  {
144  unset($this->javaScripts[‪$identifier]);
145  return $this;
146  }
147 
148  public function ‪removeInlineJavaScript(string ‪$identifier): self
149  {
150  unset($this->inlineJavaScripts[‪$identifier]);
151  return $this;
152  }
153 
154  public function ‪removeStyleSheet(string ‪$identifier): self
155  {
156  unset($this->styleSheets[‪$identifier]);
157  return $this;
158  }
159 
160  public function ‪removeInlineStyleSheet(string ‪$identifier): self
161  {
162  unset($this->inlineStyleSheets[‪$identifier]);
163  return $this;
164  }
165 
166  public function ‪removeMedia(string ‪$identifier): self
167  {
168  unset($this->media[‪$identifier]);
169  return $this;
170  }
171 
172  public function ‪getMedia(): array
173  {
174  return ‪$this->media;
175  }
176 
177  public function ‪getJavaScripts(?bool $priority = null): array
178  {
179  return $this->‪filterAssetsPriority($this->javaScripts, $priority);
180  }
181 
182  public function ‪getInlineJavaScripts(?bool $priority = null): array
183  {
184  return $this->‪filterAssetsPriority($this->inlineJavaScripts, $priority);
185  }
186 
187  public function ‪getStyleSheets(?bool $priority = null): array
188  {
189  return $this->‪filterAssetsPriority($this->styleSheets, $priority);
190  }
191 
192  public function ‪getInlineStyleSheets(?bool $priority = null): array
193  {
194  return $this->‪filterAssetsPriority($this->inlineStyleSheets, $priority);
195  }
196 
197  public function ‪hasJavaScript(string ‪$identifier): bool
198  {
199  return isset($this->javaScripts[‪$identifier]);
200  }
201 
202  public function ‪hasInlineJavaScript(string ‪$identifier): bool
203  {
204  return isset($this->inlineJavaScripts[‪$identifier]);
205  }
206 
207  public function ‪hasStyleSheet(string ‪$identifier): bool
208  {
209  return isset($this->styleSheets[‪$identifier]);
210  }
211 
212  public function ‪hasInlineStyleSheet(string ‪$identifier): bool
213  {
214  return isset($this->inlineStyleSheets[‪$identifier]);
215  }
216 
217  public function ‪hasMedia(string $fileName): bool
218  {
219  return isset($this->media[$fileName]);
220  }
221 
226  protected function ‪filterAssetsPriority(array $assets, ?bool $priority): array
227  {
228  if ($priority === null) {
229  return $assets;
230  }
231  $currentPriorityAssets = [];
232  foreach ($assets as ‪$identifier => $asset) {
233  if ($priority === ($asset['options']['priority'] ?? false)) {
234  $currentPriorityAssets[‪$identifier] = $asset;
235  }
236  }
237  return $currentPriorityAssets;
238  }
239 
243  public function ‪updateState(array $newState): void
244  {
245  foreach ($newState as $var => $value) {
246  $this->{$var} = $value;
247  }
248  }
249 
253  public function ‪getState(): array
254  {
255  $state = [];
256  foreach (get_object_vars($this) as $var => $value) {
257  $state[$var] = $value;
258  }
259  return $state;
260  }
261 }
‪TYPO3\CMS\Core\Page\AssetCollector\filterAssetsPriority
‪filterAssetsPriority(array $assets, ?bool $priority)
Definition: AssetCollector.php:226
‪TYPO3\CMS\Core\Page\AssetCollector
Definition: AssetCollector.php:42
‪TYPO3\CMS\Core\Page\AssetCollector\getInlineJavaScripts
‪getInlineJavaScripts(?bool $priority=null)
Definition: AssetCollector.php:182
‪TYPO3\CMS\Core\Page\AssetCollector\getMedia
‪getMedia()
Definition: AssetCollector.php:172
‪TYPO3\CMS\Core\Page\AssetCollector\removeJavaScript
‪removeJavaScript(string $identifier)
Definition: AssetCollector.php:142
‪TYPO3\CMS\Core\Page\AssetCollector\removeStyleSheet
‪removeStyleSheet(string $identifier)
Definition: AssetCollector.php:154
‪TYPO3\CMS\Core\Page
Definition: AssetCollector.php:18
‪TYPO3\CMS\Core\Page\AssetCollector\removeMedia
‪removeMedia(string $identifier)
Definition: AssetCollector.php:166
‪TYPO3\CMS\Core\Page\AssetCollector\hasStyleSheet
‪hasStyleSheet(string $identifier)
Definition: AssetCollector.php:207
‪TYPO3\CMS\Core\Page\AssetCollector\$javaScripts
‪array $javaScripts
Definition: AssetCollector.php:43
‪TYPO3\CMS\Core\Page\AssetCollector\getState
‪getState()
Definition: AssetCollector.php:253
‪TYPO3\CMS\Core\Page\AssetCollector\ensureAllValuesAreSerializable
‪ensureAllValuesAreSerializable(array $additionalInformation)
Definition: AssetCollector.php:136
‪TYPO3\CMS\Core\Page\AssetCollector\removeInlineJavaScript
‪removeInlineJavaScript(string $identifier)
Definition: AssetCollector.php:148
‪TYPO3\CMS\Core\Page\AssetCollector\hasInlineJavaScript
‪hasInlineJavaScript(string $identifier)
Definition: AssetCollector.php:202
‪TYPO3\CMS\Core\Page\AssetCollector\getStyleSheets
‪getStyleSheets(?bool $priority=null)
Definition: AssetCollector.php:187
‪TYPO3\CMS\Core\Page\AssetCollector\addMedia
‪addMedia(string $fileName, array $additionalInformation)
Definition: AssetCollector.php:128
‪TYPO3\CMS\Core\Page\AssetCollector\addInlineJavaScript
‪addInlineJavaScript(string $identifier, string $source, array $attributes=[], array $options=[])
Definition: AssetCollector.php:73
‪TYPO3\CMS\Core\Page\AssetCollector\addStyleSheet
‪addStyleSheet(string $identifier, string $source, array $attributes=[], array $options=[])
Definition: AssetCollector.php:92
‪TYPO3\CMS\Core\Page\AssetCollector\$inlineStyleSheets
‪array $inlineStyleSheets
Definition: AssetCollector.php:46
‪TYPO3\CMS\Core\Page\AssetCollector\updateState
‪updateState(array $newState)
Definition: AssetCollector.php:243
‪TYPO3\CMS\Core\Page\AssetCollector\removeInlineStyleSheet
‪removeInlineStyleSheet(string $identifier)
Definition: AssetCollector.php:160
‪TYPO3\CMS\Core\Page\AssetCollector\hasInlineStyleSheet
‪hasInlineStyleSheet(string $identifier)
Definition: AssetCollector.php:212
‪TYPO3\CMS\Core\Page\AssetCollector\addInlineStyleSheet
‪addInlineStyleSheet(string $identifier, string $source, array $attributes=[], array $options=[])
Definition: AssetCollector.php:111
‪TYPO3\CMS\Core\Page\AssetCollector\hasJavaScript
‪hasJavaScript(string $identifier)
Definition: AssetCollector.php:197
‪TYPO3\CMS\Core\Page\AssetCollector\getInlineStyleSheets
‪getInlineStyleSheets(?bool $priority=null)
Definition: AssetCollector.php:192
‪TYPO3\CMS\Core\Page\AssetCollector\hasMedia
‪hasMedia(string $fileName)
Definition: AssetCollector.php:217
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:26
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪TYPO3\CMS\Core\Page\AssetCollector\$styleSheets
‪array $styleSheets
Definition: AssetCollector.php:45
‪TYPO3\CMS\Core\Page\AssetCollector\getJavaScripts
‪getJavaScripts(?bool $priority=null)
Definition: AssetCollector.php:177
‪TYPO3\CMS\Core\Page\AssetCollector\$media
‪array $media
Definition: AssetCollector.php:47
‪TYPO3\CMS\Core\Page\AssetCollector\addJavaScript
‪addJavaScript(string $identifier, string $source, array $attributes=[], array $options=[])
Definition: AssetCollector.php:54
‪TYPO3\CMS\Core\Page\AssetCollector\$inlineJavaScripts
‪array $inlineJavaScripts
Definition: AssetCollector.php:44
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37