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