8000 [HttpKernel] allow cache warmers to add to the list of preloaded clas… · symfony/symfony@1735544 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1735544

Browse files
[HttpKernel] allow cache warmers to add to the list of preloaded classes and files
1 parent 4dabd00 commit 1735544

File tree

24 files changed

+150
-26
lines changed

24 files changed

+150
-26
lines changed

UPGRADE-5.1.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ HttpFoundation
3939
`__construct()` instead)
4040
* Made the Mime component an optional dependency
4141

42+
HttpKernel
43+
----------
44+
45+
* Made `WarmableInterface::warmUp()` return a list of classes or files to preload on PHP 7.4+
46+
not returning an array is deprecated
47+
4248
Mailer
4349
------
4450

UPGRADE-6.0.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ HttpFoundation
3636
`RedirectResponse::create()`, and `StreamedResponse::create()` methods (use
3737
`__construct()` instead)
3838

39+
HttpKernel
40+
----------
41+
42+
* Made `WarmableInterface::warmUp()` return a list of classes or files to preload on PHP 7.4+
43+
3944
Messenger
4045
---------
4146

src/Symfony/Bridge/Doctrine/CacheWarmer/ProxyCacheWarmer.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,12 @@ public function isOptional()
4343

4444
/**
4545
* {@inheritdoc}
46+
*
47+
* @return string[] A list of files to preload on PHP 7.4+
4648
*/
4749
public function warmUp(string $cacheDir)
4850
{
51+
$files = [];
4952
foreach ($this->registry->getManagers() as $em) {
5053
// we need the directory no matter the proxy cache generation strategy
5154
if (!is_dir($proxyCacheDir = $em->getConfiguration()->getProxyDir())) {
@@ -64,6 +67,14 @@ public function warmUp(string $cacheDir)
6467
$classes = $em->getMetadataFactory()->getAllMetadata();
6568

6669
$em->getProxyFactory()->generateProxyClasses($classes);
70+
71+
foreach (scandir($proxyCacheDir) as $file) {
72+
if (!is_dir($file = $proxyCacheDir.'/'.$file)) {
73+
$files[] = $file;
74+
}
75+
}
6776
}
77+
78+
return $files;
6879
}
6980
}

src/Symfony/Bundle/FrameworkBundle/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
/**

src/Symfony/Bundle/FrameworkBundle/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
/**

src/Symfony/Bundle/FrameworkBundle/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));

src/Symfony/Bundle/FrameworkBundle/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
/**

src/Symfony/Bundle/FrameworkBundle/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
/**

src/Symfony/Bundle/FrameworkBundle/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

src/Symfony/Bundle/FrameworkBundle/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
/**

0 commit comments

Comments
 (0)
0