[TASK] Get rid of ObjectManager in install tool
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / Configuration / AbstractFeature.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 * Abstract feature class implements common code
21 */
22 abstract class AbstractFeature
23 {
24 /**
25 * @var string Name of feature
26 */
27 protected $name = '';
28
29 /**
30 * @var array List of preset classes
31 */
32 protected $presetRegistry = array();
33
34 /**
35 * @var array Holds instances of presets
36 */
37 protected $presetInstances = array();
38
39 /**
40 * @var array List of $POST values
41 */
42 protected $postValues = array();
43
44 /**
45 * Initialize presets of feature
46 *
47 * @param array $postValues List of $POST values of this feature
48 * @return void
49 * @throws Exception
50 */
51 public function initializePresets(array $postValues)
52 {
53 // Give feature sub array of $POST values to preset and set to own property
54 $featurePostValues = array();
55 if (!empty($postValues[$this->name])) {
56 $featurePostValues = $postValues[$this->name];
57 }
58 $this->postValues = $featurePostValues;
59
60 $isNonCustomPresetActive = false;
61 $customPresetFound = false;
62 foreach ($this->presetRegistry as $presetClass) {
63 /** @var PresetInterface $presetInstance */
64 $presetInstance = GeneralUtility::makeInstance($presetClass);
65 if (!($presetInstance instanceof PresetInterface)) {
66 throw new Exception(
67 'Preset ' . $presetClass . ' does not implement PresetInterface',
68 1378644821
69 );
70 }
71
72 $presetInstance->setPostValues($featurePostValues);
73
74 // Custom preset is set active if no preset before is active
75 if ($presetInstance->isActive()) {
76 $isNonCustomPresetActive = true;
77 }
78 if ($presetInstance instanceof CustomPresetInterface
79 && !$isNonCustomPresetActive
80 ) {
81 // Throw Exception if two custom presets are registered
82 if ($customPresetFound === true) {
83 throw new Exception(
84 'Preset ' . $presetClass . ' implements CustomPresetInterface, but another'
85 . ' custom preset is already registered',
86 1378645039
87 );
88 }
89
90 /** @var CustomPresetInterface $presetInstance */
91 $presetInstance->setActive();
92 $customPresetFound = true;
93 }
94
95 $this->presetInstances[] = $presetInstance;
96 }
97 }
98
99 /**
100 * Return presets ordered by priority
101 *
102 * @return array<PresetInterface>
103 * @throws Exception
104 */
105 public function getPresetsOrderedByPriority()
106 {
107 if (empty($this->presetInstances)) {
108 throw new Exception(
109 'Presets not initialized',
110 1378645155
111 );
112 }
113 $orderedPresets = array();
114 foreach ($this->presetInstances as $presetInstance) {
115 /** @var PresetInterface $presetInstance */
116 $orderedPresets[$presetInstance->getPriority()] = $presetInstance;
117 }
118 krsort($orderedPresets, SORT_NUMERIC);
119 return $orderedPresets;
120 }
121
122 /**
123 * Return name of feature
124 *
125 * @return string Name of feature
126 */
127 public function getName()
128 {
129 return $this->name;
130 }
131 }