summaryrefslogtreecommitdiff
path: root/vendor/doctrine/event-manager/src
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/doctrine/event-manager/src')
-rw-r--r--vendor/doctrine/event-manager/src/EventArgs.php37
-rw-r--r--vendor/doctrine/event-manager/src/EventManager.php129
-rw-r--r--vendor/doctrine/event-manager/src/EventSubscriber.php21
3 files changed, 187 insertions, 0 deletions
diff --git a/vendor/doctrine/event-manager/src/EventArgs.php b/vendor/doctrine/event-manager/src/EventArgs.php
new file mode 100644
index 0000000..eea3d8a
--- /dev/null
+++ b/vendor/doctrine/event-manager/src/EventArgs.php
@@ -0,0 +1,37 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Doctrine\Common;
6
7/**
8 * EventArgs is the base class for classes containing event data.
9 *
10 * This class contains no event data. It is used by events that do not pass state
11 * information to an event handler when an event is raised. The single empty EventArgs
12 * instance can be obtained through {@link getEmptyInstance}.
13 */
14class EventArgs
15{
16 /**
17 * Single instance of EventArgs.
18 */
19 private static EventArgs|null $emptyEventArgsInstance = null;
20
21 /**
22 * Gets the single, empty and immutable EventArgs instance.
23 *
24 * This instance will be used when events are dispatched without any parameter,
25 * like this: EventManager::dispatchEvent('eventname');
26 *
27 * The benefit from this is that only one empty instance is instantiated and shared
28 * (otherwise there would be instances for every dispatched in the abovementioned form).
29 *
30 * @link https://msdn.microsoft.com/en-us/library/system.eventargs.aspx
31 * @see EventManager::dispatchEvent
32 */
33 public static function getEmptyInstance(): EventArgs
34 {
35 return self::$emptyEventArgsInstance ??= new EventArgs();
36 }
37}
diff --git a/vendor/doctrine/event-manager/src/EventManager.php b/vendor/doctrine/event-manager/src/EventManager.php
new file mode 100644
index 0000000..86f5e45
--- /dev/null
+++ b/vendor/doctrine/event-manager/src/EventManager.php
@@ -0,0 +1,129 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Doctrine\Common;
6
7use 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 */
14class 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}
diff --git a/vendor/doctrine/event-manager/src/EventSubscriber.php b/vendor/doctrine/event-manager/src/EventSubscriber.php
new file mode 100644
index 0000000..89cef55
--- /dev/null
+++ b/vendor/doctrine/event-manager/src/EventSubscriber.php
@@ -0,0 +1,21 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Doctrine\Common;
6
7/**
8 * An EventSubscriber knows what events it is interested in.
9 * If an EventSubscriber is added to an EventManager, the manager invokes
10 * {@link getSubscribedEvents} and registers the subscriber as a listener for all
11 * returned events.
12 */
13interface EventSubscriber
14{
15 /**
16 * Returns an array of events this subscriber wants to listen to.
17 *
18 * @return string[]
19 */
20 public function getSubscribedEvents();
21}