[!!!][TASK] Do not handle 'uploadsfolder' in ext_emconf anymore
[Packages/TYPO3.CMS.git] / typo3 / sysext / t3editor / Classes / Registry / ModeRegistry.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\T3editor\Registry;
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 use TYPO3\CMS\Core\SingletonInterface;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\T3editor\Exception\InvalidModeException;
21 use TYPO3\CMS\T3editor\Mode;
22
23 /**
24 * Registers and holds t3editor modes
25 * @internal
26 */
27 class ModeRegistry implements SingletonInterface
28 {
29 /**
30 * @var Mode[]
31 */
32 protected $registeredModes = [];
33
34 /**
35 * @var Mode
36 */
37 protected $defaultMode;
38
39 /**
40 * @return self
41 * @throws \InvalidArgumentException
42 */
43 public static function getInstance(): ModeRegistry
44 {
45 return GeneralUtility::makeInstance(static::class);
46 }
47
48 /**
49 * Registers modes for t3editor
50 *
51 * @param Mode $mode
52 * @return self
53 */
54 public function register(Mode $mode): ModeRegistry
55 {
56 $this->registeredModes[$mode->getIdentifier()] = $mode;
57 if ($mode->isDefault()) {
58 $this->defaultMode = $mode;
59 }
60
61 return $this;
62 }
63
64 /**
65 * Removes registered modes
66 *
67 * @param string $identifier
68 * @return self
69 */
70 public function unregister(string $identifier): ModeRegistry
71 {
72 if (isset($this->registeredModes[$identifier])) {
73 unset($this->registeredModes[$identifier]);
74 }
75
76 return $this;
77 }
78
79 /**
80 * @param string $identifier
81 * @return bool
82 */
83 public function isRegistered(string $identifier): bool
84 {
85 return isset($this->registeredModes[$identifier]);
86 }
87
88 /**
89 * @param string $identifier
90 * @return Mode
91 * @throws InvalidModeException
92 */
93 public function getByIdentifier(string $identifier): Mode
94 {
95 if ($this->isRegistered($identifier)) {
96 return $this->registeredModes[$identifier];
97 }
98
99 throw new InvalidModeException('Tried to get unregistered t3editor mode "' . $identifier . '"', 1499710202);
100 }
101
102 /**
103 * @param string $formatCode
104 * @return Mode
105 * @throws InvalidModeException
106 */
107 public function getByFormatCode(string $formatCode): Mode
108 {
109 foreach ($this->registeredModes as $mode) {
110 if ($mode->getFormatCode() === $formatCode) {
111 return $mode;
112 }
113 }
114
115 throw new InvalidModeException('Tried to get unregistered t3editor mode by format code "' . $formatCode . '"', 1499710203);
116 }
117
118 /**
119 * @param string $fileExtension
120 * @return Mode
121 * @throws InvalidModeException
122 */
123 public function getByFileExtension(string $fileExtension): Mode
124 {
125 foreach ($this->registeredModes as $mode) {
126 if (in_array($fileExtension, $mode->getBoundFileExtensions(), true)) {
127 return $mode;
128 }
129 }
130
131 throw new InvalidModeException('Cannot find a registered mode for requested file extension "' . $fileExtension . '"', 1500306488);
132 }
133
134 /**
135 * @return Mode
136 */
137 public function getDefaultMode(): Mode
138 {
139 return $this->defaultMode;
140 }
141 }