Skip to content

Commit

Permalink
Merge pull request #468 from andrerom/symfony_5
Browse files Browse the repository at this point in the history
Add support for Symfony 5.0 #SymfonyHackday
  • Loading branch information
dbu committed Nov 25, 2019
2 parents 99e856b + 501a378 commit e5db02b
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 11 deletions.
8 changes: 4 additions & 4 deletions composer.json
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
@@ -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
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
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
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 <mail@davidbu.ch>
*/
class CacheEvent extends Event
class CacheEvent extends BaseEvent
{
/**
* @var CacheInvalidation
Expand Down
11 changes: 10 additions & 1 deletion src/SymfonyCache/EventDispatchingHttpCache.php
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

0 comments on commit e5db02b

Please sign in to comment.