Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace Doctrine\Common;
6
 
7
use function spl_object_hash;
8
 
9
/**
10
 * The EventManager is the central point of Doctrine's event listener system.
11
 * Listeners are registered on the manager and events are dispatched through the
12
 * manager.
13
 */
14
class EventManager
15
{
16
    /**
17
     * Map of registered listeners.
18
     * <event> => <listeners>
19
     *
20
     * @var array<string, object[]>
21
     */
22
    private array $listeners = [];
23
 
24
    /**
25
     * Dispatches an event to all registered listeners.
26
     *
27
     * @param string         $eventName The name of the event to dispatch. The name of the event is
28
     *                                  the name of the method that is invoked on listeners.
29
     * @param EventArgs|null $eventArgs The event arguments to pass to the event handlers/listeners.
30
     *                                  If not supplied, the single empty EventArgs instance is used.
31
     */
32
    public function dispatchEvent(string $eventName, EventArgs|null $eventArgs = null): void
33
    {
34
        if (! isset($this->listeners[$eventName])) {
35
            return;
36
        }
37
 
38
        $eventArgs ??= EventArgs::getEmptyInstance();
39
 
40
        foreach ($this->listeners[$eventName] as $listener) {
41
            $listener->$eventName($eventArgs);
42
        }
43
    }
44
 
45
    /**
46
     * Gets the listeners of a specific event.
47
     *
48
     * @param string $event The name of the event.
49
     *
50
     * @return object[]
51
     */
52
    public function getListeners(string $event): array
53
    {
54
        return $this->listeners[$event] ?? [];
55
    }
56
 
57
    /**
58
     * Gets all listeners keyed by event name.
59
     *
60
     * @return array<string, object[]> The event listeners for the specified event, or all event listeners.
61
     */
62
    public function getAllListeners(): array
63
    {
64
        return $this->listeners;
65
    }
66
 
67
    /**
68
     * Checks whether an event has any registered listeners.
69
     */
70
    public function hasListeners(string $event): bool
71
    {
72
        return ! empty($this->listeners[$event]);
73
    }
74
 
75
    /**
76
     * Adds an event listener that listens on the specified events.
77
     *
78
     * @param string|string[] $events   The event(s) to listen on.
79
     * @param object          $listener The listener object.
80
     */
81
    public function addEventListener(string|array $events, object $listener): void
82
    {
83
        // Picks the hash code related to that listener
84
        $hash = spl_object_hash($listener);
85
 
86
        foreach ((array) $events as $event) {
87
            // Overrides listener if a previous one was associated already
88
            // Prevents duplicate listeners on same event (same instance only)
89
            $this->listeners[$event][$hash] = $listener;
90
        }
91
    }
92
 
93
    /**
94
     * Removes an event listener from the specified events.
95
     *
96
     * @param string|string[] $events
97
     */
98
    public function removeEventListener(string|array $events, object $listener): void
99
    {
100
        // Picks the hash code related to that listener
101
        $hash = spl_object_hash($listener);
102
 
103
        foreach ((array) $events as $event) {
104
            unset($this->listeners[$event][$hash]);
105
        }
106
    }
107
 
108
    /**
109
     * Adds an EventSubscriber.
110
     *
111
     * The subscriber is asked for all the events it is interested in and added
112
     * as a listener for these events.
113
     */
114
    public function addEventSubscriber(EventSubscriber $subscriber): void
115
    {
116
        $this->addEventListener($subscriber->getSubscribedEvents(), $subscriber);
117
    }
118
 
119
    /**
120
     * Removes an EventSubscriber.
121
     *
122
     * The subscriber is asked for all the events it is interested in and removed
123
     * as a listener for these events.
124
     */
125
    public function removeEventSubscriber(EventSubscriber $subscriber): void
126
    {
127
        $this->removeEventListener($subscriber->getSubscribedEvents(), $subscriber);
128
    }
129
}