c20273b6eb5d162bd669b853e8db0dfa9e56c0d4
[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 /**
18 * Instantiate and configure all known features and presets
19 */
20 class FeatureManager {
21
22 /**
23 * @var \TYPO3\CMS\Extbase\Object\ObjectManager
24 */
25 protected $objectManager = NULL;
26
27 /**
28 * @param \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager
29 */
30 public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager) {
31 $this->objectManager = $objectManager;
32 }
33
34 /**
35 * @var array List of feature class names
36 */
37 protected $featureRegistry = array(
38 \TYPO3\CMS\Install\Configuration\Charset\CharsetFeature::class,
39 \TYPO3\CMS\Install\Configuration\Context\ContextFeature::class,
40 \TYPO3\CMS\Install\Configuration\Image\ImageFeature::class,
41 \TYPO3\CMS\Install\Configuration\ExtbaseObjectCache\ExtbaseObjectCacheFeature::class,
42 );
43
44 /**
45 * Get initialized list of features with possible presets
46 *
47 * @param array $postValues List of $POST values
48 * @return array<FeatureInterface>
49 * @throws Exception
50 */
51 public function getInitializedFeatures(array $postValues) {
52 $features = array();
53 foreach ($this->featureRegistry as $featureClass) {
54 /** @var FeatureInterface $featureInstance */
55 $featureInstance = $this->objectManager->get($featureClass);
56 if (!($featureInstance instanceof FeatureInterface)) {
57 throw new Exception(
58 'Feature ' . $featureClass . ' does not implement FeatureInterface',
59 1378644593
60 );
61 }
62 $featureInstance->initializePresets($postValues);
63 $features[] = $featureInstance;
64 }
65 return $features;
66 }
67
68 /**
69 * Get configuration values to be set to LocalConfiguration from
70 * list of selected $POST feature presets
71 *
72 * @param array $postValues List of $POST values
73 * @return array List of configuration values
74 */
75 public function getConfigurationForSelectedFeaturePresets(array $postValues) {
76 $localConfigurationValuesToSet = array();
77 $features = $this->getInitializedFeatures($postValues);
78 foreach ($features as $feature) {
79 /** @var FeatureInterface $feature */
80 $featureName = $feature->getName();
81 $presets = $feature->getPresetsOrderedByPriority();
82 foreach ($presets as $preset) {
83 /** @var PresetInterface $preset */
84 $presetName = $preset->getName();
85 if (!empty($postValues[$featureName]['enable'])
86 && $postValues[$featureName]['enable'] === $presetName
87 && (!$preset->isActive() || $preset instanceof CustomPresetInterface)
88 ) {
89 $localConfigurationValuesToSet = array_merge(
90 $localConfigurationValuesToSet,
91 $preset->getConfigurationValues()
92 );
93 }
94 }
95 }
96 return $localConfigurationValuesToSet;
97 }
98
99 /**
100 * Cycle through features and get settings. First matching
101 * preset (highest priority) will be selected.
102 *
103 * @return array Configuration settings
104 */
105 public function getBestMatchingConfigurationForAllFeatures() {
106 $localConfigurationValuesToSet = array();
107 $features = $this->getInitializedFeatures(array());
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
131 }