Revert "[TASK] Avoid slow array functions in loops"
[Packages/TYPO3.CMS.git] / typo3 / sysext / t3editor / Classes / T3editor.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\T3editor;
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\Cache\CacheManager;
19 use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
20 use TYPO3\CMS\Core\Core\Environment;
21 use TYPO3\CMS\Core\Package\PackageManager;
22 use TYPO3\CMS\Core\SingletonInterface;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24 use TYPO3\CMS\T3editor\Registry\AddonRegistry;
25 use TYPO3\CMS\T3editor\Registry\ModeRegistry;
26
27 /**
28 * Provides necessary code to setup a t3editor instance in FormEngine
29 * @internal
30 */
31 class T3editor implements SingletonInterface
32 {
33 /**
34 * @var array
35 */
36 protected $configuration;
37
38 /**
39 * Registers the configuration and bootstraps the modes / addons.
40 *
41 * @throws \InvalidArgumentException
42 */
43 public function registerConfiguration()
44 {
45 $configuration = $this->buildConfiguration();
46
47 if (isset($configuration['modes'])) {
48 $modeRegistry = ModeRegistry::getInstance();
49 foreach ($configuration['modes'] as $formatCode => $mode) {
50 $modeInstance = GeneralUtility::makeInstance(Mode::class, $mode['module'])->setFormatCode($formatCode);
51
52 if (!empty($mode['extensions']) && is_array($mode['extensions'])) {
53 $modeInstance->bindToFileExtensions($mode['extensions']);
54 }
55
56 if (isset($mode['default']) && $mode['default'] === true) {
57 $modeInstance->setAsDefault();
58 }
59
60 $modeRegistry->register($modeInstance);
61 }
62 }
63
64 $addonRegistry = GeneralUtility::makeInstance(AddonRegistry::class);
65 if (isset($configuration['addons'])) {
66 foreach ($configuration['addons'] as $addon) {
67 $addonInstance = GeneralUtility::makeInstance(Addon::class, $addon['module']);
68
69 if (!empty($addon['cssFiles']) && is_array($addon['cssFiles'])) {
70 $addonInstance->setCssFiles($addon['cssFiles']);
71 }
72
73 if (!empty($addon['options']) && is_array($addon['options'])) {
74 $addonInstance->setOptions($addon['options']);
75 }
76
77 if (!empty($addon['modes']) && is_array($addon['modes'])) {
78 $addonInstance->setModes($addon['modes']);
79 }
80
81 $addonRegistry->register($addonInstance);
82 }
83 }
84 }
85
86 /**
87 * Compiles the configuration for t3editor. Configuration is stored in caching framework.
88 *
89 * @return array
90 * @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
91 * @throws \TYPO3\CMS\Core\Cache\Exception\InvalidDataException
92 * @throws \InvalidArgumentException
93 */
94 protected function buildConfiguration(): array
95 {
96 if ($this->configuration !== null) {
97 return $this->configuration;
98 }
99
100 $this->configuration = [
101 'modes' => [],
102 'addons' => [],
103 ];
104
105 $cache = $this->getCache();
106 $cacheIdentifier = $this->generateCacheIdentifier('T3editorConfiguration');
107 $configurationFromCache = $cache->get($cacheIdentifier);
108 if ($configurationFromCache !== false) {
109 $this->configuration = $configurationFromCache;
110 } else {
111 $packageManager = GeneralUtility::makeInstance(PackageManager::class);
112 $packages = $packageManager->getActivePackages();
113
114 foreach ($packages as $package) {
115 $configurationPath = $package->getPackagePath() . 'Configuration/Backend/T3editor';
116 $modesFileNameForPackage = $configurationPath . '/Modes.php';
117 if (is_file($modesFileNameForPackage)) {
118 $definedModes = require_once $modesFileNameForPackage;
119 if (is_array($definedModes)) {
120 $this->configuration['modes'] = array_merge($this->configuration['modes'], $definedModes);
121 }
122 }
123
124 $addonsFileNameForPackage = $configurationPath . '/Addons.php';
125 if (is_file($addonsFileNameForPackage)) {
126 $definedAddons = require_once $addonsFileNameForPackage;
127 if (is_array($definedAddons)) {
128 $this->configuration['addons'] = array_merge($this->configuration['addons'], $definedAddons);
129 }
130 }
131 }
132 $cache->set($cacheIdentifier, $this->configuration);
133 }
134
135 return $this->configuration;
136 }
137
138 /**
139 * @param string $key
140 * @return string
141 */
142 protected function generateCacheIdentifier(string $key): string
143 {
144 return $key . '_' . sha1(TYPO3_version . Environment::getProjectPath() . $key);
145 }
146
147 /**
148 * @return FrontendInterface
149 * @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
150 * @throws \InvalidArgumentException
151 */
152 protected function getCache(): FrontendInterface
153 {
154 return GeneralUtility::makeInstance(CacheManager::class)->getCache('assets');
155 }
156 }