‪TYPO3CMS  10.4
ModeRegistry.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 
24 
30 {
34  protected ‪$registeredModes = [];
35 
39  protected ‪$defaultMode;
40 
45  public static function ‪getInstance(): ‪ModeRegistry
46  {
47  return GeneralUtility::makeInstance(static::class);
48  }
49 
56  public function register(‪Mode $mode): ‪ModeRegistry
57  {
58  $this->registeredModes[$mode->getIdentifier()] = $mode;
59  if ($mode->isDefault()) {
60  $this->defaultMode = $mode;
61  }
62 
63  return $this;
64  }
65 
72  public function ‪unregister(string $identifier): ‪ModeRegistry
73  {
74  if (isset($this->registeredModes[$identifier])) {
75  unset($this->registeredModes[$identifier]);
76  }
77 
78  return $this;
79  }
80 
85  public function ‪isRegistered(string $identifier): bool
86  {
87  return isset($this->registeredModes[$identifier]);
88  }
89 
95  public function ‪getByIdentifier(string $identifier): ‪Mode
96  {
97  if ($this->‪isRegistered($identifier)) {
98  return $this->registeredModes[$identifier];
99  }
100 
101  throw new ‪InvalidModeException('Tried to get unregistered t3editor mode "' . $identifier . '"', 1499710202);
102  }
103 
109  public function ‪getByFormatCode(string $formatCode): ‪Mode
110  {
111  foreach ($this->registeredModes as $mode) {
112  if ($mode->getFormatCode() === $formatCode) {
113  return $mode;
114  }
115  }
116 
117  throw new ‪InvalidModeException('Tried to get unregistered t3editor mode by format code "' . $formatCode . '"', 1499710203);
118  }
119 
125  public function ‪getByFileExtension(string $fileExtension): ‪Mode
126  {
127  foreach ($this->registeredModes as $mode) {
128  if (in_array($fileExtension, $mode->getBoundFileExtensions(), true)) {
129  return $mode;
130  }
131  }
132 
133  throw new ‪InvalidModeException('Cannot find a registered mode for requested file extension "' . $fileExtension . '"', 1500306488);
134  }
135 
139  public function ‪getDefaultMode(): ‪Mode
140  {
141  return ‪$this->defaultMode;
142  }
143 }
‪TYPO3\CMS\T3editor\Registry\ModeRegistry\getByFormatCode
‪Mode getByFormatCode(string $formatCode)
Definition: ModeRegistry.php:107
‪TYPO3\CMS\T3editor\Registry\ModeRegistry\getByFileExtension
‪Mode getByFileExtension(string $fileExtension)
Definition: ModeRegistry.php:123
‪TYPO3\CMS\T3editor\Registry\ModeRegistry\$registeredModes
‪Mode[] $registeredModes
Definition: ModeRegistry.php:33
‪TYPO3\CMS\T3editor\Registry\ModeRegistry\unregister
‪self unregister(string $identifier)
Definition: ModeRegistry.php:70
‪TYPO3\CMS\T3editor\Registry\ModeRegistry\getByIdentifier
‪Mode getByIdentifier(string $identifier)
Definition: ModeRegistry.php:93
‪TYPO3\CMS\T3editor\Registry\ModeRegistry\isRegistered
‪bool isRegistered(string $identifier)
Definition: ModeRegistry.php:83
‪TYPO3\CMS\T3editor\Mode
Definition: Mode.php:25
‪TYPO3\CMS\T3editor\Registry\ModeRegistry\getDefaultMode
‪Mode getDefaultMode()
Definition: ModeRegistry.php:137
‪TYPO3\CMS\T3editor\Exception\InvalidModeException
Definition: InvalidModeException.php:27
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:23
‪TYPO3\CMS\T3editor\Registry
Definition: AddonRegistry.php:18
‪TYPO3\CMS\T3editor\Registry\ModeRegistry
Definition: ModeRegistry.php:30
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\T3editor\Registry\ModeRegistry\getInstance
‪static self getInstance()
Definition: ModeRegistry.php:43
‪TYPO3\CMS\T3editor\Registry\ModeRegistry\$defaultMode
‪Mode $defaultMode
Definition: ModeRegistry.php:37