‪TYPO3CMS  11.5
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 {
28  public const ‪FLAG_LOAD_REQUIRE_JS = 1;
29 
33  public const ‪FLAG_USE_TOP_WINDOW = 16;
34 
35  public const ‪ITEM_ASSIGN = 'assign';
36  public const ‪ITEM_INVOKE = 'invoke';
37  public const ‪ITEM_INSTANCE = 'instance';
38 
39  protected string ‪$name;
40  protected ?string ‪$exportName;
41  protected int ‪$flags;
42  protected array ‪$items = [];
43 
49  public static function ‪forRequireJS(string ‪$name, ?string ‪$exportName = null): self
50  {
51  $target = GeneralUtility::makeInstance(static::class, ‪$name, self::FLAG_LOAD_REQUIRE_JS);
52  $target->exportName = ‪$exportName;
53  return $target;
54  }
55 
60  public function ‪__construct(string ‪$name, int ‪$flags)
61  {
62  $this->name = ‪$name;
63  $this->flags = ‪$flags;
64  }
65 
66  public function ‪jsonSerialize(): array
67  {
68  return [
69  'name' => ‪$this->name,
70  'exportName' => ‪$this->exportName,
71  'flags' => ‪$this->flags,
72  'items' => ‪$this->items,
73  ];
74  }
75 
76  public function ‪getName(): string
77  {
78  return ‪$this->name;
79  }
80 
81  public function ‪getExportName(): ?string
82  {
83  return ‪$this->exportName;
84  }
85 
86  public function ‪getFlags(): int
87  {
88  return ‪$this->flags;
89  }
90 
91  public function ‪getItems(): array
92  {
93  return ‪$this->items;
94  }
95 
100  public function ‪addFlags(int ...‪$flags): self
101  {
102  foreach (‪$flags as $flag) {
103  $this->flags |= $flag;
104  }
105  return $this;
106  }
107 
112  public function ‪assign(array $assignments): self
113  {
114  $this->items[] = [
115  'type' => static::ITEM_ASSIGN,
116  'assignments' => $assignments,
117  ];
118  return $this;
119  }
120 
126  public function ‪invoke(string $method, ...‪$args): self
127  {
128  $this->items[] = [
129  'type' => static::ITEM_INVOKE,
130  'method' => $method,
131  'args' => ‪$args,
132  ];
133  return $this;
134  }
135 
140  public function ‪instance(...‪$args): self
141  {
142  $this->items[] = [
143  'type' => static::ITEM_INSTANCE,
144  'args' => ‪$args,
145  ];
146  return $this;
147  }
148 
149  public function ‪shallLoadRequireJs(): bool
150  {
151  return ($this->flags & self::FLAG_LOAD_REQUIRE_JS) === ‪self::FLAG_LOAD_REQUIRE_JS;
152  }
153 
154  public function ‪shallUseTopWindow(): bool
155  {
156  return ($this->flags & self::FLAG_USE_TOP_WINDOW) === ‪self::FLAG_USE_TOP_WINDOW;
157  }
158 }
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\ITEM_INVOKE
‪const ITEM_INVOKE
Definition: JavaScriptModuleInstruction.php:36
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\FLAG_LOAD_REQUIRE_JS
‪const FLAG_LOAD_REQUIRE_JS
Definition: JavaScriptModuleInstruction.php:28
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\invoke
‪static invoke(string $method,... $args)
Definition: JavaScriptModuleInstruction.php:126
‪TYPO3\CMS\Core\Page
Definition: AssetCollector.php:18
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction
Definition: JavaScriptModuleInstruction.php:23
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\ITEM_INSTANCE
‪const ITEM_INSTANCE
Definition: JavaScriptModuleInstruction.php:37
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\jsonSerialize
‪jsonSerialize()
Definition: JavaScriptModuleInstruction.php:66
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\addFlags
‪$this addFlags(int ... $flags)
Definition: JavaScriptModuleInstruction.php:100
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\$exportName
‪string $exportName
Definition: JavaScriptModuleInstruction.php:40
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\shallUseTopWindow
‪shallUseTopWindow()
Definition: JavaScriptModuleInstruction.php:154
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\getFlags
‪getFlags()
Definition: JavaScriptModuleInstruction.php:86
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\assign
‪static assign(array $assignments)
Definition: JavaScriptModuleInstruction.php:112
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\$items
‪array $items
Definition: JavaScriptModuleInstruction.php:42
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\__construct
‪__construct(string $name, int $flags)
Definition: JavaScriptModuleInstruction.php:60
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\getName
‪getName()
Definition: JavaScriptModuleInstruction.php:76
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\$flags
‪int $flags
Definition: JavaScriptModuleInstruction.php:41
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\forRequireJS
‪static self forRequireJS(string $name, ?string $exportName=null)
Definition: JavaScriptModuleInstruction.php:49
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\shallLoadRequireJs
‪shallLoadRequireJs()
Definition: JavaScriptModuleInstruction.php:149
‪$args
‪$args
Definition: validateRstFiles.php:215
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\FLAG_USE_TOP_WINDOW
‪const FLAG_USE_TOP_WINDOW
Definition: JavaScriptModuleInstruction.php:33
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\instance
‪static instance(... $args)
Definition: JavaScriptModuleInstruction.php:140
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\getItems
‪getItems()
Definition: JavaScriptModuleInstruction.php:91
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\getExportName
‪getExportName()
Definition: JavaScriptModuleInstruction.php:81
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\$name
‪string $name
Definition: JavaScriptModuleInstruction.php:39
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\ITEM_ASSIGN
‪const ITEM_ASSIGN
Definition: JavaScriptModuleInstruction.php:35