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