‪TYPO3CMS  11.5
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 
46  public static function ‪getInstance(): ‪ModeRegistry
47  {
48  trigger_error(__CLASS__ . '::getInstance() will be removed in TYPO3 v12.0. Use Dependency Injection or GeneralUtility::makeInstance() if DI is not possible.', E_USER_DEPRECATED);
49  return GeneralUtility::makeInstance(static::class);
50  }
51 
58  public function register(‪Mode $mode): ‪ModeRegistry
59  {
60  $this->registeredModes[$mode->getIdentifier()] = $mode;
61  if ($mode->isDefault()) {
62  $this->defaultMode = $mode;
63  }
64 
65  return $this;
66  }
67 
74  public function ‪unregister(string $identifier): ‪ModeRegistry
75  {
76  if (isset($this->registeredModes[$identifier])) {
77  unset($this->registeredModes[$identifier]);
78  }
79 
80  return $this;
81  }
82 
87  public function ‪isRegistered(string $identifier): bool
88  {
89  return isset($this->registeredModes[$identifier]);
90  }
91 
97  public function ‪getByIdentifier(string $identifier): ‪Mode
98  {
99  if ($this->‪isRegistered($identifier)) {
100  return $this->registeredModes[$identifier];
101  }
102 
103  throw new ‪InvalidModeException('Tried to get unregistered t3editor mode "' . $identifier . '"', 1499710202);
104  }
105 
111  public function ‪getByFormatCode(string $formatCode): ‪Mode
112  {
113  foreach ($this->registeredModes as $mode) {
114  if ($mode->getFormatCode() === $formatCode) {
115  return $mode;
116  }
117  }
118 
119  throw new ‪InvalidModeException('Tried to get unregistered t3editor mode by format code "' . $formatCode . '"', 1499710203);
120  }
121 
127  public function ‪getByFileExtension(string $fileExtension): ‪Mode
128  {
129  foreach ($this->registeredModes as $mode) {
130  if (in_array($fileExtension, $mode->getBoundFileExtensions(), true)) {
131  return $mode;
132  }
133  }
134 
135  throw new ‪InvalidModeException('Cannot find a registered mode for requested file extension "' . $fileExtension . '"', 1500306488);
136  }
137 
141  public function ‪getDefaultMode(): ‪Mode
142  {
143  return ‪$this->defaultMode;
144  }
145 }
‪TYPO3\CMS\T3editor\Registry\ModeRegistry\getByFormatCode
‪Mode getByFormatCode(string $formatCode)
Definition: ModeRegistry.php:109
‪TYPO3\CMS\T3editor\Registry\ModeRegistry\getByFileExtension
‪Mode getByFileExtension(string $fileExtension)
Definition: ModeRegistry.php:125
‪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:72
‪TYPO3\CMS\T3editor\Registry\ModeRegistry\getByIdentifier
‪Mode getByIdentifier(string $identifier)
Definition: ModeRegistry.php:95
‪TYPO3\CMS\T3editor\Registry\ModeRegistry\isRegistered
‪bool isRegistered(string $identifier)
Definition: ModeRegistry.php:85
‪TYPO3\CMS\T3editor\Mode
Definition: Mode.php:25
‪TYPO3\CMS\T3editor\Registry\ModeRegistry\getDefaultMode
‪Mode getDefaultMode()
Definition: ModeRegistry.php:139
‪TYPO3\CMS\T3editor\Exception\InvalidModeException
Definition: InvalidModeException.php:26
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪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:50
‪TYPO3\CMS\T3editor\Registry\ModeRegistry\getInstance
‪static self getInstance()
Definition: ModeRegistry.php:44
‪TYPO3\CMS\T3editor\Registry\ModeRegistry\$defaultMode
‪Mode $defaultMode
Definition: ModeRegistry.php:37