Revert "[TASK] Avoid slow array functions in loops"
[Packages/TYPO3.CMS.git] / typo3 / sysext / t3editor / Classes / Registry / AddonRegistry.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\Addon;
21
22 /**
23 * Registers and holds t3editor modes
24 * @internal
25 */
26 class AddonRegistry implements SingletonInterface
27 {
28 /**
29 * @var Addon[]
30 */
31 protected $registeredAddons = [];
32
33 /**
34 * @return self
35 * @throws \InvalidArgumentException
36 */
37 public static function getInstance(): AddonRegistry
38 {
39 return GeneralUtility::makeInstance(static::class);
40 }
41
42 /**
43 * Registers addons for global use in t3editor
44 *
45 * @param Addon $addon
46 * @return self
47 */
48 public function register(Addon $addon): AddonRegistry
49 {
50 $this->registeredAddons[$addon->getIdentifier()] = $addon;
51
52 return $this;
53 }
54
55 /**
56 * @param string $mode
57 * @return Addon[]
58 */
59 public function getForMode(string $mode = ''): array
60 {
61 $addons = [];
62
63 foreach ($this->registeredAddons as $addon) {
64 if (empty($addon->getModes()) || in_array($mode, $addon->getModes(), true)) {
65 $addons[] = $addon;
66 }
67 }
68
69 return $addons;
70 }
71
72 /**
73 * @param Addon[] $addons
74 * @return array
75 */
76 public function compileSettings(array $addons): array
77 {
78 $settings = [];
79 foreach ($addons as $addon) {
80 $settings = array_merge($settings, $addon->getOptions());
81 }
82
83 return $settings;
84 }
85 }