Skip to content

Commit

Permalink
Merge pull request #24892 from nextcloud/fix/use-symfony-dispatcher-c…
Browse files Browse the repository at this point in the history
…orrectly

Use the Symfony dispatcher correctly
  • Loading branch information
MorrisJobke authored Jan 7, 2021
2 parents 275f15c + 85454ac commit 1e3c071
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions lib/private/EventDispatcher/SymfonyAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,27 +63,35 @@ public function __construct(EventDispatcher $eventDispatcher, ILogger $logger) {
* @param Event|null $event The event to pass to the event handlers/listeners
* If not supplied, an empty Event instance is created
*
* @return void
* @return object the emitted event
* @deprecated 20.0.0
*/
public function dispatch($eventName, $event = null) {
public function dispatch($eventName, $event = null): object {
// type hinting is not possible, due to usage of GenericEvent
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 {
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']
);
}
$this->eventDispatcher->getSymfonyDispatcher()->dispatch($eventName, $newEvent);
$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) {
return $this->eventDispatcher->getSymfonyDispatcher()->dispatch($eventName);
}

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

/**
Expand Down

0 comments on commit 1e3c071

Please sign in to comment.