48c564568b61aa90bfd596ed0cfb96cbb98683f8
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / Configuration / AbstractFeature.php
1 <?php
2 namespace TYPO3\CMS\Install\Configuration;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2013 Christian Kuhn <lolli@schwarzbu.ch>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 /**
28 * Abstract feature class implements common code
29 */
30 abstract class AbstractFeature {
31
32 /**
33 * @var \TYPO3\CMS\Extbase\Object\ObjectManager
34 * @inject
35 */
36 protected $objectManager = NULL;
37
38 /**
39 * @var string Name of feature
40 */
41 protected $name = '';
42
43 /**
44 * @var array List of preset classes
45 */
46 protected $presetRegistry = array();
47
48 /**
49 * @var array Holds instances of presets
50 */
51 protected $presetInstances = array();
52
53 /**
54 * @var array List of $POST values
55 */
56 protected $postValues = array();
57
58 /**
59 * Initialize presets of feature
60 *
61 * @param array $postValues List of $POST values of this feature
62 * @return void
63 * @throws Exception
64 */
65 public function initializePresets(array $postValues) {
66 // Give feature sub array of $POST values to preset and set to own property
67 $featurePostValues = array();
68 if (!empty($postValues[$this->name])) {
69 $featurePostValues = $postValues[$this->name];
70 }
71 $this->postValues = $featurePostValues;
72
73 $isNonCustomPresetActive = FALSE;
74 $customPresetFound = FALSE;
75 foreach ($this->presetRegistry as $presetClass) {
76 /** @var PresetInterface $presetInstance */
77 $presetInstance = $this->objectManager->get($presetClass);
78 if (!($presetInstance instanceof PresetInterface)) {
79 throw new Exception(
80 'Preset ' . $presetClass . ' does not implement PresetInterface',
81 1378644821
82 );
83 }
84
85 $presetInstance->setPostValues($featurePostValues);
86
87 // Custom preset is set active if no preset before is active
88 if ($presetInstance->isActive()) {
89 $isNonCustomPresetActive = TRUE;
90 }
91 if ($presetInstance instanceof CustomPresetInterface
92 && !$isNonCustomPresetActive
93 ) {
94 // Throw Exception if two custom presets are registered
95 if ($customPresetFound === TRUE) {
96 throw new Exception(
97 'Preset ' . $presetClass . ' implements CustomPresetInterface, but another'
98 . ' custom preset is already registered',
99 1378645039
100 );
101 }
102
103 /** @var CustomPresetInterface $presetInstance */
104 $presetInstance->setActive();
105 $customPresetFound = TRUE;
106 }
107
108 $this->presetInstances[] = $presetInstance;
109 }
110 }
111
112 /**
113 * Return presets ordered by priority
114 *
115 * @return array<PresetInterface>
116 * @throws Exception
117 */
118 public function getPresetsOrderedByPriority() {
119 if (empty($this->presetInstances)) {
120 throw new Exception(
121 'Presets not initialized',
122 1378645155
123 );
124 }
125 $orderedPresets = array();
126 foreach ($this->presetInstances as $presetInstance) {
127 /** @var PresetInterface $presetInstance */
128 $orderedPresets[$presetInstance->getPriority()] = $presetInstance;
129 }
130 krsort($orderedPresets, SORT_NUMERIC);
131 return $orderedPresets;
132 }
133
134 /**
135 * Return name of feature
136 *
137 * @return string Name of feature
138 */
139 public function getName() {
140 return $this->name;
141 }
142 }