[ Index ] |
PHP Cross Reference of phpBB-3.1.12-deutsch |
[Summary view] [Print] [Text view]
1 <?php 2 3 /* 4 * This file is part of the Symfony package. 5 * 6 * (c) Fabien Potencier <fabien@symfony.com> 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12 namespace Symfony\Component\EventDispatcher; 13 14 /** 15 * The EventDispatcherInterface is the central point of Symfony's event listener system. 16 * 17 * Listeners are registered on the manager and events are dispatched through the 18 * manager. 19 * 20 * @author Guilherme Blanco <guilhermeblanco@hotmail.com> 21 * @author Jonathan Wage <jonwage@gmail.com> 22 * @author Roman Borschel <roman@code-factory.org> 23 * @author Bernhard Schussek <bschussek@gmail.com> 24 * @author Fabien Potencier <fabien@symfony.com> 25 * @author Jordi Boggiano <j.boggiano@seld.be> 26 * @author Jordan Alliot <jordan.alliot@gmail.com> 27 */ 28 class EventDispatcher implements EventDispatcherInterface 29 { 30 private $listeners = array(); 31 private $sorted = array(); 32 33 /** 34 * {@inheritdoc} 35 */ 36 public function dispatch($eventName, Event $event = null) 37 { 38 if (null === $event) { 39 $event = new Event(); 40 } 41 42 $event->setDispatcher($this); 43 $event->setName($eventName); 44 45 if ($listeners = $this->getListeners($eventName)) { 46 $this->doDispatch($listeners, $eventName, $event); 47 } 48 49 return $event; 50 } 51 52 /** 53 * {@inheritdoc} 54 */ 55 public function getListeners($eventName = null) 56 { 57 if (null !== $eventName) { 58 if (!isset($this->listeners[$eventName])) { 59 return array(); 60 } 61 62 if (!isset($this->sorted[$eventName])) { 63 $this->sortListeners($eventName); 64 } 65 66 return $this->sorted[$eventName]; 67 } 68 69 foreach ($this->listeners as $eventName => $eventListeners) { 70 if (!isset($this->sorted[$eventName])) { 71 $this->sortListeners($eventName); 72 } 73 } 74 75 return array_filter($this->sorted); 76 } 77 78 /** 79 * {@inheritdoc} 80 */ 81 public function hasListeners($eventName = null) 82 { 83 return (bool) count($this->getListeners($eventName)); 84 } 85 86 /** 87 * {@inheritdoc} 88 */ 89 public function addListener($eventName, $listener, $priority = 0) 90 { 91 $this->listeners[$eventName][$priority][] = $listener; 92 unset($this->sorted[$eventName]); 93 } 94 95 /** 96 * {@inheritdoc} 97 */ 98 public function removeListener($eventName, $listener) 99 { 100 if (!isset($this->listeners[$eventName])) { 101 return; 102 } 103 104 foreach ($this->listeners[$eventName] as $priority => $listeners) { 105 if (false !== ($key = array_search($listener, $listeners, true))) { 106 unset($this->listeners[$eventName][$priority][$key], $this->sorted[$eventName]); 107 } 108 } 109 } 110 111 /** 112 * {@inheritdoc} 113 */ 114 public function addSubscriber(EventSubscriberInterface $subscriber) 115 { 116 foreach ($subscriber->getSubscribedEvents() as $eventName => $params) { 117 if (is_string($params)) { 118 $this->addListener($eventName, array($subscriber, $params)); 119 } elseif (is_string($params[0])) { 120 $this->addListener($eventName, array($subscriber, $params[0]), isset($params[1]) ? $params[1] : 0); 121 } else { 122 foreach ($params as $listener) { 123 $this->addListener($eventName, array($subscriber, $listener[0]), isset($listener[1]) ? $listener[1] : 0); 124 } 125 } 126 } 127 } 128 129 /** 130 * {@inheritdoc} 131 */ 132 public function removeSubscriber(EventSubscriberInterface $subscriber) 133 { 134 foreach ($subscriber->getSubscribedEvents() as $eventName => $params) { 135 if (is_array($params) && is_array($params[0])) { 136 foreach ($params as $listener) { 137 $this->removeListener($eventName, array($subscriber, $listener[0])); 138 } 139 } else { 140 $this->removeListener($eventName, array($subscriber, is_string($params) ? $params : $params[0])); 141 } 142 } 143 } 144 145 /** 146 * Triggers the listeners of an event. 147 * 148 * This method can be overridden to add functionality that is executed 149 * for each listener. 150 * 151 * @param array[callback] $listeners The event listeners. 152 * @param string $eventName The name of the event to dispatch. 153 * @param Event $event The event object to pass to the event handlers/listeners. 154 */ 155 protected function doDispatch($listeners, $eventName, Event $event) 156 { 157 foreach ($listeners as $listener) { 158 if ($event->isPropagationStopped()) { 159 break; 160 } 161 call_user_func($listener, $event); 162 } 163 } 164 165 /** 166 * Sorts the internal list of listeners for the given event by priority. 167 * 168 * @param string $eventName The name of the event. 169 */ 170 private function sortListeners($eventName) 171 { 172 $this->sorted[$eventName] = array(); 173 174 krsort($this->listeners[$eventName]); 175 $this->sorted[$eventName] = call_user_func_array('array_merge', $this->listeners[$eventName]); 176 } 177 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Jan 11 00:25:41 2018 | Cross-referenced by PHPXref 0.7.1 |