Skip to content

Commit 948d1b9

Browse files
[HttpKernel] allow cache warmers to add to the list of preloaded classes and files
1 parent e88e25a commit 948d1b9

File tree

8 files changed

+46
-15
lines changed

8 files changed

+46
-15
lines changed

CacheWarmer/AbstractPhpFileCacheWarmer.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ public function isOptional()
4242

4343
/**
4444
* {@inheritdoc}
45+
*
46+
* @return string[] A list of classes to preload on PHP 7.4+
4547
*/
4648
public function warmUp(string $cacheDir)
4749
{
@@ -61,12 +63,15 @@ public function warmUp(string $cacheDir)
6163
// so here we un-serialize the values first
6264
$values = array_map(function ($val) { return null !== $val ? unserialize($val) : null; }, $arrayAdapter->getValues());
6365

64-
$this->warmUpPhpArrayAdapter(new PhpArrayAdapter($this->phpArrayFile, new NullAdapter()), $values);
66+
return $this->warmUpPhpArrayAdapter(new PhpArrayAdapter($this->phpArrayFile, new NullAdapter()), $values);
6567
}
6668

69+
/**
70+
* @return string[] A list of classes to preload on PHP 7.4+
71+
*/
6772
protected function warmUpPhpArrayAdapter(PhpArrayAdapter $phpArrayAdapter, array $values)
6873
{
69-
$phpArrayAdapter->warmUp($values);
74+
return (array) $phpArrayAdapter->warmUp($values);
7075
}
7176

7277
/**

CacheWarmer/CachePoolClearerCacheWarmer.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,18 @@ public function __construct(Psr6CacheClearer $poolClearer, array $pools = [])
3636

3737
/**
3838
* {@inheritdoc}
39+
*
40+
* @return string[]
3941
*/
40-
public function warmUp($cacheDirectory): void
42+
public function warmUp($cacheDirectory): array
4143
{
4244
foreach ($this->pools as $pool) {
4345
if ($this->poolClearer->hasPool($pool)) {
4446
$this->poolClearer->clearPool($pool);
4547
}
4648
}
49+
50+
return [];
4751
}
4852

4953
/**

CacheWarmer/RouterCacheWarmer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ public function __construct(ContainerInterface $container)
3636

3737
/**
3838
* {@inheritdoc}
39+
*
40+
* @return string[]
3941
*/
4042
public function warmUp(string $cacheDir)
4143
{
4244
$router = $this->container->get('router');
4345

4446
if ($router instanceof WarmableInterface) {
45-
$router->warmUp($cacheDir);
46-
47-
return;
47+
return (array) $router->warmUp($cacheDir);
4848
}
4949

5050
throw new \LogicException(sprintf('The router "%s" cannot be warmed up because it does not implement "%s".', get_debug_type($router), WarmableInterface::class));

CacheWarmer/TranslationsCacheWarmer.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public function __construct(ContainerInterface $container)
3535

3636
/**
3737
* {@inheritdoc}
38+
*
39+
* @return string[]
3840
*/
3941
public function warmUp(string $cacheDir)
4042
{
@@ -43,8 +45,10 @@ public function warmUp(string $cacheDir)
4345
}
4446

4547
if ($this->translator instanceof WarmableInterface) {
46-
$this->translator->warmUp($cacheDir);
48+
return (array) $this->translator->warmUp($cacheDir);
4749
}
50+
51+
return [];
4852
}
4953

5054
/**

CacheWarmer/ValidatorCacheWarmer.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,13 @@ protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter)
6868
return true;
6969
}
7070

71+
/**
72+
* @return string[] A list of classes to preload on PHP 7.4+
73+
*/
7174
protected function warmUpPhpArrayAdapter(PhpArrayAdapter $phpArrayAdapter, array $values)
7275
{
7376
// make sure we don't cache null values
74-
parent::warmUpPhpArrayAdapter($phpArrayAdapter, array_filter($values));
77+
return parent::warmUpPhpArrayAdapter($phpArrayAdapter, array_filter($values));
7578
}
7679

