TYPO3 CMS  TYPO3_6-2
Dispatcher.php
Go to the documentation of this file.
1 <?php
3 
24 
28  protected $isInitialized = FALSE;
29 
33  protected $objectManager;
34 
42  protected $slots = array();
43 
54  public function initializeObject() {
55  if (!$this->isInitialized) {
56  $this->objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
57  $this->isInitialized = TRUE;
58  }
59  }
60 
74  public function connect($signalClassName, $signalName, $slotClassNameOrObject, $slotMethodName = '', $passSignalInformation = TRUE) {
75  $class = NULL;
76  $object = NULL;
77  if (is_object($slotClassNameOrObject)) {
78  $object = $slotClassNameOrObject;
79  $method = $slotClassNameOrObject instanceof \Closure ? '__invoke' : $slotMethodName;
80  } else {
81  if ($slotMethodName === '') {
82  throw new \InvalidArgumentException('The slot method name must not be empty (except for closures).', 1229531659);
83  }
84  $class = $slotClassNameOrObject;
85  $method = $slotMethodName;
86  }
87  $slot = array(
88  'class' => $class,
89  'method' => $method,
90  'object' => $object,
91  'passSignalInformation' => $passSignalInformation === TRUE
92  );
93  if (!is_array($this->slots[$signalClassName][$signalName]) || !in_array($slot, $this->slots[$signalClassName][$signalName])) {
94  $this->slots[$signalClassName][$signalName][] = $slot;
95  }
96  }
97 
109  public function dispatch($signalClassName, $signalName, array $signalArguments = array()) {
110  $this->initializeObject();
111  if (!isset($this->slots[$signalClassName][$signalName])) {
112  return $signalArguments;
113  }
114  foreach ($this->slots[$signalClassName][$signalName] as $slotInformation) {
115  if (isset($slotInformation['object'])) {
116  $object = $slotInformation['object'];
117  } else {
118  if (!isset($this->objectManager)) {
119  throw new Exception\InvalidSlotException(sprintf('Cannot dispatch %s::%s to class %s. The object manager is not yet available in the Signal Slot Dispatcher and therefore it cannot dispatch classes.', $signalClassName, $signalName, $slotInformation['class']), 1298113624);
120  }
121  if (!$this->objectManager->isRegistered($slotInformation['class'])) {
122  throw new Exception\InvalidSlotException('The given class "' . $slotInformation['class'] . '" is not a registered object.', 1245673367);
123  }
124  $object = $this->objectManager->get($slotInformation['class']);
125  }
126 
127  if (!method_exists($object, $slotInformation['method'])) {
128  throw new Exception\InvalidSlotException('The slot method ' . get_class($object) . '->' . $slotInformation['method'] . '() does not exist.', 1245673368);
129  }
130 
131  $preparedSlotArguments = $signalArguments;
132  if ($slotInformation['passSignalInformation'] === TRUE) {
133  $preparedSlotArguments[] = $signalClassName . '::' . $signalName;
134  }
135 
136  $slotReturn = call_user_func_array(array($object, $slotInformation['method']), $preparedSlotArguments);
137 
138  if ($slotReturn) {
139  if (!is_array($slotReturn)) {
140  throw new Exception\InvalidSlotReturnException('The slot method ' . get_class($object) . '->' . $slotInformation['method'] . '()\'s return value is of an not allowed type ('
141  . gettype($slotReturn) . ').', 1376683067);
142  } elseif (count($slotReturn) !== count($signalArguments)) {
143  throw new Exception\InvalidSlotReturnException('The slot method ' . get_class($object) . '->' . $slotInformation['method'] . '() returned a different number ('
144  . count($slotReturn) . ') of arguments, than it recieved (' . count($signalArguments) . ').', 1376683066);
145  } else {
146  $signalArguments = $slotReturn;
147  }
148  }
149  }
150 
151  return $signalArguments;
152  }
153 
162  public function getSlots($signalClassName, $signalName) {
163  return isset($this->slots[$signalClassName][$signalName]) ? $this->slots[$signalClassName][$signalName] : array();
164  }
165 }
getSlots($signalClassName, $signalName)
Definition: Dispatcher.php:162
dispatch($signalClassName, $signalName, array $signalArguments=array())
Definition: Dispatcher.php:109
connect($signalClassName, $signalName, $slotClassNameOrObject, $slotMethodName='', $passSignalInformation=TRUE)
Definition: Dispatcher.php:74