Skip to content

Commit

Permalink
Merge 63f988d into 367334e
Browse files Browse the repository at this point in the history
  • Loading branch information
netwarex committed Jul 6, 2022
2 parents 367334e + 63f988d commit d431a93
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 25 deletions.
22 changes: 22 additions & 0 deletions classAliases.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use FOS\HttpCache\SymfonyCache\CacheInvalidation;
use FOS\HttpCache\SymfonyCache\Compatibility\CacheInvalidationS6;
use FOS\HttpCache\SymfonyCache\Compatibility\CacheInvalidationLegacy;

/*
* Hint phpstan on the class aliases.
* Symfony 6 introduced a BC break in the signature of the protected method HttpKernelInterface::fetch.
* Load the correct interface to match the signature.
*/
if (version_compare(PHP_VERSION, '8.1.0', '>=')) {
class_alias(
CacheInvalidationS6::class,
CacheInvalidation::class
);
} else {
class_alias(
CacheInvalidationLegacy::class,
CacheInvalidation::class
);
}
2 changes: 2 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ parameters:
level: 1
paths:
- src
bootstrapFiles:
- classAliases.php
excludePaths:
analyseAndScan:
# contains code to support legacy phpunit versions
Expand Down
53 changes: 28 additions & 25 deletions src/SymfonyCache/CacheInvalidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,38 @@

namespace FOS\HttpCache\SymfonyCache;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpCache\StoreInterface;
use function class_alias;
use function class_exists;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Kernel;

/**
* Interface for a HttpCache that supports active cache invalidation.
if (interface_exists(CacheInvalidation::class)) {
return;
}

/*
* Symfony 6 introduced a BC break in the signature of the protected method HttpKernelInterface::fetch.
* Load the correct interface to match the signature.
*/
interface CacheInvalidation extends HttpKernelInterface
{
/**
* Forwards the Request to the backend and determines whether the response should be stored.
*
* This methods is triggered when the cache missed or a reload is required.
*
* This method is present on HttpCache but must be public to allow event listeners to do
* refresh operations.
*
* @param Request $request A Request instance
* @param bool $catch Whether to process exceptions
*
* @return Response A Response instance
*/
public function fetch(Request $request, $catch = false);
if (class_exists(Kernel::class) && Kernel::MAJOR_VERSION >= 6) {
// Load class for Symfony >=6.0
class_alias(
Compatibility\CacheInvalidationS6::class,
CacheInvalidation::class
);
} else {
// Load class for any other cases
class_alias(
Compatibility\CacheInvalidationLegacy::class,
CacheInvalidation::class
);
}

if (!interface_exists(CacheInvalidation::class)) {
/**
* Gets the store for cached responses.
*
* @return StoreInterface $store The store used by the HttpCache
* Provide an empty interface for code scanners.
*/
public function getStore();
interface SearchHandler extends HttpKernelInterface
{
}
}
47 changes: 47 additions & 0 deletions src/SymfonyCache/Compatibility/CacheInvalidationLegacy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/*
* This file is part of the FOSHttpCache package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\HttpCache\SymfonyCache\Compatibility;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpCache\StoreInterface;
use Symfony\Component\HttpKernel\HttpKernelInterface;

/**
* Interface for a HttpCache that supports active cache invalidation.
*
* Method signature of `fetch` and `getStore` compatible with Symfony 5 and older.
*/
interface CacheInvalidationLegacy extends HttpKernelInterface
{
/**
* Forwards the Request to the backend and determines whether the response should be stored.
*
* This methods is triggered when the cache missed or a reload is required.
*
* This method is present on HttpCache but must be public to allow event listeners to do
* refresh operations.
*
* @param Request $request A Request instance
* @param bool $catch Whether to process exceptions
*
* @return Response A Response instance
*/
public function fetch(Request $request, $catch = false);

/**
* Gets the store for cached responses.
*
* @return StoreInterface $store The store used by the HttpCache
*/
public function getStore();
}
47 changes: 47 additions & 0 deletions src/SymfonyCache/Compatibility/CacheInvalidationS6.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/*
* This file is part of the FOSHttpCache package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\HttpCache\SymfonyCache\Compatibility;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpCache\StoreInterface;
use Symfony\Component\HttpKernel\HttpKernelInterface;

/**
* Interface for a HttpCache that supports active cache invalidation.
*
* Method signature of `fetch` and `getStore` compatible with Symfony 6 or newer.
*/
interface CacheInvalidationS6 extends HttpKernelInterface
{
/**
* Forwards the Request to the backend and determines whether the response should be stored.
*
* This methods is triggered when the cache missed or a reload is required.
*
* This method is present on HttpCache but must be public to allow event listeners to do
* refresh operations.
*
* @param Request $request A Request instance
* @param bool $catch Whether to process exceptions
*
* @return Response A Response instance
*/
public function fetch(Request $request, bool $catch = false): Response;

/**
* Gets the store for cached responses.
*
* @return StoreInterface $store The store used by the HttpCache
*/
public function getStore(): StoreInterface;
}

0 comments on commit d431a93

Please sign in to comment.