|
17 | 17 | use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
18 | 18 | use Symfony\Component\DependencyInjection\ContainerBuilder; |
19 | 19 | use Symfony\Component\DependencyInjection\ContainerInterface; |
| 20 | +use Symfony\Component\DependencyInjection\Reference; |
20 | 21 | use Symfony\Component\DependencyInjection\Extension\ExtensionInterface; |
21 | 22 | use Symfony\Component\Filesystem\Exception\IOException; |
22 | 23 | use Symfony\Component\Filesystem\Filesystem; |
|
30 | 31 | use Symfony\Component\HttpKernel\HttpKernel; |
31 | 32 | use Symfony\Component\HttpKernel\HttpKernelInterface; |
32 | 33 | use Symfony\Component\HttpKernel\Kernel; |
| 34 | +use Symfony\Component\HttpKernel\KernelInterface; |
33 | 35 | use Symfony\Component\HttpKernel\Tests\Fixtures\KernelWithoutBundles; |
34 | 36 | use Symfony\Component\HttpKernel\Tests\Fixtures\ResettableService; |
35 | 37 |
|
@@ -492,6 +494,39 @@ public function testServicesResetter() |
492 | 494 | $this->assertEquals(1, ResettableService::$counter); |
493 | 495 | } |
494 | 496 |
|
| 497 | + public function testServicesAreNotResetBetweenHttpCacheFragments() |
| 498 | + { |
| 499 | + ResettableService::$counter = 0; |
| 500 | + $fragmentKernel = new FragmentHandlingKernel(); |
| 501 | + |
| 502 | + $kernel = new CustomProjectDirKernel(function (ContainerBuilder $container) { |
| 503 | + $container->addCompilerPass(new ResettableServicePass()); |
| 504 | + $container->register('kernel', CustomProjectDirKernel::class) |
| 505 | + ->setSynthetic(true) |
| 506 | + ->setPublic(true); |
| 507 | + $container->register('one', ResettableService::class) |
| 508 | + ->setPublic(true) |
| 509 | + ->addTag('kernel.reset', ['method' => 'reset']); |
| 510 | + $container->register('services_resetter', ServicesResetter::class)->setPublic(true); |
| 511 | + $container->register('http_cache', FragmentRenderingHttpCache::class) |
| 512 | + ->setPublic(true) |
| 513 | + ->addArgument(new Reference('kernel')); |
| 514 | + }, $fragmentKernel, 'http_cache_fragments'); |
| 515 | + |
| 516 | + $kernel->handle(new Request()); |
| 517 | + |
| 518 | + $this->assertSame([ |
| 519 | + ['/first-fragment', HttpKernelInterface::MAIN_REQUEST], |
| 520 | + ['/second-fragment', HttpKernelInterface::MAIN_REQUEST], |
| 521 | + ], $fragmentKernel->handledPaths); |
| 522 | + $this->assertSame([0, 0], $fragmentKernel->resetCounters); |
| 523 | + $this->assertSame(0, ResettableService::$counter); |
| 524 | + |
| 525 | + $kernel->boot(); |
| 526 | + |
| 527 | + $this->assertSame(1, ResettableService::$counter); |
| 528 | + } |
| 529 | + |
495 | 530 | #[Group('time-sensitive')] |
496 | 531 | public function testKernelStartTimeIsResetWhileBootingAlreadyBootedKernel() |
497 | 532 | { |
@@ -731,3 +766,39 @@ public function doLoadClassCache(): void |
731 | 766 | { |
732 | 767 | } |
733 | 768 | } |
| 769 | + |
| 770 | +class FragmentHandlingKernel implements HttpKernelInterface |
| 771 | +{ |
| 772 | + public array $handledPaths = []; |
| 773 | + public array $resetCounters = []; |
| 774 | + |
| 775 | + public function handle(Request $request, int $type = self::MAIN_REQUEST, bool $catch = true): Response |
| 776 | + { |
| 777 | + $this->handledPaths[] = [$request->getPathInfo(), $type]; |
| 778 | + $this->resetCounters[] = ResettableService::$counter; |
| 779 | + |
| 780 | + return new Response($request->getPathInfo()); |
| 781 | + } |
| 782 | +} |
| 783 | + |
| 784 | +class FragmentRenderingHttpCache implements HttpKernelInterface |
| 785 | +{ |
| 786 | + public function __construct( |
| 787 | + private KernelInterface $kernel, |
| 788 | + private string $trackedServiceId = 'one', |
| 789 | + ) { |
| 790 | + } |
| 791 | + |
| 792 | + public function handle(Request $request, int $type = self::MAIN_REQUEST, bool $catch = true): Response |
| 793 | + { |
| 794 | + $this->kernel->boot(); |
| 795 | + $this->kernel->getContainer()->get($this->trackedServiceId); |
| 796 | + |
| 797 | + $responses = []; |
| 798 | + foreach (['/first-fragment', '/second-fragment'] as $path) { |
| 799 | + $responses[] = $this->kernel->handle(Request::create($path), self::MAIN_REQUEST, $catch); |
| 800 | + } |
| 801 | + |
| 802 | + return new Response(implode('', array_map(static fn (Response $response) => $response->getContent(), $responses))); |
| 803 | + } |
| 804 | +} |
0 commit comments