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