Skip to content

Commit

Permalink
adjust for symfony 7
Browse files Browse the repository at this point in the history
  • Loading branch information
dbu committed Dec 11, 2023
1 parent 0761a0b commit 7b5582d
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 69 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
}
],
"require": {
"php": "^7.2 || ^8.0",
"php": "^8.0",
"symfony/event-dispatcher": "^4.3 || ^5.0 || ^6.0 || ^7.0",
"symfony/options-resolver": "^4.3 || ^5.0 || ^6.0 || ^7.0",
"php-http/client-implementation": "^1.0 || ^2.0",
Expand Down
7 changes: 2 additions & 5 deletions src/SymfonyCache/AccessControlledListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@
*/
abstract class AccessControlledListener implements EventSubscriberInterface
{
/**
* @var RequestMatcherInterface
*/
private $clientMatcher;
private RequestMatcherInterface $clientMatcher;

/**
* When creating this event listener, you can configure a number of options.
Expand Down Expand Up @@ -57,7 +54,7 @@ public function __construct(array $options = [])
if (!$clientMatcher) {
$clientMatcher = class_exists(IpsRequestMatcher::class)
? new IpsRequestMatcher($options['client_ips'] ?: '127.0.0.1')
: new RequestMatcher(null, null, null, $options['client_ips'] ?: '127.0.0.1')
: new RequestMatcher(null, null, null, $options['client_ips'] ?: '127.0.0.1') /* @phpstan-ignore-line for BC with Symfony < 6.2 */

Check failure on line 57 in src/SymfonyCache/AccessControlledListener.php

View workflow job for this annotation

GitHub Actions / PHPStan src

No error to ignore is reported on line 57.
;
}

Expand Down
38 changes: 10 additions & 28 deletions src/SymfonyCache/CacheEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,13 @@
*/
class CacheEvent extends BaseEvent
{
/**
* @var CacheInvalidation
*/
private $kernel;
private CacheInvalidation $kernel;

/**
* @var Request
*/
private $request;
private Request $request;

/**
* @var Response
*/
private $response;
private ?Response $response;

/**
* @var int
*/
private $requestType;
private int $requestType;

/**
* Make sure your $kernel implements CacheInvalidationInterface.
Expand All @@ -51,7 +39,7 @@ class CacheEvent extends BaseEvent
* @param Response|null $response the response, if available
* @param int $requestType the request type (default HttpKernelInterface::MASTER_REQUEST)
*/
public function __construct(CacheInvalidation $kernel, Request $request, Response $response = null, $requestType = HttpKernelInterface::MASTER_REQUEST)
public function __construct(CacheInvalidation $kernel, Request $request, Response $response = null, int $requestType = HttpKernelInterface::MAIN_REQUEST)
{
$this->kernel = $kernel;
$this->request = $request;
Expand All @@ -71,31 +59,25 @@ public function getKernel()

/**
* Get the request that is being processed.
*
* @return Request
*/
public function getRequest()
public function getRequest(): Request
{
return $this->request;
}

/**
* One of the constants HttpKernelInterface::MASTER_REQUEST or SUB_REQUEST.
*
* @return int
* One of the constants HttpKernelInterface::MAIN_REQUEST or SUB_REQUEST.
*/
public function getRequestType()
public function getRequestType(): int
{
return $this->requestType;
}

/**
* Events that occur after the response is created provide the default response.
* Event listeners can also set the response to make it available here.
*
* @return Response|null the response if one was set
*/
public function getResponse()
public function getResponse(): ?Response
{
return $this->response;
}
Expand All @@ -105,7 +87,7 @@ public function getResponse()
*
* Setting a response stops propagation of the event to further event handlers.
*/
public function setResponse(Response $response)
public function setResponse(Response $response): void
{
$this->response = $response;

Expand Down
24 changes: 7 additions & 17 deletions src/SymfonyCache/EventDispatchingHttpCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,9 @@
*/
trait EventDispatchingHttpCache
{
/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;
private ?EventDispatcherInterface $eventDispatcher = null;

/**
* Get event dispatcher.
*
* @return EventDispatcherInterface
*/
public function getEventDispatcher()
public function getEventDispatcher(): EventDispatcherInterface
{
if (!$this->eventDispatcher) {
$this->eventDispatcher = new EventDispatcher();
Expand All @@ -64,7 +56,7 @@ public function getEventDispatcher()
*
* @see EventDispatcherInterface::addSubscriber
*/
public function addSubscriber(EventSubscriberInterface $subscriber)
public function addSubscriber(EventSubscriberInterface $subscriber): void
{
$this->getEventDispatcher()->addSubscriber($subscriber);
}
Expand All @@ -74,7 +66,7 @@ public function addSubscriber(EventSubscriberInterface $subscriber)
*
* @see EventDispatcherInterface::addListener
*/
public function addListener($eventName, $listener, $priority = 0)
public function addListener($eventName, $listener, $priority = 0): void
{
$this->getEventDispatcher()->addListener($eventName, $listener, $priority);
}
Expand All @@ -84,7 +76,7 @@ public function addListener($eventName, $listener, $priority = 0)
*
* Adding the Events::PRE_HANDLE and Events::POST_HANDLE events.
*/
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true): Response
public function handle(Request $request, $type = HttpKernelInterface::MAIN_REQUEST, $catch = true): Response
{
// trigger loading the CacheEvent to avoid fatal error when HttpKernel::loadClassCache is used.
class_exists(CacheEvent::class);
Expand All @@ -102,10 +94,8 @@ class_exists(CacheEvent::class);
* {@inheritdoc}
*
* Trigger event to alter response before storing it in the cache.
*
* @return void
*/
protected function store(Request $request, Response $response)
protected function store(Request $request, Response $response): void
{
$response = $this->dispatch(Events::PRE_STORE, $request, $response);

Expand Down Expand Up @@ -135,7 +125,7 @@ protected function invalidate(Request $request, $catch = false): Response
*
* @return Response|null The response to return, which might be provided/altered by a listener
*/
protected function dispatch($name, Request $request, Response $response = null, $requestType = HttpKernelInterface::MASTER_REQUEST): ?Response
protected function dispatch(string $name, Request $request, Response $response = null, int $requestType = HttpKernelInterface::MAIN_REQUEST): ?Response
{
if ($this->getEventDispatcher()->hasListeners($name)) {
$event = new CacheEvent($this, $request, $response, $requestType);
Expand Down
15 changes: 6 additions & 9 deletions src/SymfonyCache/KernelDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,19 @@
*/
class KernelDispatcher implements Dispatcher
{
/**
* @var HttpCacheProvider
*/
private $httpCacheProvider;
private HttpCacheProvider $httpCacheProvider;

/**
* @var array
* @var Request[]
*/
private $queue = [];
private array $queue = [];

public function __construct(HttpCacheProvider $httpCacheProvider)
{
$this->httpCacheProvider = $httpCacheProvider;
}

public function invalidate(RequestInterface $invalidationRequest, $validateHost = true)
public function invalidate(RequestInterface $invalidationRequest, $validateHost = true): void
{
$request = Request::create(
$invalidationRequest->getUri(),
Expand Down Expand Up @@ -82,7 +79,7 @@ public function invalidate(RequestInterface $invalidationRequest, $validateHost
$this->queue[sha1($request)] = $request;
}

public function flush()
public function flush(): int
{
if (!count($this->queue)) {
return 0;
Expand All @@ -98,7 +95,7 @@ public function flush()

foreach ($queue as $request) {
try {
$httpCache->handle($request, HttpKernelInterface::MASTER_REQUEST, false);
$httpCache->handle($request, HttpKernelInterface::MAIN_REQUEST, false);
} catch (\Exception $e) {
$exceptions->add($e);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Functional/Fixtures/Symfony/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class AppKernel implements HttpKernelInterface
*
* {@inheritdoc}
*/
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true): Response
public function handle(Request $request, $type = HttpKernelInterface::MAIN_REQUEST, $catch = true): Response
{
switch ($request->getPathInfo()) {
case '/cache':
Expand Down
2 changes: 1 addition & 1 deletion tests/Functional/Symfony/EventDispatchingHttpCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function testEventListeners()

$httpKernel = \Mockery::mock(HttpKernelInterface::class)
->shouldReceive('handle')
->withArgs([$request, HttpKernelInterface::MASTER_REQUEST, true])
->withArgs([$request, HttpKernelInterface::MAIN_REQUEST, true])
->andReturn($expectedResponse)
->getMock();
$store = \Mockery::mock(StoreInterface::class)
Expand Down
2 changes: 1 addition & 1 deletion tests/Functional/Varnish/UserContextFailureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function setUp(): void
{
// needs to be decided before doing the setup
// phpunit 9 calls the method getName(), phpunit 10 name()
$name = method_exists($this, 'name') ? $this->name() : $this->getName(); /* @phpstan-ignore-line */
$name = method_exists($this, 'name') ? $this->name() : $this->getName();

Check failure on line 35 in tests/Functional/Varnish/UserContextFailureTest.php

View workflow job for this annotation

GitHub Actions / PHPStan tests

Call to an undefined method FOS\HttpCache\Tests\Functional\Varnish\UserContextFailureTest::getName().
$this->mode = 'testHashRequestFailure' === $name ? 'failure' : 'cache';

parent::setUp();
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/SymfonyCache/CacheEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function testEventGetters()
$this->assertSame($this->kernel, $event->getKernel());
$this->assertSame($request, $event->getRequest());
$this->assertNull($event->getResponse());
$this->assertSame(HttpKernelInterface::MASTER_REQUEST, $event->getRequestType());
$this->assertSame(HttpKernelInterface::MAIN_REQUEST, $event->getRequestType());

$response = new Response();

Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/SymfonyCache/PurgeListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ private function createRequestMatcher(string $path): RequestMatcherInterface
{
return class_exists(PathRequestMatcher::class)
? new PathRequestMatcher($path)
: new RequestMatcher($path)
: new RequestMatcher($path) /* @phpstan-ignore-line */

Check failure on line 193 in tests/Unit/SymfonyCache/PurgeListenerTest.php

View workflow job for this annotation

GitHub Actions / PHPStan tests

No error to ignore is reported on line 193.
;
}

Expand Down
12 changes: 9 additions & 3 deletions tests/Unit/SymfonyCache/PurgeTagsListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestMatcher;
use Symfony\Component\HttpFoundation\RequestMatcher\PathRequestMatcher;
use Symfony\Component\HttpFoundation\RequestMatcherInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpCache\StoreInterface;
use Toflar\Psr6HttpCacheStore\Psr6Store;
Expand All @@ -43,7 +45,9 @@ public function testConstructorOverspecified()
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('You may not set both a request matcher and an IP');
new PurgeTagsListener([
'client_matcher' => new RequestMatcher('/forbidden'),
'client_matcher' => class_exists(PathRequestMatcher::class)
? new PathRequestMatcher('/forbidden')
: new RequestMatcher('/forbidden'), /* @phpstan-ignore-line */

Check failure on line 50 in tests/Unit/SymfonyCache/PurgeTagsListenerTest.php

View workflow job for this annotation

GitHub Actions / PHPStan tests

No error to ignore is reported on line 50.
'client_ips' => ['1.2.3.4'],
]);
}
Expand Down Expand Up @@ -122,7 +126,9 @@ public function testPurgeForbiddenMatcher()
{
$kernel = $this->getUnusedKernelMock();

$matcher = new RequestMatcher('/forbidden');
$matcher = class_exists(PathRequestMatcher::class)
? new PathRequestMatcher('/forbidden')
: new RequestMatcher('/forbidden'); /* @phpstan-ignore-line */

Check failure on line 131 in tests/Unit/SymfonyCache/PurgeTagsListenerTest.php

View workflow job for this annotation

GitHub Actions / PHPStan tests

No error to ignore is reported on line 131.
$purgeTagsListener = new PurgeTagsListener(['client_matcher' => $matcher]);
$request = Request::create('http://example.com/foo', 'PURGETAGS');
$event = new CacheEvent($kernel, $request);
Expand Down Expand Up @@ -155,7 +161,7 @@ public function testPurgeForbiddenIp()
public function testOtherMethod()
{
$kernel = $this->getUnusedKernelMock();
$matcher = \Mockery::mock(RequestMatcher::class)
$matcher = \Mockery::mock(RequestMatcherInterface::class)
->shouldNotReceive('isRequestAllowed')
->getMock();

Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/SymfonyCache/RefreshListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function testRefreshForbiddenMatcher()

$matcher = class_exists(PathRequestMatcher::class)
? new PathRequestMatcher('/forbidden')
: new RequestMatcher('/forbidden')
: new RequestMatcher('/forbidden') /* @phpstan-ignore-line */

Check failure on line 63 in tests/Unit/SymfonyCache/RefreshListenerTest.php

View workflow job for this annotation

GitHub Actions / PHPStan tests

No error to ignore is reported on line 63.
;
$refreshListener = new RefreshListener(['client_matcher' => $matcher]);
$request = Request::create('http://example.com/foo');
Expand Down

0 comments on commit 7b5582d

Please sign in to comment.