TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
extbase/Classes/SignalSlot/Dispatcher.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Extbase\SignalSlot;
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  if (!is_array($this->slots[$signalClassName][$signalName]) || !in_array($slot, $this->slots[$signalClassName][$signalName])) {
97  $this->slots[$signalClassName][$signalName][] = $slot;
98  }
99  }
100 
112  public function dispatch($signalClassName, $signalName, array $signalArguments = [])
113  {
114  $this->initializeObject();
115  if (!isset($this->slots[$signalClassName][$signalName])) {
116  return $signalArguments;
117  }
118  foreach ($this->slots[$signalClassName][$signalName] as $slotInformation) {
119  if (isset($slotInformation['object'])) {
120  $object = $slotInformation['object'];
121  } else {
122  if (!isset($this->objectManager)) {
123  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);
124  }
125  if (!$this->objectManager->isRegistered($slotInformation['class'])) {
126  throw new Exception\InvalidSlotException('The given class "' . $slotInformation['class'] . '" is not a registered object.', 1245673367);
127  }
128  $object = $this->objectManager->get($slotInformation['class']);
129  }
130 
131  if (!method_exists($object, $slotInformation['method'])) {
132  throw new Exception\InvalidSlotException('The slot method ' . get_class($object) . '->' . $slotInformation['method'] . '() does not exist.', 1245673368);
133  }
134 
135  $preparedSlotArguments = $signalArguments;
136  if ($slotInformation['passSignalInformation'] === true) {
137  $preparedSlotArguments[] = $signalClassName . '::' . $signalName;
138  }
139 
140  $slotReturn = call_user_func_array([$object, $slotInformation['method']], $preparedSlotArguments);
141 
142  if ($slotReturn) {
143  if (!is_array($slotReturn)) {
144  throw new Exception\InvalidSlotReturnException('The slot method ' . get_class($object) . '->' . $slotInformation['method'] . '()\'s return value is of an not allowed type ('
145  . gettype($slotReturn) . ').', 1376683067);
146  } elseif (count($slotReturn) !== count($signalArguments)) {
147  throw new Exception\InvalidSlotReturnException('The slot method ' . get_class($object) . '->' . $slotInformation['method'] . '() returned a different number ('
148  . count($slotReturn) . ') of arguments, than it received (' . count($signalArguments) . ').', 1376683066);
149  } else {
150  $signalArguments = $slotReturn;
151  }
152  }
153  }
154 
155  return $signalArguments;
156  }
157 
166  public function getSlots($signalClassName, $signalName)
167  {
168  return isset($this->slots[$signalClassName][$signalName]) ? $this->slots[$signalClassName][$signalName] : [];
169  }
170 }
dispatch($signalClassName, $signalName, array $signalArguments=[])
connect($signalClassName, $signalName, $slotClassNameOrObject, $slotMethodName= '', $passSignalInformation=true)
static makeInstance($className,...$constructorArguments)