TYPO3 CMS  TYPO3_8-7
AbstractFeature.php
Go to the documentation of this file.
1 <?php
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 
18 
22 abstract class AbstractFeature
23 {
27  protected $name = '';
28 
32  protected $presetRegistry = [];
33 
37  protected $presetInstances = [];
38 
42  protected $postValues = [];
43 
50  public function initializePresets(array $postValues)
51  {
52  // Give feature sub array of $POST values to preset and set to own property
53  $featurePostValues = [];
54  if (!empty($postValues[$this->name])) {
55  $featurePostValues = $postValues[$this->name];
56  }
57  $this->postValues = $featurePostValues;
58 
59  $isNonCustomPresetActive = false;
60  $customPresetFound = false;
61  foreach ($this->presetRegistry as $presetClass) {
63  $presetInstance = GeneralUtility::makeInstance($presetClass);
64  if (!($presetInstance instanceof PresetInterface)) {
65  throw new Exception(
66  'Preset ' . $presetClass . ' does not implement PresetInterface',
67  1378644821
68  );
69  }
70 
71  $presetInstance->setPostValues($featurePostValues);
72 
73  // Custom preset is set active if no preset before is active
74  if ($presetInstance->isActive()) {
75  $isNonCustomPresetActive = true;
76  }
77  if ($presetInstance instanceof CustomPresetInterface
78  && !$isNonCustomPresetActive
79  ) {
80  // Throw Exception if two custom presets are registered
81  if ($customPresetFound === true) {
82  throw new Exception(
83  'Preset ' . $presetClass . ' implements CustomPresetInterface, but another'
84  . ' custom preset is already registered',
85  1378645039
86  );
87  }
88 
90  $presetInstance->setActive();
91  $customPresetFound = true;
92  }
93 
94  $this->presetInstances[] = $presetInstance;
95  }
96  }
97 
104  public function getPresetsOrderedByPriority()
105  {
106  if (empty($this->presetInstances)) {
107  throw new Exception(
108  'Presets not initialized',
109  1378645155
110  );
111  }
112  $orderedPresets = [];
113  foreach ($this->presetInstances as $presetInstance) {
115  $orderedPresets[$presetInstance->getPriority()] = $presetInstance;
116  }
117  krsort($orderedPresets, SORT_NUMERIC);
118  return $orderedPresets;
119  }
120 
126  public function getName()
127  {
128  return $this->name;
129  }
130 }
static makeInstance($className,... $constructorArguments)