Files
archived-framework-bundle/HttpCache/HttpCache.php
T
Robin Chalas a97e8c7b9f Merge branch '5.4' into 6.0
* 5.4: (30 commits)
  [HttpKernel] Add support for configuring log level, and status code by exception class
  Add return types to HttpCache createSurrogate and createStore methods
  [Serializer] Fix denormalizing of array with empty body
  Fix framework configuration when messenger uses without console
  [Security] Remove annoying deprecation in `UsageTrackingTokenStorage`
  [Mailer] Improve error message when STARTTLS fails
  [Security] Add alias for FirewallMapInterface to @security.firewall.map
  Bump Symfony version to 5.3.10
  Update VERSION for 5.3.9
  Fix CHANGELOG
  Update CHANGELOG for 5.3.9
  Bump Symfony version to 4.4.33
  Update VERSION for 4.4.32
  Fix CHANGELOG
  Update CHANGELOG for 4.4.32
  [Workflow] Remove dead code in XML schemas
  [Runtime] Fix test for env var names
  Bump Symfony version to 5.3.9
  Update VERSION for 5.3.8
  Update CHANGELOG for 5.3.8
  ...
2021-09-30 11:30:05 +02:00

98 lines
2.7 KiB
PHP

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\HttpCache;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpCache\Esi;
use Symfony\Component\HttpKernel\HttpCache\HttpCache as BaseHttpCache;
use Symfony\Component\HttpKernel\HttpCache\Store;
use Symfony\Component\HttpKernel\HttpCache\StoreInterface;
use Symfony\Component\HttpKernel\HttpCache\SurrogateInterface;
use Symfony\Component\HttpKernel\KernelInterface;
/**
* Manages HTTP cache objects in a Container.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class HttpCache extends BaseHttpCache
{
protected $cacheDir;
protected $kernel;
private $store;
private $surrogate;
private $options;
/**
* @param $cache The cache directory (default used if null) or the storage instance
*/
public function __construct(KernelInterface $kernel, string|StoreInterface $cache = null, SurrogateInterface $surrogate = null, array $options = null)
{
$this->kernel = $kernel;
$this->surrogate = $surrogate;
$this->options = $options ?? [];
if ($cache instanceof StoreInterface) {
$this->store = $cache;
} else {
$this->cacheDir = $cache;
}
if (null === $options && $kernel->isDebug()) {
$this->options = ['debug' => true];
}
if ($this->options['debug'] ?? false) {
$this->options += ['stale_if_error' => 0];
}
parent::__construct($kernel, $this->createStore(), $this->createSurrogate(), array_merge($this->options, $this->getOptions()));
}
/**
* {@inheritdoc}
*/
protected function forward(Request $request, bool $catch = false, Response $entry = null): Response
{
$this->getKernel()->boot();
$this->getKernel()->getContainer()->set('cache', $this);
return parent::forward($request, $catch, $entry);
}
/**
* Returns an array of options to customize the Cache configuration.
*/
protected function getOptions(): array
{
return [];
}
/**
* @return SurrogateInterface
*/
protected function createSurrogate()
{
return $this->surrogate ?? new Esi();
}
/**
* @return StoreInterface
*/
protected function createStore()
{
return $this->store ?? new Store($this->cacheDir ?: $this->kernel->getCacheDir().'/http_cache');
}
}