Skip to content

Add support for Symfony 5.0 #SymfonyHackday #468

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

Merged
merged 4 commits into from
Nov 25, 2019
Merged
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
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
],
"require": {
"php": "^5.6 || ^7.0.0",
"symfony/event-dispatcher": "^3.4 || ^4.0",
"symfony/options-resolver": "^3.4 || ^4.0",
"symfony/event-dispatcher": "^3.4 || ^4.3 || ^5.0",
"symfony/options-resolver": "^3.4 || ^4.3 || ^5.0",
"php-http/client-implementation": "^1.0 || ^2.0",
"php-http/client-common": "^1.1.0 || ^2.0",
"php-http/message": "^1.0 || ^2.0",
Expand All @@ -35,8 +35,8 @@
"php-http/guzzle6-adapter": "^1.0 || ^2.0",
"php-http/mock-client": "^1.2",
"phpunit/phpunit": "^5.7 || ^6.0",
"symfony/process": "^3.4 || ^4.0",
"symfony/http-kernel": "^3.4 || ^4.0"
"symfony/process": "^3.4 || ^4.3 || ^5.0",
"symfony/http-kernel": "^3.4 || ^4.3 || ^5.0"
},
"conflict": {
"toflar/psr6-symfony-http-cache-store": "<1.1.2"
Expand Down
29 changes: 29 additions & 0 deletions src/BaseEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the FOSHttpCache package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\HttpCache;

use Symfony\Component\EventDispatcher\Event as OldEvent;
use Symfony\Contracts\EventDispatcher\Event as ContractEvent;

if (class_exists(ContractEvent::class)) {
class BaseEvent extends ContractEvent
{
}
} else {
/**
* @codeCoverageIgnore
* @ignore This is purely for 3.4 comparability.
*/
class BaseEvent extends OldEvent
{
}
}
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);
}
}
}
2 changes: 0 additions & 2 deletions src/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

namespace FOS\HttpCache;

use Symfony\Component\EventDispatcher\Event as BaseEvent;

class Event extends BaseEvent
{
private $exception;
Expand Down
4 changes: 2 additions & 2 deletions src/SymfonyCache/CacheEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace FOS\HttpCache\SymfonyCache;

use Symfony\Component\EventDispatcher\Event;
use FOS\HttpCache\BaseEvent;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
Expand All @@ -21,7 +21,7 @@
*
* @author David Buchmann <[email protected]>
*/
class CacheEvent extends Event
class CacheEvent extends BaseEvent
{
/**
* @var CacheInvalidation
Expand Down
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);
} else {
// Old EventDispatcher signature
$this->getEventDispatcher()->dispatch($name, $event);
}

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

Expand Down