‪TYPO3CMS  ‪main
JavaScriptModuleInstruction.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 
21 
22 class ‪JavaScriptModuleInstruction implements \JsonSerializable
23 {
27  public const ‪FLAG_LOAD_IMPORTMAP = 2;
28 
32  public const ‪FLAG_USE_TOP_WINDOW = 16;
33 
34  public const ‪ITEM_ASSIGN = 'assign';
35  public const ‪ITEM_INVOKE = 'invoke';
36  public const ‪ITEM_INSTANCE = 'instance';
37 
38  protected string ‪$name;
39  protected ?string ‪$exportName;
40  protected int ‪$flags;
41  protected array ‪$items = [];
42 
47  public static function ‪create(string ‪$name, string ‪$exportName = null): self
48  {
49  $target = GeneralUtility::makeInstance(static::class, ‪$name, self::FLAG_LOAD_IMPORTMAP);
50  $target->exportName = ‪$exportName;
51  return $target;
52  }
53 
58  public static function ‪fromState(array $state)
59  {
60  $target = GeneralUtility::makeInstance(static::class, $state['name'], $state['flags'] ?? 0);
61  $target->exportName = $state['exportName'] ?? null;
62  $target->items = $state['items'];
63  return $target;
64  }
65 
69  public function ‪__construct(string ‪$name, int ‪$flags)
70  {
71  $this->name = ‪$name;
72  $this->flags = ‪$flags;
73  }
74 
78  public function ‪getState(): array
79  {
80  return [
81  'name' => ‪$this->name,
82  'exportName' => ‪$this->exportName,
83  'flags' => ‪$this->flags,
84  'items' => ‪$this->items,
85  ];
86  }
87 
88  public function ‪jsonSerialize(): array
89  {
90  return $this->‪getState();
91  }
92 
93  public function ‪getName(): string
94  {
95  return ‪$this->name;
96  }
97 
98  public function ‪getExportName(): ?string
99  {
100  return ‪$this->exportName;
101  }
102 
103  public function ‪getFlags(): int
104  {
105  return ‪$this->flags;
106  }
107 
108  public function ‪getItems(): array
109  {
110  return ‪$this->items;
111  }
112 
116  public function ‪addFlags(int ...‪$flags): self
117  {
118  foreach (‪$flags as $flag) {
119  $this->flags |= $flag;
120  }
121  return $this;
122  }
123 
128  public function ‪assign(array $assignments): self
129  {
130  $this->items[] = [
131  'type' => static::ITEM_ASSIGN,
132  'assignments' => $assignments,
133  ];
134  return $this;
135  }
136 
142  public function ‪invoke(?string $method = null, ...‪$args): self
143  {
144  $this->items[] = [
145  'type' => static::ITEM_INVOKE,
146  'method' => $method,
147  'args' => ‪$args,
148  ];
149  return $this;
150  }
151 
156  public function ‪instance(...‪$args): self
157  {
158  $this->items[] = [
159  'type' => static::ITEM_INSTANCE,
160  'args' => ‪$args,
161  ];
162  return $this;
163  }
164 
165  public function ‪shallLoadImportMap(): bool
166  {
167  return ($this->flags & self::FLAG_LOAD_IMPORTMAP) === ‪self::FLAG_LOAD_IMPORTMAP;
168  }
169 
170  public function ‪shallUseTopWindow(): bool
171  {
172  return ($this->flags & self::FLAG_USE_TOP_WINDOW) === ‪self::FLAG_USE_TOP_WINDOW;
173  }
174 }
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\ITEM_INVOKE
‪const ITEM_INVOKE
Definition: JavaScriptModuleInstruction.php:35
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\shallLoadImportMap
‪shallLoadImportMap()
Definition: JavaScriptModuleInstruction.php:165
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\create
‪static create(string $name, string $exportName=null)
Definition: JavaScriptModuleInstruction.php:47
‪TYPO3\CMS\Core\Page
Definition: AssetCollector.php:18
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction
Definition: JavaScriptModuleInstruction.php:23
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\fromState
‪static self fromState(array $state)
Definition: JavaScriptModuleInstruction.php:58
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\ITEM_INSTANCE
‪const ITEM_INSTANCE
Definition: JavaScriptModuleInstruction.php:36
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\jsonSerialize
‪jsonSerialize()
Definition: JavaScriptModuleInstruction.php:88
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\addFlags
‪$this addFlags(int ... $flags)
Definition: JavaScriptModuleInstruction.php:116
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\$exportName
‪string $exportName
Definition: JavaScriptModuleInstruction.php:39
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\shallUseTopWindow
‪shallUseTopWindow()
Definition: JavaScriptModuleInstruction.php:170
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\getFlags
‪getFlags()
Definition: JavaScriptModuleInstruction.php:103
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\assign
‪static assign(array $assignments)
Definition: JavaScriptModuleInstruction.php:128
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\$items
‪array $items
Definition: JavaScriptModuleInstruction.php:41
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\__construct
‪__construct(string $name, int $flags)
Definition: JavaScriptModuleInstruction.php:69
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\getName
‪getName()
Definition: JavaScriptModuleInstruction.php:93
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\FLAG_LOAD_IMPORTMAP
‪const FLAG_LOAD_IMPORTMAP
Definition: JavaScriptModuleInstruction.php:27
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\$flags
‪int $flags
Definition: JavaScriptModuleInstruction.php:40
‪$args
‪$args
Definition: validateRstFiles.php:258
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\FLAG_USE_TOP_WINDOW
‪const FLAG_USE_TOP_WINDOW
Definition: JavaScriptModuleInstruction.php:32
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\instance
‪static instance(... $args)
Definition: JavaScriptModuleInstruction.php:156
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\getItems
‪getItems()
Definition: JavaScriptModuleInstruction.php:108
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\invoke
‪static invoke(?string $method=null,... $args)
Definition: JavaScriptModuleInstruction.php:142
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\getExportName
‪getExportName()
Definition: JavaScriptModuleInstruction.php:98
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\$name
‪string $name
Definition: JavaScriptModuleInstruction.php:38
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\ITEM_ASSIGN
‪const ITEM_ASSIGN
Definition: JavaScriptModuleInstruction.php:34
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\getState
‪getState()
Definition: JavaScriptModuleInstruction.php:78