8000 feature #36599 [HttpKernel] make kernels implementing `WarmableInterf… · symfony/symfony@07a0334 · GitHub
[go: up one dir, main page]

Skip to content

Commit 07a0334

Browse files
committed
feature #36599 [HttpKernel] make kernels implementing WarmableInterface be part of the cache warmup stage (nicolas-grekas)
This PR was merged into the 5.1-dev branch. Discussion ---------- [HttpKernel] make kernels implementing `WarmableInterface` be part of the cache warmup stage | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - This allows your kernel to return extra classes to preload also (which was my main motivation for creating this PR actually.) ```php // ... use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface; // ... class Kernel ... implements ..., WarmableInterface { // ... public function warmUp(string $cacheDir): array { 8000 // ... return [ SomeClassToPreload::class, AnotherClassClassToPreload::class, $cacheDir.'/some-file-to-preload.php', // ... ]; } // ... } ``` Commits ------- 649e530 [HttpKernel] make kernels implementing `WarmableInterface` be part of the cache warmup stage
2 parents 3a6f8ca + 649e530 commit 07a0334

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

src/Symfony/Component/HttpKernel/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* made `WarmableInterface::warmUp()` return a list of classes or files to preload on PHP 7.4+;
88
not returning an array is deprecated
9+
* made kernels implementing `WarmableInterface` be part of the cache warmup stage
910
* deprecated support for `service:action` syntax to reference controllers, use `serviceOrFqcn::method` instead
1011
* allowed using public aliases to reference controllers
1112
* added session usage reporting when the `_stateless` attribute of the request is set to `true`

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
use Symfony\Component\HttpFoundation\Request;
3636
use Symfony\Component\HttpFoundation\Response;
3737
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
38+
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
3839
use Symfony\Component\HttpKernel\Config\FileLocator;
3940
use Symfony\Component\HttpKernel\DependencyInjection\AddAnnotatedClassesToCachePass;
4041
use Symfony\Component\HttpKernel\DependencyInjection\MergeExtensionConfigurationPass;
@@ -566,12 +567,14 @@ protected function initializeContainer()
566567
touch($oldContainerDir.'.legacy');
567568
}
568569

570+
$preload = $this instanceof WarmableInterface ? (array) $this->warmUp($this->container->getParameter('kernel.cache_dir')) : [];
571+
569572
if ($this->container->has('cache_warmer')) {
570-
$preload = (array) $this->container->get('cache_warmer')->warmUp($this->container->getParameter('kernel.cache_dir'));
573+
$preload = array_merge($preload, (array) $this->container->get('cache_warmer')->warmUp($this->container->getParameter('kernel.cache_dir')));
574+
}
571575

572-
if ($preload && method_exists(Preloader::class, 'append') && file_exists($preloadFile = $cacheDir.'/'.$class.'.preload.php')) {
573-
Preloader::append($preloadFile, $preload);
574-
}
576+
if ($preload && method_exists(Preloader::class, 'append') && file_exists($preloadFile = $cacheDir.'/'.$class.'.preload.php')) {
577+
Preloader::append($preloadFile, $preload);
575578
}
576579
}
577580

src/Symfony/Component/HttpKernel/Tests/KernelTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\HttpFoundation\Request;
2020
use Symfony\Component\HttpFoundation\Response;
2121
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
22+
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
2223
use Symfony\Component\HttpKernel\DependencyInjection\ResettableServicePass;
2324
use Symfony\Component\HttpKernel\DependencyInjection\ServicesResetter;
2425
use Symfony\Component\HttpKernel\HttpKernelInterface;
@@ -480,6 +481,14 @@ public function testKernelPass()
480481
$this->assertTrue($kernel->getContainer()->getParameter('test.processed'));
481482
}
482483

484+
public function testWarmup()
485+
{
486+
$kernel = new CustomProjectDirKernel();
487+
$kernel->boot();
488+
489+
$this->assertTrue($kernel->warmedUp);
490+
}
491+
483492
public function testServicesResetter()
484493
{
485494
$httpKernelMock = $this->getMockBuilder(HttpKernelInterface::class)
@@ -603,8 +612,9 @@ public function getProjectDir(): string
603612
}
604613
}
605614

606-
class CustomProjectDirKernel extends Kernel
615+
class CustomProjectDirKernel extends Kernel implements WarmableInterface
607616
{
617+
public $warmedUp = false;
608618
private $baseDir;
609619
private $buildContainer;
610620
private $httpKernel;
@@ -631,6 +641,13 @@ public function getProjectDir(): string
631641
return __DIR__.'/Fixtures';
632642
}
633643

644+
public function warmUp(string $cacheDir): array
645+
{
646+
$this->warmedUp = true;
647+
648+
return [];
649+
}
650+
634651
protected function build(ContainerBuilder $container)
635652
{
636653
if ($build = $this->buildContainer) {

0 commit comments

Comments
 (0)
0