1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @copyright 2019 Christoph Wurst <[email protected]> |
7
|
|
|
* |
8
|
|
|
* @author 2019 Christoph Wurst <[email protected]> |
9
|
|
|
* |
10
|
|
|
* @license GNU AGPL version 3 or any later version |
11
|
|
|
* |
12
|
|
|
* This program is free software: you can redistribute it and/or modify |
13
|
|
|
* it under the terms of the GNU Affero General Public License as |
14
|
|
|
* published by the Free Software Foundation, either version 3 of the |
15
|
|
|
* License, or (at your option) any later version. |
16
|
|
|
* |
17
|
|
|
* This program is distributed in the hope that it will be useful, |
18
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
19
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20
|
|
|
* GNU Affero General Public License for more details. |
21
|
|
|
* |
22
|
|
|
* You should have received a copy of the GNU Affero General Public License |
23
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
namespace OC\EventDispatcher; |
27
|
|
|
|
28
|
|
|
use function is_callable; |
29
|
|
|
use OCP\EventDispatcher\Event; |
30
|
|
|
use Symfony\Component\EventDispatcher\Event as SymfonyEvent; |
31
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
32
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
33
|
|
|
|
34
|
|
|
class SymfonyAdapter implements EventDispatcherInterface { |
35
|
|
|
|
36
|
|
|
/** @var EventDispatcher */ |
37
|
|
|
private $eventDispatcher; |
38
|
|
|
|
39
|
|
|
public function __construct(EventDispatcher $eventDispatcher) { |
40
|
|
|
$this->eventDispatcher = $eventDispatcher; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Dispatches an event to all registered listeners. |
45
|
|
|
* |
46
|
|
|
* @param string $eventName The name of the event to dispatch. The name of |
47
|
|
|
* the event is the name of the method that is |
48
|
|
|
* invoked on listeners. |
49
|
|
|
* @param SymfonyEvent|null $event The event to pass to the event handlers/listeners |
50
|
|
|
* If not supplied, an empty Event instance is created |
51
|
|
|
* |
52
|
|
|
* @return SymfonyEvent |
53
|
|
|
*/ |
54
|
|
|
public function dispatch($eventName, SymfonyEvent $event = null) { |
55
|
|
|
if ($event instanceof Event) { |
56
|
|
|
$this->eventDispatcher->dispatch($eventName, $event); |
57
|
|
|
} else { |
58
|
|
|
// Legacy event |
59
|
|
|
$this->eventDispatcher->getSymfonyDispatcher()->dispatch($eventName, $event); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Adds an event listener that listens on the specified events. |
65
|
|
|
* |
66
|
|
|
* @param string $eventName The event to listen on |
67
|
|
|
* @param callable $listener The listener |
68
|
|
|
* @param int $priority The higher this value, the earlier an event |
69
|
|
|
* listener will be triggered in the chain (defaults to 0) |
70
|
|
|
*/ |
71
|
|
|
public function addListener($eventName, $listener, $priority = 0) { |
72
|
|
|
if (is_callable($listener)) { |
73
|
|
|
$this->eventDispatcher->addListener($eventName, $listener, $priority); |
74
|
|
|
} else { |
75
|
|
|
// Legacy listener |
76
|
|
|
$this->eventDispatcher->getSymfonyDispatcher()->addListener($eventName, $listener, $priority); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Adds an event subscriber. |
82
|
|
|
* |
83
|
|
|
* The subscriber is asked for all the events it is |
84
|
|
|
* interested in and added as a listener for these events. |
85
|
|
|
*/ |
86
|
|
|
public function addSubscriber(EventSubscriberInterface $subscriber) { |
87
|
|
|
$this->eventDispatcher->getSymfonyDispatcher()->addSubscriber($subscriber); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Removes an event listener from the specified events. |
92
|
|
|
* |
93
|
|
|
* @param string $eventName The event to remove a listener from |
94
|
|
|
* @param callable $listener The listener to remove |
95
|
|
|
*/ |
96
|
|
|
public function removeListener($eventName, $listener) { |
97
|
|
|
$this->eventDispatcher->getSymfonyDispatcher()->removeListener($eventName, $listener); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function removeSubscriber(EventSubscriberInterface $subscriber) { |
101
|
|
|
$this->eventDispatcher->getSymfonyDispatcher()->removeSubscriber($subscriber); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Gets the listeners of a specific event or all listeners sorted by descending priority. |
106
|
|
|
* |
107
|
|
|
* @param string|null $eventName The name of the event |
108
|
|
|
* |
109
|
|
|
* @return array The event listeners for the specified event, or all event listeners by event name |
110
|
|
|
*/ |
111
|
|
|
public function getListeners($eventName = null) { |
112
|
|
|
return $this->eventDispatcher->getSymfonyDispatcher()->getListeners($eventName); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Gets the listener priority for a specific event. |
117
|
|
|
* |
118
|
|
|
* Returns null if the event or the listener does not exist. |
119
|
|
|
* |
120
|
|
|
* @param string $eventName The name of the event |
121
|
|
|
* @param callable $listener The listener |
122
|
|
|
* |
123
|
|
|
* @return int|null The event listener priority |
124
|
|
|
*/ |
125
|
|
|
public function getListenerPriority($eventName, $listener) { |
126
|
|
|
return $this->eventDispatcher->getSymfonyDispatcher()->getListenerPriority($eventName, $listener); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Checks whether an event has any registered listeners. |
131
|
|
|
* |
132
|
|
|
* @param string|null $eventName The name of the event |
133
|
|
|
* |
134
|
|
|
* @return bool true if the specified event has any listeners, false otherwise |
135
|
|
|
*/ |
136
|
|
|
public function hasListeners($eventName = null) { |
137
|
|
|
return $this->eventDispatcher->getSymfonyDispatcher()->hasListeners($eventName); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
} |
141
|
|
|
|