TYPO3 CMS  TYPO3_7-6
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 
20 abstract class AbstractFeature
21 {
25  protected $objectManager = null;
26 
30  protected $name = '';
31 
35  protected $presetRegistry = [];
36 
40  protected $presetInstances = [];
41 
45  protected $postValues = [];
46 
50  public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager)
51  {
52  $this->objectManager = $objectManager;
53  }
54 
62  public function initializePresets(array $postValues)
63  {
64  // Give feature sub array of $POST values to preset and set to own property
65  $featurePostValues = [];
66  if (!empty($postValues[$this->name])) {
67  $featurePostValues = $postValues[$this->name];
68  }
69  $this->postValues = $featurePostValues;
70 
71  $isNonCustomPresetActive = false;
72  $customPresetFound = false;
73  foreach ($this->presetRegistry as $presetClass) {
75  $presetInstance = $this->objectManager->get($presetClass);
76  if (!($presetInstance instanceof PresetInterface)) {
77  throw new Exception(
78  'Preset ' . $presetClass . ' does not implement PresetInterface',
79  1378644821
80  );
81  }
82 
83  $presetInstance->setPostValues($featurePostValues);
84 
85  // Custom preset is set active if no preset before is active
86  if ($presetInstance->isActive()) {
87  $isNonCustomPresetActive = true;
88  }
89  if ($presetInstance instanceof CustomPresetInterface
90  && !$isNonCustomPresetActive
91  ) {
92  // Throw Exception if two custom presets are registered
93  if ($customPresetFound === true) {
94  throw new Exception(
95  'Preset ' . $presetClass . ' implements CustomPresetInterface, but another'
96  . ' custom preset is already registered',
97  1378645039
98  );
99  }
100 
102  $presetInstance->setActive();
103  $customPresetFound = true;
104  }
105 
106  $this->presetInstances[] = $presetInstance;
107  }
108  }
109 
116  public function getPresetsOrderedByPriority()
117  {
118  if (empty($this->presetInstances)) {
119  throw new Exception(
120  'Presets not initialized',
121  1378645155
122  );
123  }
124  $orderedPresets = [];
125  foreach ($this->presetInstances as $presetInstance) {
127  $orderedPresets[$presetInstance->getPriority()] = $presetInstance;
128  }
129  krsort($orderedPresets, SORT_NUMERIC);
130  return $orderedPresets;
131  }
132 
138  public function getName()
139  {
140  return $this->name;
141  }
142 }
injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager)