Skip to content

Commit e5db02b

Browse files
authored
Merge pull request #468 from andrerom/symfony_5
Add support for Symfony 5.0 #SymfonyHackday
2 parents 99e856b + 501a378 commit e5db02b

File tree

6 files changed

+59
-11
lines changed

6 files changed

+59
-11
lines changed

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
],
2323
"require": {
2424
"php": "^5.6 || ^7.0.0",
25-
"symfony/event-dispatcher": "^3.4 || ^4.0",
26-
"symfony/options-resolver": "^3.4 || ^4.0",
25+
"symfony/event-dispatcher": "^3.4 || ^4.3 || ^5.0",
26+
"symfony/options-resolver": "^3.4 || ^4.3 || ^5.0",
2727
"php-http/client-implementation": "^1.0 || ^2.0",
2828
"php-http/client-common": "^1.1.0 || ^2.0",
2929
"php-http/message": "^1.0 || ^2.0",
@@ -35,8 +35,8 @@
3535
"php-http/guzzle6-adapter": "^1.0 || ^2.0",
3636
"php-http/mock-client": "^1.2",
3737
"phpunit/phpunit": "^5.7 || ^6.0",
38-
"symfony/process": "^3.4 || ^4.0",
39-
"symfony/http-kernel": "^3.4 || ^4.0"
38+
"symfony/process": "^3.4 || ^4.3 || ^5.0",
39+
"symfony/http-kernel": "^3.4 || ^4.3 || ^5.0"
4040
},
4141
"conflict": {
4242
"toflar/psr6-symfony-http-cache-store": "<1.1.2"

src/BaseEvent.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSHttpCache package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\HttpCache;
13+
14+
use Symfony\Component\EventDispatcher\Event as OldEvent;
15+
use Symfony\Contracts\EventDispatcher\Event as ContractEvent;
16+
17+
if (class_exists(ContractEvent::class)) {
18+
class BaseEvent extends ContractEvent
19+
{
20+
}
21+
} else {
22+
/**
23+
* @codeCoverageIgnore
24+
* @ignore This is purely for 3.4 comparability.
25+
*/
26+
class BaseEvent extends OldEvent
27+
{
28+
}
29+
}

src/CacheInvalidator.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,13 +311,25 @@ public function flush()
311311
$event = new Event();
312312
$event->setException($exception);
313313
if ($exception instanceof ProxyResponseException) {
314-
$this->getEventDispatcher()->dispatch(Events::PROXY_RESPONSE_ERROR, $event);
314+
$this->dispatch($event, Events::PROXY_RESPONSE_ERROR);
315315
} elseif ($exception instanceof ProxyUnreachableException) {
316-
$this->getEventDispatcher()->dispatch(Events::PROXY_UNREACHABLE_ERROR, $event);
316+
$this->dispatch($event, Events::PROXY_UNREACHABLE_ERROR);
317317
}
318318
}
319319

320320
throw $exceptions;
321321
}
322322
}
323+
324+
private function dispatch(Event $event, $eventName)
325+
{
326+
// LegacyEventDispatcherProxy exists in Symfony >= 4.3
327+
if (class_exists(LegacyEventDispatcherProxy::class)) {
328+
// New Symfony 4.3 EventDispatcher signature
329+
$this->getEventDispatcher()->dispatch($event, $eventName);
330+
} else {
331+
// Old EventDispatcher signature
332+
$this->getEventDispatcher()->dispatch($eventName, $event);
333+
}
334+
}
323335
}

src/Event.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
namespace FOS\HttpCache;
1313

14-
use Symfony\Component\EventDispatcher\Event as BaseEvent;
15-
1614
class Event extends BaseEvent
1715
{
1816
private $exception;

src/SymfonyCache/CacheEvent.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace FOS\HttpCache\SymfonyCache;
1313

14-
use Symfony\Component\EventDispatcher\Event;
14+
use FOS\HttpCache\BaseEvent;
1515
use Symfony\Component\HttpFoundation\Request;
1616
use Symfony\Component\HttpFoundation\Response;
1717
use Symfony\Component\HttpKernel\HttpKernelInterface;
@@ -21,7 +21,7 @@
2121
*
2222
* @author David Buchmann <[email protected]>
2323
*/
24-
class CacheEvent extends Event
24+
class CacheEvent extends BaseEvent
2525
{
2626
/**
2727
* @var CacheInvalidation

src/SymfonyCache/EventDispatchingHttpCache.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,16 @@ protected function dispatch($name, Request $request, Response $response = null,
145145
{
146146
if ($this->getEventDispatcher()->hasListeners($name)) {
147147
$event = new CacheEvent($this, $request, $response, $requestType);
148-
$this->getEventDispatcher()->dispatch($name, $event);
148+
149+
// LegacyEventDispatcherProxy exists in Symfony >= 4.3
150+
if (class_exists(LegacyEventDispatcherProxy::class)) {
151+
// New Symfony 4.3 EventDispatcher signature
152+
$this->getEventDispatcher()->dispatch($event, $name);
153+
} else {
154+
// Old EventDispatcher signature
155+
$this->getEventDispatcher()->dispatch($name, $event);
156+
}
157+
149158
$response = $event->getResponse();
150159
}
151160

0 commit comments

Comments
 (0)