TYPO3 CMS  TYPO3_7-6
Dispatcher.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 
25 {
29  protected $isInitialized = false;
30 
34  protected $objectManager;
35 
43  protected $slots = [];
44 
55  public function initializeObject()
56  {
57  if (!$this->isInitialized) {
58  $this->objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
59  $this->isInitialized = true;
60  }
61  }
62 
76  public function connect($signalClassName, $signalName, $slotClassNameOrObject, $slotMethodName = '', $passSignalInformation = true)
77  {
78  $class = null;
79  $object = null;
80  if (is_object($slotClassNameOrObject)) {
81  $object = $slotClassNameOrObject;
82  $method = $slotClassNameOrObject instanceof \Closure ? '__invoke' : $slotMethodName;
83  } else {
84  if ($slotMethodName === '') {
85  throw new \InvalidArgumentException('The slot method name must not be empty (except for closures).', 1229531659);
86  }
87  $class = $slotClassNameOrObject;
88  $method = $slotMethodName;
89  }
90  $slot = [
91  'class' => $class,
92  'method' => $method,
93  'object' => $object,
94  'passSignalInformation' => $passSignalInformation === true
95  ];
96  // The in_array() comparision needs to be strict to avoid potential issues
97  // with complex objects being registered as slot.
98  if (!is_array($this->slots[$signalClassName][$signalName]) || !in_array($slot, $this->slots[$signalClassName][$signalName], true)) {
99  $this->slots[$signalClassName][$signalName][] = $slot;
100  }
101  }
102 
114  public function dispatch($signalClassName, $signalName, array $signalArguments = [])
115  {
116  $this->initializeObject();
117  if (!isset($this->slots[$signalClassName][$signalName])) {
118  return $signalArguments;
119  }
120  foreach ($this->slots[$signalClassName][$signalName] as $slotInformation) {
121  if (isset($slotInformation['object'])) {
122  $object = $slotInformation['object'];
123  } else {
124  if (!isset($this->objectManager)) {
125  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);
126  }
127  if (!$this->objectManager->isRegistered($slotInformation['class'])) {
128  throw new Exception\InvalidSlotException('The given class "' . $slotInformation['class'] . '" is not a registered object.', 1245673367);
129  }
130  $object = $this->objectManager->get($slotInformation['class']);
131  }
132 
133  if (!method_exists($object, $slotInformation['method'])) {
134  throw new Exception\InvalidSlotException('The slot method ' . get_class($object) . '->' . $slotInformation['method'] . '() does not exist.', 1245673368);
135  }
136 
137  $preparedSlotArguments = $signalArguments;
138  if ($slotInformation['passSignalInformation'] === true) {
139  $preparedSlotArguments[] = $signalClassName . '::' . $signalName;
140  }
141 
142  $slotReturn = call_user_func_array([$object, $slotInformation['method']], $preparedSlotArguments);
143 
144  if ($slotReturn) {
145  if (!is_array($slotReturn)) {
146  throw new Exception\InvalidSlotReturnException('The slot method ' . get_class($object) . '->' . $slotInformation['method'] . '()\'s return value is of an not allowed type ('
147  . gettype($slotReturn) . ').', 1376683067);
148  } elseif (count($slotReturn) !== count($signalArguments)) {
149  throw new Exception\InvalidSlotReturnException('The slot method ' . get_class($object) . '->' . $slotInformation['method'] . '() returned a different number ('
150  . count($slotReturn) . ') of arguments, than it received (' . count($signalArguments) . ').', 1376683066);
151  } else {
152  $signalArguments = $slotReturn;
153  }
154  }
155  }
156 
157  return $signalArguments;
158  }
159 
168  public function getSlots($signalClassName, $signalName)
169  {
170  return isset($this->slots[$signalClassName][$signalName]) ? $this->slots[$signalClassName][$signalName] : [];
171  }
172 }
connect($signalClassName, $signalName, $slotClassNameOrObject, $slotMethodName='', $passSignalInformation=true)
Definition: Dispatcher.php:76
getSlots($signalClassName, $signalName)
Definition: Dispatcher.php:168
dispatch($signalClassName, $signalName, array $signalArguments=[])
Definition: Dispatcher.php:114