Revert "[TASK] Avoid slow array functions in loops"
[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 use TYPO3\CMS\Install\Configuration\Cache\CacheFeature;
19 use TYPO3\CMS\Install\Configuration\Context\ContextFeature;
20 use TYPO3\CMS\Install\Configuration\Image\ImageFeature;
21 use TYPO3\CMS\Install\Configuration\Mail\MailFeature;
22 use TYPO3\CMS\Install\Configuration\PasswordHashing\PasswordHashingFeature;
23
24 /**
25 * Instantiate and configure all known features and presets
26 * @internal only to be used within EXT:install
27 */
28 class FeatureManager
29 {
30 /**
31 * @var array List of feature class names
32 */
33 protected $featureRegistry = [
34 CacheFeature::class,
35 ContextFeature::class,
36 ImageFeature::class,
37 MailFeature::class,
38 PasswordHashingFeature::class,
39 ];
40
41 /**
42 * Get initialized list of features with possible presets
43 *
44 * @param array $postValues List of $POST values
45 * @return FeatureInterface[]
46 * @throws Exception
47 */
48 public function getInitializedFeatures(array $postValues = [])
49 {
50 $features = [];
51 foreach ($this->featureRegistry as $featureClass) {
52 /** @var FeatureInterface $featureInstance */
53 $featureInstance = GeneralUtility::makeInstance($featureClass);
54 if (!($featureInstance instanceof FeatureInterface)) {
55 throw new Exception(
56 'Feature ' . $featureClass . ' does not implement FeatureInterface',
57 1378644593
58 );
59 }
60 $featureInstance->initializePresets($postValues);
61 $features[] = $featureInstance;
62 }
63 return $features;
64 }
65
66 /**
67 * Get configuration values to be set to LocalConfiguration from
68 * list of selected $POST feature presets
69 *
70 * @param array $postValues List of $POST values
71 * @return array List of configuration values
72 */
73 public function getConfigurationForSelectedFeaturePresets(array $postValues)
74 {
75 $localConfigurationValuesToSet = [];
76 $features = $this->getInitializedFeatures($postValues);
77 foreach ($features as $feature) {
78 /** @var FeatureInterface $feature */
79 $featureName = $feature->getName();
80 $presets = $feature->getPresetsOrderedByPriority();
81 foreach ($presets as $preset) {
82 /** @var PresetInterface $preset */
83 $presetName = $preset->getName();
84 if (!empty($postValues[$featureName]['enable'])
85 && $postValues[$featureName]['enable'] === $presetName
86 && (!$preset->isActive() || $preset instanceof CustomPresetInterface)
87 ) {
88 $localConfigurationValuesToSet = array_merge(
89 $localConfigurationValuesToSet,
90 $preset->getConfigurationValues()
91 );
92 }
93 }
94 }
95 return $localConfigurationValuesToSet;
96 }
97
98 /**
99 * Cycle through features and get settings. First matching
100 * preset (highest priority) will be selected.
101 *
102 * @return array Configuration settings
103 */
104 public function getBestMatchingConfigurationForAllFeatures()
105 {
106 $localConfigurationValuesToSet = [];
107 $features = $this->getInitializedFeatures([]);
108 foreach ($features as $feature) {
109 /** @var FeatureInterface $feature */
110 $presets = $feature->getPresetsOrderedByPriority();
111 foreach ($presets as $preset) {
112 // Only choose "normal" presets, no custom presets
113 if ($preset instanceof CustomPresetInterface) {
114 break;
115 }
116
117 /** @var PresetInterface $preset */
118 if ($preset->isAvailable()) {
119 $localConfigurationValuesToSet = array_merge(
120 $localConfigurationValuesToSet,
121 $preset->getConfigurationValues()
122 );
123 // Setting for this feature done, go to next feature
124 break;
125 }
126 }
127 }
128 return $localConfigurationValuesToSet;
129 }
130 }