[TASK] Migrate to short array syntax
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / Configuration / FeatureManager.php
1 <?php
2 namespace TYPO3\CMS\Install\Configuration;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18
19 /**
20 * Instantiate and configure all known features and presets
21 */
22 class FeatureManager
23 {
24 /**
25 * @var array List of feature class names
26 */
27 protected $featureRegistry = [
28 \TYPO3\CMS\Install\Configuration\Context\ContextFeature::class,
29 \TYPO3\CMS\Install\Configuration\Image\ImageFeature::class,
30 \TYPO3\CMS\Install\Configuration\ExtbaseObjectCache\ExtbaseObjectCacheFeature::class,
31 \TYPO3\CMS\Install\Configuration\Mail\MailFeature::class,
32 ];
33
34 /**
35 * Get initialized list of features with possible presets
36 *
37 * @param array $postValues List of $POST values
38 * @return array<FeatureInterface>
39 * @throws Exception
40 */
41 public function getInitializedFeatures(array $postValues)
42 {
43 $features = [];
44 foreach ($this->featureRegistry as $featureClass) {
45 /** @var FeatureInterface $featureInstance */
46 $featureInstance = GeneralUtility::makeInstance($featureClass);
47 if (!($featureInstance instanceof FeatureInterface)) {
48 throw new Exception(
49 'Feature ' . $featureClass . ' does not implement FeatureInterface',
50 1378644593
51 );
52 }
53 $featureInstance->initializePresets($postValues);
54 $features[] = $featureInstance;
55 }
56 return $features;
57 }
58
59 /**
60 * Get configuration values to be set to LocalConfiguration from
61 * list of selected $POST feature presets
62 *
63 * @param array $postValues List of $POST values
64 * @return array List of configuration values
65 */
66 public function getConfigurationForSelectedFeaturePresets(array $postValues)
67 {
68 $localConfigurationValuesToSet = [];
69 $features = $this->getInitializedFeatures($postValues);
70 foreach ($features as $feature) {
71 /** @var FeatureInterface $feature */
72 $featureName = $feature->getName();
73 $presets = $feature->getPresetsOrderedByPriority();
74 foreach ($presets as $preset) {
75 /** @var PresetInterface $preset */
76 $presetName = $preset->getName();
77 if (!empty($postValues[$featureName]['enable'])
78 && $postValues[$featureName]['enable'] === $presetName
79 && (!$preset->isActive() || $preset instanceof CustomPresetInterface)
80 ) {
81 $localConfigurationValuesToSet = array_merge(
82 $localConfigurationValuesToSet,
83 $preset->getConfigurationValues()
84 );
85 }
86 }
87 }
88 return $localConfigurationValuesToSet;
89 }
90
91 /**
92 * Cycle through features and get settings. First matching
93 * preset (highest priority) will be selected.
94 *
95 * @return array Configuration settings
96 */
97 public function getBestMatchingConfigurationForAllFeatures()
98 {
99 $localConfigurationValuesToSet = [];
100 $features = $this->getInitializedFeatures([]);
101 foreach ($features as $feature) {
102 /** @var FeatureInterface $feature */
103 $presets = $feature->getPresetsOrderedByPriority();
104 foreach ($presets as $preset) {
105 // Only choose "normal" presets, no custom presets
106 if ($preset instanceof CustomPresetInterface) {
107 break;
108 }
109
110 /** @var PresetInterface $preset */
111 if ($preset->isAvailable()) {
112 $localConfigurationValuesToSet = array_merge(
113 $localConfigurationValuesToSet,
114 $preset->getConfigurationValues()
115 );
116 // Setting for this feature done, go to next feature
117 break;
118 }
119 }
120 }
121 return $localConfigurationValuesToSet;
122 }
123 }