Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 09e6a82

Browse files
stakovicznicolas-grekas
authored andcommitted
[HttpKernel] Fix using HTTP Cache in worker mode
1 parent aba894b commit 09e6a82

2 files changed

Lines changed: 59 additions & 16 deletions

File tree

Kernel.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function __clone()
110110
*/
111111
public function boot()
112112
{
113-
if (true === $this->booted) {
113+
if ($this->booted) {
114114
if (!$this->requestStackSize && $this->resetServices) {
115115
if ($this->container->has('services_resetter')) {
116116
$this->container->get('services_resetter')->reset();
@@ -124,7 +124,7 @@ public function boot()
124124
return;
125125
}
126126

127-
if (null === $this->container) {
127+
if (!$this->container) {
128128
$this->preBoot();
129129
}
130130

@@ -151,7 +151,7 @@ public function reboot(?string $warmupDir)
151151
*/
152152
public function terminate(Request $request, Response $response)
153153
{
154-
if (false === $this->booted) {
154+
if (!$this->booted) {
155155
return;
156156
}
157157

@@ -165,7 +165,7 @@ public function terminate(Request $request, Response $response)
165165
*/
166166
public function shutdown()
167167
{
168-
if (false === $this->booted) {
168+
if (!$this->booted) {
169169
return;
170170
}
171171

@@ -183,18 +183,18 @@ public function shutdown()
183183

184184
public function handle(Request $request, int $type = HttpKernelInterface::MAIN_REQUEST, bool $catch = true): Response
185185
{
186-
if (!$this->booted) {
187-
$container = $this->container ?? $this->preBoot();
186+
if (!$this->container) {
187+
$this->preBoot();
188+
}
188189

189-
if ($container->has('http_cache')) {
190-
$this->handlingHttpCache = true;
190+
if (HttpKernelInterface::MAIN_REQUEST === $type && !$this->handlingHttpCache && $this->container->has('http_cache')) {
191+
$this->handlingHttpCache = true;
191192

192-
try {
193-
return $container->get('http_cache')->handle($request, $type, $catch);
194-
} finally {
195-
$this->handlingHttpCache = false;
196-
$this->resetServices = true;
197-
}
193+
try {
194+
return $this->container->get('http_cache')->handle($request, $type, $catch);
195+
} finally {
196+
$this->handlingHttpCache = false;
197+
$this->resetServices = true;
198198
}
199199
}
200200

@@ -614,7 +614,7 @@ protected function buildContainer(): ContainerBuilder
614614
{
615615
foreach (['cache' => $this->getCacheDir(), 'build' => $this->warmupDir ?: $this->getBuildDir(), 'logs' => $this->getLogDir()] as $name => $dir) {
616616
if (!is_dir($dir)) {
617-
if (false === @mkdir($dir, 0777, true) && !is_dir($dir)) {
617+
if (!@mkdir($dir, 0o777, true) && !is_dir($dir)) {
618618
throw new \RuntimeException(\sprintf('Unable to create the "%s" directory (%s).', $name, $dir));
619619
}
620620
} elseif (!is_writable($dir)) {

Tests/KernelTest.php

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
use Symfony\Component\DependencyInjection\Container;
2020
use Symfony\Component\DependencyInjection\ContainerBuilder;
2121
use Symfony\Component\DependencyInjection\ContainerInterface;
22-
use Symfony\Component\DependencyInjection\Reference;
2322
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
2423
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
24+
use Symfony\Component\DependencyInjection\Reference;
2525
use Symfony\Component\Filesystem\Exception\IOException;
2626
use Symfony\Component\Filesystem\Filesystem;
2727
use Symfony\Component\HttpFoundation\Request;
@@ -633,6 +633,29 @@ public function testServicesAreNotResetBetweenHttpCacheFragments()
633633
$this->assertSame(1, ResettableService::$counter);
634634
}
635635

636+
public function testHttpCacheHandlesRequestsAfterKernelBoot()
637+
{
638+
$kernel = new CustomProjectDirKernel(static function (ContainerBuilder $container) {
639+
$container->register('http_cache', RecordingHttpCache::class)
640+
->setPublic(true);
641+
}, new ThrowingHttpKernel(), 'http_cache_worker');
642+
643+
$kernel->boot();
644+
645+
$firstResponse = $kernel->handle(Request::create('/worker-first'));
646+
$secondResponse = $kernel->handle(Request::create('/worker-second'));
647+
648+
/** @var RecordingHttpCache $httpCache */
649+
$httpCache = $kernel->getContainer()->get('http_cache');
650+
651+
$this->assertSame([
652+
['/worker-first', HttpKernelInterface::MAIN_REQUEST],
653+
['/worker-second', HttpKernelInterface::MAIN_REQUEST],
654+
], $httpCache->handledPaths);
655+
$this->assertSame('cached: /worker-first', $firstResponse->getContent());
656+
$this->assertSame('cached: /worker-second', $secondResponse->getContent());
657+
}
658+
636659
/**
637660
* @group time-sensitive
638661
*/
@@ -882,3 +905,23 @@ public function handle(Request $request, int $type = self::MAIN_REQUEST, bool $c
882905
return new Response(implode('', array_map(static fn (Response $response) => $response->getContent(), $responses)));
883906
}
884907
}
908+
909+
class RecordingHttpCache implements HttpKernelInterface
910+
{
911+
public array $handledPaths = [];
912+
913+
public function handle(Request $request, int $type = self::MAIN_REQUEST, bool $catch = true): Response
914+
{
915+
$this->handledPaths[] = [$request->getPathInfo(), $type];
916+
917+
return new Response('cached: '.$request->getPathInfo());
918+
}
919+
}
920+
921+
class ThrowingHttpKernel implements HttpKernelInterface
922+
{
923+
public function handle(Request $request, int $type = self::MAIN_REQUEST, bool $catch = true): Response
924+
{
925+
throw new \LogicException('The worker HTTP kernel should not be reached when the http_cache service handles the request.');
926+
}
927+
}

0 commit comments

Comments
 (0)