EventArgs.php
2 years ago
EventManager.php
2 years ago
EventSubscriber.php
2 years ago
index.php
2 years ago
EventManager.php
62 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Doctrine\Common; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Doctrine\Deprecations\Deprecation; |
| 5 | use function spl_object_hash; |
| 6 | class EventManager |
| 7 | { |
| 8 | private $listeners = []; |
| 9 | public function dispatchEvent($eventName, ?EventArgs $eventArgs = null) |
| 10 | { |
| 11 | if (!isset($this->listeners[$eventName])) { |
| 12 | return; |
| 13 | } |
| 14 | $eventArgs = $eventArgs ?? EventArgs::getEmptyInstance(); |
| 15 | foreach ($this->listeners[$eventName] as $listener) { |
| 16 | $listener->{$eventName}($eventArgs); |
| 17 | } |
| 18 | } |
| 19 | public function getListeners($event = null) |
| 20 | { |
| 21 | if ($event === null) { |
| 22 | Deprecation::trigger('doctrine/event-manager', 'https://github.com/doctrine/event-manager/pull/50', 'Calling %s without an event name is deprecated. Call getAllListeners() instead.', __METHOD__); |
| 23 | return $this->getAllListeners(); |
| 24 | } |
| 25 | return $this->listeners[$event] ?? []; |
| 26 | } |
| 27 | public function getAllListeners() : array |
| 28 | { |
| 29 | return $this->listeners; |
| 30 | } |
| 31 | public function hasListeners($event) |
| 32 | { |
| 33 | return !empty($this->listeners[$event]); |
| 34 | } |
| 35 | public function addEventListener($events, $listener) |
| 36 | { |
| 37 | // Picks the hash code related to that listener |
| 38 | $hash = spl_object_hash($listener); |
| 39 | foreach ((array) $events as $event) { |
| 40 | // Overrides listener if a previous one was associated already |
| 41 | // Prevents duplicate listeners on same event (same instance only) |
| 42 | $this->listeners[$event][$hash] = $listener; |
| 43 | } |
| 44 | } |
| 45 | public function removeEventListener($events, $listener) |
| 46 | { |
| 47 | // Picks the hash code related to that listener |
| 48 | $hash = spl_object_hash($listener); |
| 49 | foreach ((array) $events as $event) { |
| 50 | unset($this->listeners[$event][$hash]); |
| 51 | } |
| 52 | } |
| 53 | public function addEventSubscriber(EventSubscriber $subscriber) |
| 54 | { |
| 55 | $this->addEventListener($subscriber->getSubscribedEvents(), $subscriber); |
| 56 | } |
| 57 | public function removeEventSubscriber(EventSubscriber $subscriber) |
| 58 | { |
| 59 | $this->removeEventListener($subscriber->getSubscribedEvents(), $subscriber); |
| 60 | } |
| 61 | } |
| 62 |