[ Index ] |
PHP Cross Reference of phpBB-3.2.11-deutsch |
[Summary view] [Print] [Text view]
1 <?php 2 namespace GuzzleHttp\Event; 3 4 /** 5 * Guzzle event emitter. 6 * 7 * Some of this class is based on the Symfony EventDispatcher component, which 8 * ships with the following license: 9 * 10 * This file is part of the Symfony package. 11 * 12 * (c) Fabien Potencier <fabien@symfony.com> 13 * 14 * For the full copyright and license information, please view the LICENSE 15 * file that was distributed with this source code. 16 * 17 * @link https://github.com/symfony/symfony/tree/master/src/Symfony/Component/EventDispatcher 18 */ 19 class Emitter implements EmitterInterface 20 { 21 /** @var array */ 22 private $listeners = []; 23 24 /** @var array */ 25 private $sorted = []; 26 27 public function on($eventName, callable $listener, $priority = 0) 28 { 29 if ($priority === 'first') { 30 $priority = isset($this->listeners[$eventName]) 31 ? max(array_keys($this->listeners[$eventName])) + 1 32 : 1; 33 } elseif ($priority === 'last') { 34 $priority = isset($this->listeners[$eventName]) 35 ? min(array_keys($this->listeners[$eventName])) - 1 36 : -1; 37 } 38 39 $this->listeners[$eventName][$priority][] = $listener; 40 unset($this->sorted[$eventName]); 41 } 42 43 public function once($eventName, callable $listener, $priority = 0) 44 { 45 $onceListener = function ( 46 EventInterface $event 47 ) use (&$onceListener, $eventName, $listener, $priority) { 48 $this->removeListener($eventName, $onceListener); 49 $listener($event, $eventName); 50 }; 51 52 $this->on($eventName, $onceListener, $priority); 53 } 54 55 public function removeListener($eventName, callable $listener) 56 { 57 if (empty($this->listeners[$eventName])) { 58 return; 59 } 60 61 foreach ($this->listeners[$eventName] as $priority => $listeners) { 62 if (false !== ($key = array_search($listener, $listeners, true))) { 63 unset( 64 $this->listeners[$eventName][$priority][$key], 65 $this->sorted[$eventName] 66 ); 67 } 68 } 69 } 70 71 public function listeners($eventName = null) 72 { 73 // Return all events in a sorted priority order 74 if ($eventName === null) { 75 foreach (array_keys($this->listeners) as $eventName) { 76 if (empty($this->sorted[$eventName])) { 77 $this->listeners($eventName); 78 } 79 } 80 return $this->sorted; 81 } 82 83 // Return the listeners for a specific event, sorted in priority order 84 if (empty($this->sorted[$eventName])) { 85 $this->sorted[$eventName] = []; 86 if (isset($this->listeners[$eventName])) { 87 krsort($this->listeners[$eventName], SORT_NUMERIC); 88 foreach ($this->listeners[$eventName] as $listeners) { 89 foreach ($listeners as $listener) { 90 $this->sorted[$eventName][] = $listener; 91 } 92 } 93 } 94 } 95 96 return $this->sorted[$eventName]; 97 } 98 99 public function hasListeners($eventName) 100 { 101 return !empty($this->listeners[$eventName]); 102 } 103 104 public function emit($eventName, EventInterface $event) 105 { 106 if (isset($this->listeners[$eventName])) { 107 foreach ($this->listeners($eventName) as $listener) { 108 $listener($event, $eventName); 109 if ($event->isPropagationStopped()) { 110 break; 111 } 112 } 113 } 114 115 return $event; 116 } 117 118 public function attach(SubscriberInterface $subscriber) 119 { 120 foreach ($subscriber->getEvents() as $eventName => $listeners) { 121 if (is_array($listeners[0])) { 122 foreach ($listeners as $listener) { 123 $this->on( 124 $eventName, 125 [$subscriber, $listener[0]], 126 isset($listener[1]) ? $listener[1] : 0 127 ); 128 } 129 } else { 130 $this->on( 131 $eventName, 132 [$subscriber, $listeners[0]], 133 isset($listeners[1]) ? $listeners[1] : 0 134 ); 135 } 136 } 137 } 138 139 public function detach(SubscriberInterface $subscriber) 140 { 141 foreach ($subscriber->getEvents() as $eventName => $listener) { 142 $this->removeListener($eventName, [$subscriber, $listener[0]]); 143 } 144 } 145 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Nov 11 20:33:01 2020 | Cross-referenced by PHPXref 0.7.1 |