Skip to content

Commit 99e856b

Browse files
authored
Merge pull request #463 from XWB/event-dispatcher-fix
Use LegacyEventDispatcherProxy for Symfony 4.3
2 parents df6d775 + 506e2d4 commit 99e856b

File tree

5 files changed

+35
-5
lines changed

5 files changed

+35
-5
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ Changelog
33

44
See also the [GitHub releases page](https://github.com/FriendsOfSymfony/FOSHttpCache/releases).
55

6+
2.8.0
7+
-----
8+
9+
### General
10+
11+
* Use `LegacyEventDispatcherProxy` for Symfony >= 4.3 to avoid deprecation messages.
12+
613
2.7.0
714
-----
815

src/CacheInvalidator.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use FOS\HttpCache\ProxyClient\Symfony;
2626
use Symfony\Component\EventDispatcher\EventDispatcher;
2727
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
28+
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
2829
use Toflar\Psr6HttpCacheStore\Psr6Store;
2930

3031
/**
@@ -143,7 +144,11 @@ public function setEventDispatcher(EventDispatcherInterface $eventDispatcher)
143144
public function getEventDispatcher()
144145
{
145146
if (!$this->eventDispatcher) {
146-
$this->eventDispatcher = new EventDispatcher();
147+
if (class_exists(LegacyEventDispatcherProxy::class)) {
148+
$this->eventDispatcher = LegacyEventDispatcherProxy::decorate(new EventDispatcher());
149+
} else {
150+
$this->eventDispatcher = new EventDispatcher();
151+
}
147152
}
148153

149154
return $this->eventDispatcher;

src/SymfonyCache/EventDispatchingHttpCache.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\EventDispatcher\EventDispatcher;
1515
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
1616
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17+
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
1718
use Symfony\Component\HttpFoundation\Request;
1819
use Symfony\Component\HttpFoundation\Response;
1920
use Symfony\Component\HttpKernel\HttpKernelInterface;
@@ -54,8 +55,12 @@ trait EventDispatchingHttpCache
5455
*/
5556
public function getEventDispatcher()
5657
{
57-
if (null === $this->eventDispatcher) {
58-
$this->eventDispatcher = new EventDispatcher();
58+
if (!$this->eventDispatcher) {
59+
if (class_exists(LegacyEventDispatcherProxy::class)) {
60+
$this->eventDispatcher = LegacyEventDispatcherProxy::decorate(new EventDispatcher());
61+
} else {
62+
$this->eventDispatcher = new EventDispatcher();
63+
}
5964
}
6065

6166
return $this->eventDispatcher;

src/Test/EventDispatchingHttpCacheTestCase.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ protected function getHttpCachePartialMock(array $mockedMethods = null)
6262
'allow_revalidate' => false,
6363
'stale_while_revalidate' => 2,
6464
'stale_if_error' => 60,
65+
'trace_level' => 'full',
66+
'trace_header' => 'FOSHttpCache',
6567
];
6668

6769
$refHttpCache = new \ReflectionClass(HttpCache::class);

tests/Unit/CacheInvalidatorTest.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use Psr\Http\Message\ResponseInterface;
3333
use Psr\Log\LoggerInterface;
3434
use Symfony\Component\EventDispatcher\EventDispatcher;
35+
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
3536

3637
class CacheInvalidatorTest extends TestCase
3738
{
@@ -257,8 +258,13 @@ public function testEventDispatcher()
257258
/** @var MockInterface|Varnish $proxyClient */
258259
$proxyClient = \Mockery::mock(Varnish::class);
259260

261+
if (class_exists(LegacyEventDispatcherProxy::class)) {
262+
$eventDispatcher = LegacyEventDispatcherProxy::decorate(new EventDispatcher());
263+
} else {
264+
$eventDispatcher = new EventDispatcher();
265+
}
266+
260267
$cacheInvalidator = new CacheInvalidator($proxyClient);
261-
$eventDispatcher = new EventDispatcher();
262268
$cacheInvalidator->setEventDispatcher($eventDispatcher);
263269
$this->assertSame($eventDispatcher, $cacheInvalidator->getEventDispatcher());
264270
}
@@ -268,8 +274,13 @@ public function testEventDispatcherImmutable()
268274
/** @var MockInterface|Varnish $proxyClient */
269275
$proxyClient = \Mockery::mock(Varnish::class);
270276

277+
if (class_exists(LegacyEventDispatcherProxy::class)) {
278+
$eventDispatcher = LegacyEventDispatcherProxy::decorate(new EventDispatcher());
279+
} else {
280+
$eventDispatcher = new EventDispatcher();
281+
}
282+
271283
$cacheInvalidator = new CacheInvalidator($proxyClient);
272-
$eventDispatcher = new EventDispatcher();
273284
$cacheInvalidator->setEventDispatcher($eventDispatcher);
274285
$this->expectException(\Exception::class);
275286
$cacheInvalidator->setEventDispatcher($eventDispatcher);

0 commit comments

Comments
 (0)