Skip to content

Use LegacyEventDispatcherProxy for Symfony 4.3 #463

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 3 commits into from
Nov 6, 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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ Changelog

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

2.8.0
-----

### General

* Use `LegacyEventDispatcherProxy` for Symfony >= 4.3 to avoid deprecation messages.

2.7.0
-----

Expand Down
7 changes: 6 additions & 1 deletion src/CacheInvalidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use FOS\HttpCache\ProxyClient\Symfony;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
use Toflar\Psr6HttpCacheStore\Psr6Store;

/**
Expand Down Expand Up @@ -143,7 +144,11 @@ public function setEventDispatcher(EventDispatcherInterface $eventDispatcher)
public function getEventDispatcher()
{
if (!$this->eventDispatcher) {
$this->eventDispatcher = new EventDispatcher();
if (class_exists(LegacyEventDispatcherProxy::class)) {
$this->eventDispatcher = LegacyEventDispatcherProxy::decorate(new EventDispatcher());
} else {
$this->eventDispatcher = new EventDispatcher();
}
}

return $this->eventDispatcher;
Expand Down
9 changes: 7 additions & 2 deletions src/SymfonyCache/EventDispatchingHttpCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
Expand Down Expand Up @@ -54,8 +55,12 @@ trait EventDispatchingHttpCache
*/
public function getEventDispatcher()
{
if (null === $this->eventDispatcher) {
$this->eventDispatcher = new EventDispatcher();
if (!$this->eventDispatcher) {
if (class_exists(LegacyEventDispatcherProxy::class)) {
$this->eventDispatcher = LegacyEventDispatcherProxy::decorate(new EventDispatcher());
} else {
$this->eventDispatcher = new EventDispatcher();
}
}

return $this->eventDispatcher;
Expand Down
2 changes: 2 additions & 0 deletions src/Test/EventDispatchingHttpCacheTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ protected function getHttpCachePartialMock(array $mockedMethods = null)
'allow_revalidate' => false,
'stale_while_revalidate' => 2,
'stale_if_error' => 60,
'trace_level' => 'full',
'trace_header' => 'FOSHttpCache',
];

$refHttpCache = new \ReflectionClass(HttpCache::class);
Expand Down
15 changes: 13 additions & 2 deletions tests/Unit/CacheInvalidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;

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

if (class_exists(LegacyEventDispatcherProxy::class)) {
$eventDispatcher = LegacyEventDispatcherProxy::decorate(new EventDispatcher());
} else {
$eventDispatcher = new EventDispatcher();
}

$cacheInvalidator = new CacheInvalidator($proxyClient);
$eventDispatcher = new EventDispatcher();
$cacheInvalidator->setEventDispatcher($eventDispatcher);
$this->assertSame($eventDispatcher, $cacheInvalidator->getEventDispatcher());
}
Expand All @@ -268,8 +274,13 @@ public function testEventDispatcherImmutable()
/** @var MockInterface|Varnish $proxyClient */
$proxyClient = \Mockery::mock(Varnish::class);

if (class_exists(LegacyEventDispatcherProxy::class)) {
$eventDispatcher = LegacyEventDispatcherProxy::decorate(new EventDispatcher());
} else {
$eventDispatcher = new EventDispatcher();
}

$cacheInvalidator = new CacheInvalidator($proxyClient);
$eventDispatcher = new EventDispatcher();
$cacheInvalidator->setEventDispatcher($eventDispatcher);
$this->expectException(\Exception::class);
$cacheInvalidator->setEventDispatcher($eventDispatcher);
Expand Down