Skip to content

Commit 139a033

Browse files
authored
Make CacheInvalidation.php work with Symfony 6 (#529)
Signatures of symfony 5 and 6 are incompatible for implementers.
1 parent 367334e commit 139a033

File tree

7 files changed

+149
-25
lines changed

7 files changed

+149
-25
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
.gitignore export-ignore
33
.scrutinizer.yml export-ignore
44
.travis.yml export-ignore
5+
.phpstan/ export-ignore
56
phpunit.xml.dist export-ignore
67
tests/ export-ignore

.phpstan/classAliases.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
use FOS\HttpCache\SymfonyCache\CacheInvalidation;
4+
use FOS\HttpCache\SymfonyCache\Compatibility\CacheInvalidationS6;
5+
use FOS\HttpCache\SymfonyCache\Compatibility\CacheInvalidationLegacy;
6+
7+
/*
8+
* Hint phpstan on the class aliases.
9+
* Symfony 6 introduced a BC break in the signature of the protected method HttpKernelInterface::fetch.
10+
* Load the correct interface to match the signature.
11+
*/
12+
if (version_compare(PHP_VERSION, '8.1.0', '>=')) {
13+
class_alias(
14+
CacheInvalidationS6::class,
15+
CacheInvalidation::class
16+
);
17+
} else {
18+
class_alias(
19+
CacheInvalidationLegacy::class,
20+
CacheInvalidation::class
21+
);
22+
}

phpstan.neon.dist

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ parameters:
22
level: 1
33
paths:
44
- src
5+
bootstrapFiles:
6+
- .phpstan/classAliases.php
57
excludePaths:
68
analyseAndScan:
79
# contains code to support legacy phpunit versions

phpstan.tests.neon.dist

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
parameters:
22
level: 1
3+
bootstrapFiles:
4+
- .phpstan/classAliases.php
35
paths:
46
- tests

src/SymfonyCache/CacheInvalidation.php

+28-25
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,38 @@
1111

1212
namespace FOS\HttpCache\SymfonyCache;
1313

14-
use Symfony\Component\HttpFoundation\Request;
15-
use Symfony\Component\HttpFoundation\Response;
16-
use Symfony\Component\HttpKernel\HttpCache\StoreInterface;
14+
use function class_alias;
15+
use function class_exists;
1716
use Symfony\Component\HttpKernel\HttpKernelInterface;
17+
use Symfony\Component\HttpKernel\Kernel;
1818

19-
/**
20-
* Interface for a HttpCache that supports active cache invalidation.
19+
if (interface_exists(CacheInvalidation::class)) {
20+
return;
21+
}
22+
23+
/*
24+
* Symfony 6 introduced a BC break in the signature of the protected method HttpKernelInterface::fetch.
25+
* Load the correct interface to match the signature.
2126
*/
22-
interface CacheInvalidation extends HttpKernelInterface
23-
{
24-
/**
25-
* Forwards the Request to the backend and determines whether the response should be stored.
26-
*
27-
* This methods is triggered when the cache missed or a reload is required.
28-
*
29-
* This method is present on HttpCache but must be public to allow event listeners to do
30-
* refresh operations.
31-
*
32-
* @param Request $request A Request instance
33-
* @param bool $catch Whether to process exceptions
34-
*
35-
* @return Response A Response instance
36-
*/
37-
public function fetch(Request $request, $catch = false);
27+
if (class_exists(Kernel::class) && Kernel::MAJOR_VERSION >= 6) {
28+
// Load class for Symfony >=6.0
29+
class_alias(
30+
Compatibility\CacheInvalidationS6::class,
31+
CacheInvalidation::class
32+
);
33+
} else {
34+
// Load class for any other cases
35+
class_alias(
36+
Compatibility\CacheInvalidationLegacy::class,
37+
CacheInvalidation::class
38+
);
39+
}
3840

41+
if (!interface_exists(CacheInvalidation::class)) {
3942
/**
40-
* Gets the store for cached responses.
41-
*
42-
* @return StoreInterface $store The store used by the HttpCache
43+
* Provide an empty interface for code scanners.
4344
*/
44-
public function getStore();
45+
interface SearchHandler extends HttpKernelInterface
46+
{
47+
}
4548
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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\SymfonyCache\Compatibility;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\HttpFoundation\Response;
16+
use Symfony\Component\HttpKernel\HttpCache\StoreInterface;
17+
use Symfony\Component\HttpKernel\HttpKernelInterface;
18+
19+
/**
20+
* Interface for a HttpCache that supports active cache invalidation.
21+
*
22+
* Method signature of `fetch` and `getStore` compatible with Symfony 5 and older.
23+
*/
24+
interface CacheInvalidationLegacy extends HttpKernelInterface
25+
{
26+
/**
27+
* Forwards the Request to the backend and determines whether the response should be stored.
28+
*
29+
* This methods is triggered when the cache missed or a reload is required.
30+
*
31+
* This method is present on HttpCache but must be public to allow event listeners to do
32+
* refresh operations.
33+
*
34+
* @param Request $request A Request instance
35+
* @param bool $catch Whether to process exceptions
36+
*
37+
* @return Response A Response instance
38+
*/
39+
public function fetch(Request $request, $catch = false);
40+
41+
/**
42+
* Gets the store for cached responses.
43+
*
44+
* @return StoreInterface $store The store used by the HttpCache
45+
*/
46+
public function getStore();
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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\SymfonyCache\Compatibility;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\HttpFoundation\Response;
16+
use Symfony\Component\HttpKernel\HttpCache\StoreInterface;
17+
use Symfony\Component\HttpKernel\HttpKernelInterface;
18+
19+
/**
20+
* Interface for a HttpCache that supports active cache invalidation.
21+
*
22+
* Method signature of `fetch` and `getStore` compatible with Symfony 6 or newer.
23+
*/
24+
interface CacheInvalidationS6 extends HttpKernelInterface
25+
{
26+
/**
27+
* Forwards the Request to the backend and determines whether the response should be stored.
28+
*
29+
* This methods is triggered when the cache missed or a reload is required.
30+
*
31+
* This method is present on HttpCache but must be public to allow event listeners to do
32+
* refresh operations.
33+
*
34+
* @param Request $request A Request instance
35+
* @param bool $catch Whether to process exceptions
36+
*
37+
* @return Response A Response instance
38+
*/
39+
public function fetch(Request $request, bool $catch = false): Response;
40+
41+
/**
42+
* Gets the store for cached responses.
43+
*
44+
* @return StoreInterface $store The store used by the HttpCache
45+
*/
46+
public function getStore(): StoreInterface;
47+
}

0 commit comments

Comments
 (0)