7780
/**

Command/CacheClearCommand.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Console\Input\InputOption;
1818
use Symfony\Component\Console\Output\OutputInterface;
1919
use Symfony\Component\Console\Style\SymfonyStyle;
20+
use Symfony\Component\DependencyInjection\Dumper\Preloader;
2021
use Symfony\Component\EventDispatcher\EventDispatcher;
2122
use Symfony\Component\Filesystem\Exception\IOException;
2223
use Symfony\Component\Filesystem\Filesystem;
@@ -117,7 +118,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
117118
$warmer = $kernel->getContainer()->get('cache_warmer');
118119
// non optional warmers already ran during container compilation
119120
$warmer->enableOnlyOptionalWarmers();
120-
$warmer->warmUp($realCacheDir);
121+
$preload = (array) $warmer->warmUp($warmupDir);
122+
123+
if (file_exists($preloadFile = $warmupDir.'/'.$kernel->getContainer()->getParameter('kernel.container_class').'.preload.php')) {
124+
Preloader::append($preloadFile, $preload);
125+
}
121126
}
122127
} else {
123128
$fs->mkdir($warmupDir);
@@ -193,7 +198,11 @@ private function warmup(string $warmupDir, string $realCacheDir, bool $enableOpt
193198
$warmer = $kernel->getContainer()->get('cache_warmer');
194199
// non optional warmers already ran during container compilation
195200
$warmer->enableOnlyOptionalWarmers();
196-
$warmer->warmUp($warmupDir);
201+
$preload = (array) $warmer->warmUp($warmupDir);
202+
203+
if (file_exists($preloadFile = $warmupDir.'/'.$kernel->getContainer()->getParameter('kernel.container_class').'.preload.php')) {
204+
Preloader::append($preloadFile, $preload);
205+
}
197206
}
198207

199208
// fix references to cached files with the real cache directory name

Routing/Router.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,11 @@
2121
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
2222
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
2323
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
24-
use Symfony\Component\Routing\Annotation\Route;
2524
use Symfony\Component\Routing\RequestContext;
2625
use Symfony\Component\Routing\RouteCollection;
2726
use Symfony\Component\Routing\Router as BaseRouter;
2827
use Symfony\Contracts\Service\ServiceSubscriberInterface;
2928

30-
// Help opcache.preload discover always-needed symbols
31-
class_exists(RedirectableCompiledUrlMatcher::class);
32-
class_exists(Route::class);
33-
3429
/**
3530
* This Router creates the Loader only when the cache is empty.
3631
*
@@ -90,6 +85,8 @@ public function getRouteCollection()
9085

9186
/**
9287
* {@inheritdoc}
88+
*
89+
* @return string[] A list of classes to preload on PHP 7.4+
9390
*/
9491
public function warmUp(string $cacheDir)
9592
{
@@ -101,6 +98,11 @@ public function warmUp(string $cacheDir)
10198
$this->getGenerator();
10299

103100
$this->setOption('cache_dir', $currentDir);
101+
102+
return [
103+
$this->getOption('generator_class'),
104+
$this->getOption('matcher_class'),
105+
];
104106
}
105107

106108
/**

Translation/Translator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ public function __construct(ContainerInterface $container, MessageFormatterInter
9595

9696
/**
9797
* {@inheritdoc}
98+
*
99+
* @return string[]
98100
*/
99101
public function warmUp(string $cacheDir)
100102
{
@@ -113,6 +115,8 @@ public function warmUp(string $cacheDir)
113115

114116
$this->loadCatalogue($locale);
115117
}
118+
119+
return [];
116120
}
117121

118122
public function addResource(string $format, $resource, string $locale, string $domain = null)

0 commit comments

Comments
 (0)