[HttpKernel] make kernels implementing WarmableInterface be part of the cache warmup stage

This commit is contained in:
Nicolas Grekas 2020-04-27 13:15:27 +02:00
parent 0633308bb3
commit 649e530356
3 changed files with 27 additions and 6 deletions

View File

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

View File

@ -35,6 +35,7 @@ use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
use Symfony\Component\HttpKernel\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\AddAnnotatedClassesToCachePass;
use Symfony\Component\HttpKernel\DependencyInjection\MergeExtensionConfigurationPass;
@ -566,12 +567,14 @@ abstract class Kernel implements KernelInterface, RebootableInterface, Terminabl
touch($oldContainerDir.'.legacy');
}
if ($this->container->has('cache_warmer')) {
$preload = (array) $this->container->get('cache_warmer')->warmUp($this->container->getParameter('kernel.cache_dir'));
$preload = $this instanceof WarmableInterface ? (array) $this->warmUp($this->container->getParameter('kernel.cache_dir')) : [];
if ($preload && method_exists(Preloader::class, 'append') && file_exists($preloadFile = $cacheDir.'/'.$class.'.preload.php')) {
Preloader::append($preloadFile, $preload);
}
if ($this->container->has('cache_warmer')) {
$preload = array_merge($preload, (array) $this->container->get('cache_warmer')->warmUp($this->container->getParameter('kernel.cache_dir')));
}
if ($preload && method_exists(Preloader::class, 'append') && file_exists($preloadFile = $cacheDir.'/'.$class.'.preload.php')) {
Preloader::append($preloadFile, $preload);
}
}

View File

@ -19,6 +19,7 @@ use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
use Symfony\Component\HttpKernel\DependencyInjection\ResettableServicePass;
use Symfony\Component\HttpKernel\DependencyInjection\ServicesResetter;
use Symfony\Component\HttpKernel\HttpKernelInterface;
@ -480,6 +481,14 @@ EOF;
$this->assertTrue($kernel->getContainer()->getParameter('test.processed'));
}
public function testWarmup()
{
$kernel = new CustomProjectDirKernel();
$kernel->boot();
$this->assertTrue($kernel->warmedUp);
}
public function testServicesResetter()
{
$httpKernelMock = $this->getMockBuilder(HttpKernelInterface::class)
@ -603,8 +612,9 @@ class TestKernel implements HttpKernelInterface
}
}
class CustomProjectDirKernel extends Kernel
class CustomProjectDirKernel extends Kernel implements WarmableInterface
{
public $warmedUp = false;
private $baseDir;
private $buildContainer;
private $httpKernel;
@ -631,6 +641,13 @@ class CustomProjectDirKernel extends Kernel
return __DIR__.'/Fixtures';
}
public function warmUp(string $cacheDir): array
{
$this->warmedUp = true;
return [];
}
protected function build(ContainerBuilder $container)
{
if ($build = $this->buildContainer) {