Skip to content

Commit fbb92e9

Browse files
[HttpKernel] allow cache warmers to add to the list of preloaded classes and files
1 parent 9591cba commit fbb92e9

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ CHANGELOG
1212
* updated the signature of method `DeprecateTrait::deprecate()` to `DeprecateTrait::deprecation(string $package, string $version, string $message)`
1313
* deprecated the `Psr\Container\ContainerInterface` and `Symfony\Component\DependencyInjection\ContainerInterface` aliases of the `service_container` service,
1414
configure them explicitly instead
15+
* added class `Symfony\Component\DependencyInjection\Dumper\Preloader` to help with preloading on PHP 7.4+
1516

1617
5.0.0
1718
-----

Dumper/Preloader.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,31 @@
1313

1414
/**
1515
* @author Nicolas Grekas <[email protected]>
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) {

0 commit comments

Comments
 (0)