Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SymfonyAdapter.php « EventDispatcher « private « lib - github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6cd636afc55dded55a2dc92e30de9050186b0fd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php

declare(strict_types=1);

/**
 * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
 *
 * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
 * @author Christoph Wurst <christoph@winzerhof-wurst.at>
 * @author Roeland Jago Douma <roeland@famdouma.nl>
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */
namespace OC\EventDispatcher;

use Symfony\Component\EventDispatcher\GenericEvent;
use function is_callable;
use OCP\EventDispatcher\Event;
use OCP\ILogger;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use function is_object;
use function is_string;

/**
 * @deprecated 20.0.0 use \OCP\EventDispatcher\IEventDispatcher
 */
class SymfonyAdapter implements EventDispatcherInterface {

	/** @var EventDispatcher */
	private $eventDispatcher;
	/** @var ILogger */
	private $logger;

	/**
	 * @deprecated 20.0.0
	 */
	public function __construct(EventDispatcher $eventDispatcher, ILogger $logger) {
		$this->eventDispatcher = $eventDispatcher;
		$this->logger = $logger;
	}

	private static function detectEventAndName($a, $b) {
		if (is_object($a) && (is_string($b) || $b === null)) {
			// a is the event, the other one is the optional name
			return [$a, $b];
		}
		if (is_object($b) && (is_string($a) || $a === null)) {
			// b is the event, the other one is the optional name
			return [$b, $a];
		}
		if (is_string($a) && $b === null) {
			// a is a payload-less event
			return [null, $a];
		}
		if (is_string($b) && $a === null) {
			// b is a payload-less event
			return [null, $b];
		}

		// Anything else we can't detect
		return [$a, $b];
	}

	/**
	 * Dispatches an event to all registered listeners.
	 *
	 * @param string $eventName The name of the event to dispatch. The name of
	 *                              the event is the name of the method that is
	 *                              invoked on listeners.
	 * @param Event|null $event The event to pass to the event handlers/listeners
	 *                              If not supplied, an empty Event instance is created
	 *
	 * @return object the emitted event
	 * @deprecated 20.0.0
	 */
	public function dispatch($eventName, $event = null): object {
		[$event, $eventName] = self::detectEventAndName($event, $eventName);

		// type hinting is not possible, due to usage of GenericEvent
		if ($event instanceof Event && $eventName === null) {
			$this->eventDispatcher->dispatchTyped($event);
			return $event;
		}
		if ($event instanceof Event) {
			$this->eventDispatcher->dispatch($eventName, $event);
			return $event;
		}

		if ($event instanceof GenericEvent && get_class($event) === GenericEvent::class) {
			$newEvent = new GenericEventWrapper($this->logger, $eventName, $event);
		} else {
			$newEvent = $event;

			// Legacy event
			$this->logger->info(
				'Deprecated event type for {name}: {class}',
				['name' => $eventName, 'class' => is_object($event) ? get_class($event) : 'null']
			);
		}

		// Event with no payload (object) need special handling
		if ($newEvent === null) {
			$this->eventDispatcher->getSymfonyDispatcher()->dispatch($eventName);
			return new Event();
		}

		// Flip the argument order for Symfony to prevent a trigger_error
		return $this->eventDispatcher->getSymfonyDispatcher()->dispatch($newEvent, $eventName);
	}

	/**
	 * Adds an event listener that listens on the specified events.
	 *
	 * @param string $eventName The event to listen on
	 * @param callable $listener The listener
	 * @param int $priority The higher this value, the earlier an event
	 *                            listener will be triggered in the chain (defaults to 0)
	 * @deprecated 20.0.0
	 */
	public function addListener($eventName, $listener, $priority = 0) {
		if (is_callable($listener)) {
			$this->eventDispatcher->addListener($eventName, $listener, $priority);
		} else {
			// Legacy listener
			$this->eventDispatcher->getSymfonyDispatcher()->addListener($eventName, $listener, $priority);
		}
	}

	/**
	 * Adds an event subscriber.
	 *
	 * The subscriber is asked for all the events it is
	 * interested in and added as a listener for these events.
	 * @deprecated 20.0.0
	 */
	public function addSubscriber(EventSubscriberInterface $subscriber) {
		$this->eventDispatcher->getSymfonyDispatcher()->addSubscriber($subscriber);
	}

	/**
	 * Removes an event listener from the specified events.
	 *
	 * @param string $eventName The event to remove a listener from
	 * @param callable $listener The listener to remove
	 * @deprecated 20.0.0
	 */
	public function removeListener($eventName, $listener) {
		$this->eventDispatcher->getSymfonyDispatcher()->removeListener($eventName, $listener);
	}

	/**
	 * @deprecated 20.0.0
	 */
	public function removeSubscriber(EventSubscriberInterface $subscriber) {
		$this->eventDispatcher->getSymfonyDispatcher()->removeSubscriber($subscriber);
	}

	/**
	 * Gets the listeners of a specific event or all listeners sorted by descending priority.
	 *
	 * @param string|null $eventName The name of the event
	 *
	 * @return array The event listeners for the specified event, or all event listeners by event name
	 * @deprecated 20.0.0
	 */
	public function getListeners($eventName = null) {
		return $this->eventDispatcher->getSymfonyDispatcher()->getListeners($eventName);
	}

	/**
	 * Gets the listener priority for a specific event.
	 *
	 * Returns null if the event or the listener does not exist.
	 *
	 * @param string $eventName The name of the event
	 * @param callable $listener The listener
	 *
	 * @return int|null The event listener priority
	 * @deprecated 20.0.0
	 */
	public function getListenerPriority($eventName, $listener) {
		return $this->eventDispatcher->getSymfonyDispatcher()->getListenerPriority($eventName, $listener);
	}

	/**
	 * Checks whether an event has any registered listeners.
	 *
	 * @param string|null $eventName The name of the event
	 *
	 * @return bool true if the specified event has any listeners, false otherwise
	 * @deprecated 20.0.0
	 */
	public function hasListeners($eventName = null) {
		return $this->eventDispatcher->getSymfonyDispatcher()->hasListeners($eventName);
	}
}