TYPO3 CMS  TYPO3_6-2
AbstractFeature.php
Go to the documentation of this file.
1 <?php
3 
20 abstract class AbstractFeature {
21 
26  protected $objectManager = NULL;
27 
31  protected $name = '';
32 
36  protected $presetRegistry = array();
37 
41  protected $presetInstances = array();
42 
46  protected $postValues = array();
47 
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) {
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 
94  $presetInstance->setActive();
95  $customPresetFound = TRUE;
96  }
97 
98  $this->presetInstances[] = $presetInstance;
99  }
100  }
101 
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) {
118  $orderedPresets[$presetInstance->getPriority()] = $presetInstance;
119  }
120  krsort($orderedPresets, SORT_NUMERIC);
121  return $orderedPresets;
122  }
123 
129  public function getName() {
130  return $this->name;
131  }
132 }