Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit dded411

Browse filesBrowse files
[HttpKernel] allow cache warmers to add to the list of preloaded classes and files
1 parent 4dabd00 commit dded411
Copy full SHA for dded411

File tree

24 files changed

+150
-26
lines changed
Filter options

24 files changed

+150
-26
lines changed

‎UPGRADE-5.1.md

Copy file name to clipboardExpand all lines: UPGRADE-5.1.md
+6Lines 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

Copy file name to clipboardExpand all lines: UPGRADE-6.0.md
+5Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/CacheWarmer/ProxyCacheWarmer.php
+11Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/CacheWarmer/AbstractPhpFileCacheWarmer.php
+7-2Lines 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 $phpArrayAdapter->warmUp($values);
7075
}
7176

7277
/**

‎src/Symfony/Bundle/FrameworkBundle/CacheWarmer/CachePoolClearerCacheWarmer.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/CacheWarmer/CachePoolClearerCacheWarmer.php
+5-1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/CacheWarmer/RouterCacheWarmer.php
+3-3Lines 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 $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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/CacheWarmer/TranslationsCacheWarmer.php
+5-1Lines 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 $this->translator->warmUp($cacheDir);
4749
}
50+
51+
return [];
4852
}
4953

5054
/**

‎src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ValidatorCacheWarmer.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ValidatorCacheWarmer.php
+4-1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php
+11-2Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Routing/Router.php
+7-5Lines 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
/**

‎src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php
+4Lines 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)

‎src/Symfony/Bundle/SecurityBundle/CacheWarmer/ExpressionCacheWarmer.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/SecurityBundle/CacheWarmer/ExpressionCacheWarmer.php
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,15 @@ public function isOptional()
3434
return true;
3535
}
3636

37+
/**
38+
* @return string[]
39+
*/
3740
public function warmUp(string $cacheDir)
3841
{
3942
foreach ($this->expressions as $expression) {
4043
$this->expressionLanguage->parse($expression, ['token', 'user', 'object', 'subject', 'roles', 'request', 'trust_resolver']);
4144
}
45+
46+
return [];
4247
}
4348
}

‎src/Symfony/Bundle/TwigBundle/CacheWarmer/TemplateCacheWarmer.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/TwigBundle/CacheWarmer/TemplateCacheWarmer.php
+11-1Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,31 @@ public function __construct(ContainerInterface $container, iterable $iterator)
3737

3838
/**
3939
* {@inheritdoc}
40+
*
41+
* @return string[] A list of template files to preload on PHP 7.4+
4042
*/
4143
public function warmUp(string $cacheDir)
4244
{
4345
if (null === $this->twig) {
4446
$this->twig = $this->container->get('twig');
4547
}
4648

49+
$files = [];
50+
4751
foreach ($this->iterator as $template) {
4852
try {
49-
$this->twig->load($template);
53+
$template = $this->twig->load($template);
54+
55+
if (\is_callable([$template, 'unwrap'])) {
56+
$files[] = (new \ReflectionClass($template->unwrap()))->getFileName();
57+
}
5058
} catch (Error $e) {
5159
// problem during compilation, give up
5260
// might be a syntax error or a non-Twig template
5361
}
5462
}
63+
64+
return $files;
5565
}
5666

5767
/**

‎src/Symfony/Component/Cache/Adapter/PhpArrayAdapter.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Cache/Adapter/PhpArrayAdapter.php
+6-1Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,8 @@ public function clear(string $prefix = '')
291291
* Store an array of cached values.
292292
*
293293
* @param array $values The cached values
294+
*
295+
* @return string[] A list of classes to preload on PHP 7.4+
294296
*/
295297
public function warmUp(array $values)
296298
{
@@ -314,6 +316,7 @@ public function warmUp(array $values)
314316
}
315317
}
316318

319+
$preload = [];
317320
$dumpedValues = '';
318321
$dumpedMap = [];
319322
$dump = <<<'EOF'
@@ -334,7 +337,7 @@ public function warmUp(array $values)
334337
$value = "'N;'";
335338
} elseif (\is_object($value) || \is_array($value)) {
336339
try {
337-
$value = VarExporter::export($value, $isStaticValue);
340+
$value = VarExporter::export($value, $isStaticValue, $preload);
338341
} catch (\Exception $e) {
339342
throw new InvalidArgumentException(sprintf('Cache key "%s" has non-serializable "%s" value.', $key, get_debug_type($value)), 0, $e);
340343
}
@@ -376,6 +379,8 @@ public function warmUp(array $values)
376379
unset(self::$valuesCache[$this->file]);
377380

378381
$this->initialize();
382+
383+
return $preload;
379384
}
380385

381386
/**

‎src/Symfony/Component/DependencyInjection/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CHANGELOG
77
* added support to autowire public typed properties in php 7.4
88
* added support for defining method calls, a configurator, and property setters in `InlineServiceConfigurator`
99
* added possibility to define abstract service arguments
10+
* added class `Symfony\Component\DependencyInjection\Dumper\Preloader` to help with preloading on PHP 7.4+
1011

1112
5.0.0
1213
-----

‎src/Symfony/Component/DependencyInjection/Dumper/Preloader.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Dumper/Preloader.php
+23-4Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,31 @@
1313

1414
/**
1515
* @author Nicolas Grekas <p@tchwork.com>
16-
*
17-
* @internal
1816
*/
19-
class Preloader
17+
final class Preloader
2018
{
21-
public static function preload(array $classes)
19+
public static function append(string $file, array $list): void
20+
{
21+
if (!file_exists($file)) {
22+
throw new \LogicException(sprintf('File "%s" does not exist.', $file));
23+
}
24+
25+
$cacheDir = \dirname($file);
26+
$classes = [];
27+
28+
foreach ($list as $item) {
29+
if (0 === strpos($item, $cacheDir)) {
30+
file_put_contents($file, sprintf("require __DIR__.%s;\n", var_export(substr($item, \strlen($cacheDir)), true)), FILE_APPEND);
31+
continue;
32+
}
33+
34+
$classes[] = sprintf("\$classes[] = %s;\n", var_export($item, true));
35+
}
36+
37+
file_put_contents($file, sprintf("\n\$classes = [];\n%sPreloader::preload(\$classes);\n", implode('', $classes)), FILE_APPEND);
38+
}
39+
40+
public static function preload(array $classes): void
2241
{
2342
set_error_handler(function ($t, $m, $f, $l) {
2443
if (error_reporting() & $t) {

‎src/Symfony/Component/HttpKernel/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/CHANGELOG.md
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ CHANGELOG
66

77
* allowed using public aliases to reference controllers
88
* added session usage reporting when the `_stateless` attribute of the request is set to `true`
9+
* made `WarmableInterface::warmUp()` return a list of classes or files to preload on PHP 7.4+;
10+
not returning an array is deprecated
911

1012
5.0.0
1113
-----

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.