Skip to content

Update dispatch call #467

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/CacheInvalidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,13 +311,25 @@ public function flush()
$event = new Event();
$event->setException($exception);
if ($exception instanceof ProxyResponseException) {
$this->getEventDispatcher()->dispatch(Events::PROXY_RESPONSE_ERROR, $event);
$this->dispatch($event, Events::PROXY_RESPONSE_ERROR);
} elseif ($exception instanceof ProxyUnreachableException) {
$this->getEventDispatcher()->dispatch(Events::PROXY_UNREACHABLE_ERROR, $event);
$this->dispatch($event, Events::PROXY_UNREACHABLE_ERROR);
}
}

throw $exceptions;
}
}

private function dispatch(Event $event, $eventName)
{
// LegacyEventDispatcherProxy exists in Symfony >= 4.3
if (class_exists(LegacyEventDispatcherProxy::class)) {
// New Symfony 4.3 EventDispatcher signature
$this->getEventDispatcher()->dispatch($event, $eventName);
} else {
// Old EventDispatcher signature
$this->getEventDispatcher()->dispatch($eventName, $event);
}
}
}
11 changes: 10 additions & 1 deletion src/SymfonyCache/EventDispatchingHttpCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,16 @@ protected function dispatch($name, Request $request, Response $response = null,
{
if ($this->getEventDispatcher()->hasListeners($name)) {
$event = new CacheEvent($this, $request, $response, $requestType);
$this->getEventDispatcher()->dispatch($name, $event);

// LegacyEventDispatcherProxy exists in Symfony >= 4.3
if (class_exists(LegacyEventDispatcherProxy::class)) {
// New Symfony 4.3 EventDispatcher signature
$this->getEventDispatcher()->dispatch($event, $name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't the point of the legacy proxy that we then can use the same code regardless of dispatcher version?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently not. The LegacyEventDispatcherProxy triggers a deprecation notice if you use the old syntax: https://github.com/symfony/event-dispatcher/blob/4.3/LegacyEventDispatcherProxy.php#L63

And LegacyEventDispatcherProxy is not available in Symfony versions below 4.3 so there is no other way around this hack.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but if we do all that, why do we use the legacy dispatcher at all? does it do anything for us, or could we just conditionally use the new order if we are on a new enough version?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@XWB ping ;-)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dbu Because if somebody extends EventDispatchingHttpCache, the LegacyEventDispatcherProxy will make sure the old dispatcher syntax will continue to work.

} else {
// Old EventDispatcher signature
$this->getEventDispatcher()->dispatch($name, $event);
}

$response = $event->getResponse();
}

Expand Down