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

Skip to content

Commit 522600e

Browse files
Merge branch '8.0' into 8.1
* 8.0: [HttpKernel] Fix using HTTP Cache in worker mode bug #62535 [HttpKernel] Don't reset services between fragments redering when using in HttpCache (nicolas-grekas) Fix existing headers must be replaced in CachingHttpClient
2 parents b8ab13e + cbee94c commit 522600e

2 files changed

Lines changed: 94 additions & 51 deletions

File tree

Kernel.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public function __clone()
102102

103103
public function boot(): void
104104
{
105-
if (true === $this->booted) {
105+
if ($this->booted) {
106106
if (!$this->requestStackSize && $this->resetServices) {
107107
if ($this->container->has('services_resetter')) {
108108
$this->container->get('services_resetter')->reset();
@@ -116,7 +116,7 @@ public function boot(): void
116116
return;
117117
}
118118

119-
if (null === $this->container) {
119+
if (!$this->container) {
120120
$this->preBoot();
121121
}
122122

@@ -137,7 +137,7 @@ public function reboot(?string $warmupDir): void
137137

138138
public function terminate(Request $request, Response $response): void
139139
{
140-
if (false === $this->booted) {
140+
if (!$this->booted) {
141141
return;
142142
}
143143

@@ -148,7 +148,7 @@ public function terminate(Request $request, Response $response): void
148148

149149
public function shutdown(): void
150150
{
151-
if (false === $this->booted) {
151+
if (!$this->booted) {
152152
return;
153153
}
154154

@@ -166,18 +166,18 @@ public function shutdown(): void
166166

167167
public function handle(Request $request, int $type = HttpKernelInterface::MAIN_REQUEST, bool $catch = true): Response
168168
{
169-
if (!$this->booted) {
170-
$container = $this->container ?? $this->preBoot();
169+
if (!$this->container) {
170+
$this->preBoot();
171+
}
171172

172-
if ($container->has('http_cache')) {
173-
$this->handlingHttpCache = true;
173+
if (HttpKernelInterface::MAIN_REQUEST === $type && !$this->handlingHttpCache && $this->container->has('http_cache')) {
174+
$this->handlingHttpCache = true;
174175

175-
try {
176-
return $container->get('http_cache')->handle($request, $type, $catch);
177-
} finally {
178-
$this->handlingHttpCache = false;
179-
$this->resetServices = true;
180-
}
176+
try {
177+
return $this->container->get('http_cache')->handle($request, $type, $catch);
178+
} finally {
179+
$this->handlingHttpCache = false;
180+
$this->resetServices = true;
181181
}
182182
}
183183

@@ -586,7 +586,7 @@ protected function buildContainer(): ContainerBuilder
586586
{
587587
foreach (['cache' => $this->getCacheDir(), 'build' => $this->warmupDir ?: $this->getBuildDir()] as $name => $dir) {
588588
if (!is_dir($dir)) {
589-
if (false === @mkdir($dir, 0o777, true) && !is_dir($dir)) {
589+
if (!@mkdir($dir, 0o777, true) && !is_dir($dir)) {
590590
throw new \RuntimeException(\sprintf('Unable to create the "%s" directory (%s).', $name, $dir));
591591
}
592592
} elseif (!is_writable($dir)) {

Tests/KernelTest.php

Lines changed: 79 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,29 @@ public function testServicesAreNotResetBetweenHttpCacheFragments()
527527
$this->assertSame(1, ResettableService::$counter);
528528
}
529529

530+
public function testHttpCacheHandlesRequestsAfterKernelBoot()
531+
{
532+
$kernel = new CustomProjectDirKernel(static function (ContainerBuilder $container) {
533+
$container->register('http_cache', RecordingHttpCache::class)
534+
->setPublic(true);
535+
}, new ThrowingHttpKernel(), 'http_cache_worker');
536+
537+
$kernel->boot();
538+
539+
$firstResponse = $kernel->handle(Request::create('/worker-first'));
540+
$secondResponse = $kernel->handle(Request::create('/worker-second'));
541+
542+
/** @var RecordingHttpCache $httpCache */
543+
$httpCache = $kernel->getContainer()->get('http_cache');
544+
545+
$this->assertSame([
546+
['/worker-first', HttpKernelInterface::MAIN_REQUEST],
547+
['/worker-second', HttpKernelInterface::MAIN_REQUEST],
548+
], $httpCache->handledPaths);
549+
$this->assertSame('cached: /worker-first', $firstResponse->getContent());
550+
$this->assertSame('cached: /worker-second', $secondResponse->getContent());
551+
}
552+
530553
#[Group('time-sensitive')]
531554
public function testKernelStartTimeIsResetWhileBootingAlreadyBootedKernel()
532555
{
@@ -759,6 +782,62 @@ public function process(ContainerBuilder $container): void
759782
}
760783
}
761784

785+
class FragmentHandlingKernel implements HttpKernelInterface
786+
{
787+
public array $handledPaths = [];
788+
public array $resetCounters = [];
789+
790+
public function handle(Request $request, int $type = self::MAIN_REQUEST, bool $catch = true): Response
791+
{
792+
$this->handledPaths[] = [$request->getPathInfo(), $type];
793+
$this->resetCounters[] = ResettableService::$counter;
794+
795+
return new Response($request->getPathInfo());
796+
}
797+
}
798+
799+
class FragmentRenderingHttpCache implements HttpKernelInterface
800+
{
801+
public function __construct(
802+
private KernelInterface $kernel,
803+
private string $trackedServiceId = 'one',
804+
) {
805+
}
806+
807+
public function handle(Request $request, int $type = self::MAIN_REQUEST, bool $catch = true): Response
808+
{
809+
$this->kernel->boot();
810+
$this->kernel->getContainer()->get($this->trackedServiceId);
811+
812+
$responses = [];
813+
foreach (['/first-fragment', '/second-fragment'] as $path) {
814+
$responses[] = $this->kernel->handle(Request::create($path), self::MAIN_REQUEST, $catch);
815+
}
816+
817+
return new Response(implode('', array_map(static fn (Response $response) => $response->getContent(), $responses)));
818+
}
819+
}
820+
821+
class RecordingHttpCache implements HttpKernelInterface
822+
{
823+
public array $handledPaths = [];
824+
825+
public function handle(Request $request, int $type = self::MAIN_REQUEST, bool $catch = true): Response
826+
{
827+
$this->handledPaths[] = [$request->getPathInfo(), $type];
828+
829+
return new Response('cached: '.$request->getPathInfo());
830+
}
831+
}
832+
833+
class ThrowingHttpKernel implements HttpKernelInterface
834+
{
835+
public function handle(Request $request, int $type = self::MAIN_REQUEST, bool $catch = true): Response
836+
{
837+
throw new \LogicException('The worker HTTP kernel should not be reached when the http_cache service handles the request.');
838+
}
839+
}
840+
762841
class KernelForTest extends Kernel
763842
{
764843
public function __construct(string $environment, bool $debug, private readonly bool $fakeContainer = true)
@@ -806,39 +885,3 @@ public function doLoadClassCache(): void
806885
{
807886
}
808887
}
809-
810-
class FragmentHandlingKernel implements HttpKernelInterface
811-
{
812-
public array $handledPaths = [];
813-
public array $resetCounters = [];
814-
815-
public function handle(Request $request, int $type = self::MAIN_REQUEST, bool $catch = true): Response
816-
{
817-
$this->handledPaths[] = [$request->getPathInfo(), $type];
818-
$this->resetCounters[] = ResettableService::$counter;
819-
820-
return new Response($request->getPathInfo());
821-
}
822-
}
823-
824-
class FragmentRenderingHttpCache implements HttpKernelInterface
825-
{
826-
public function __construct(
827-
private KernelInterface $kernel,
828-
private string $trackedServiceId = 'one',
829-
) {
830-
}
831-
832-
public function handle(Request $request, int $type = self::MAIN_REQUEST, bool $catch = true): Response
833-
{
834-
$this->kernel->boot();
835-
$this->kernel->getContainer()->get($this->trackedServiceId);
836-
837-
$responses = [];
838-
foreach (['/first-fragment', '/second-fragment'] as $path) {
839-
$responses[] = $this->kernel->handle(Request::create($path), self::MAIN_REQUEST, $catch);
840-
}
841-
842-
return new Response(implode('', array_map(static fn (Response $response) => $response->getContent(), $responses)));
843-
}
844-
}

0 commit comments

Comments
 (0)