8000 [DI] generate preload.php file for PHP 7.4 in cache folder · symfony/symfony@85e1fec · GitHub
[go: up one dir, main page]

Skip to content

Commit 85e1fec

Browse files
[DI] generate preload.php file for PHP 7.4 in cache folder
1 parent 567cb27 commit 85e1fec

File tree

2 files changed

+58
-20
lines changed

2 files changed

+58
-20
lines changed

src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use Symfony\Component\DependencyInjection\LazyProxy\PhpDumper\DumperInterface as ProxyDumper;
3333
use Symfony\Component\DependencyInjection\LazyProxy\PhpDumper\NullDumper;
3434
use Symfony\Component\DependencyInjection\Parameter;
35+
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
3536
use Symfony\Component\DependencyInjection\Reference;
3637
use Symfony\Component\DependencyInjection\ServiceLocator as BaseServiceLocator;
3738
use Symfony\Component\DependencyInjection\TypedReference;
@@ -216,7 +217,7 @@ public function dump(array $options = [])
216217
}
217218

218219
$code =
219-
$this->startClass($options['class'], $baseClass, $baseClassWithNamespace).
220+
$this->startClass($options['class'], $baseClass, $baseClassWithNamespace, $preload).
220221
$this->addServices($services).
221222
$this->addDeprecatedAliases().
222223
$this->addDefaultParametersMethod()
@@ -243,6 +244,10 @@ public function dump(array $options = [])
243244
EOF;
244245
$files = [];
245246

247+
if ($preload) {
248+
$files['preload.php'] = "<?php\n".str_replace("\n include_once ", "\ninclude_once ", $preload)."\n";
249+
}
250+
246251
$ids = $this->container->getRemovedIds();
247252
foreach ($this->container->getDefinitions() as $id => $definition) {
248253
if (!$definition->isPublic()) {
@@ -277,6 +282,34 @@ public function dump(array $options = [])
277282
$time = $options['build_time'];
278283
$id = hash('crc32', $hash.$time);
279284

285+
if ($preload) {
286+
$preload = <<<EOF
287+
<?php
288+
289+
// This file has been auto-generated by the Symfony Dependency Injection Component
290+
// You can reference it in the "opcache.preload" php.ini setting on PHP >= 7.4 when preloading is desired
291+
292+
require __DIR__.'/Container{$hash}/preload.php';
293+
294+
EOF;
295+
296+
$lineage = [];
297+
298+
$this->collectLineage($baseClassWithNamespace, $lineage, true);
299+
$this->collectLineage(EnvPlaceholderParameterBag::class, $lineage);
300+
301+
$lineage[] = "__DIR__.'/Container{$hash}/{$options['class']}.php'";
302+
303+
foreach ($lineage as $file) {
304+
if (!isset($this->inlinedRequires[$file])) {
305+
$this->inlinedRequires[$file] = true;
306+
$preload .= sprintf("include_once %s;\n", preg_replace('#^\\$this->targetDirs\[(\d++)\]#', 'dirname(__DIR__, $1)', $file));
307+
}
308+
}
309+
310+
$code[$options['class'].'.preload.php'] = $preload;
311+
}
312+
280313
$code[$options['class'].'.php'] = <<<EOF
281314
<?php
282315
{$namespaceLine}
@@ -391,15 +424,15 @@ private function connectCircularReferences($sourceId, &$currentPath, &$subPath =
391424
unset($subPath[$sourceId]);
392425
}
393426

394-
private function collectLineage($class, array &$lineage)
427+
private function collectLineage($class, array &$lineage, bool $allowContainer = false)
395428
{
396429
if (isset($lineage[$class])) {
397430
return;
398431
}
399432
if (!$r = $this->container->getReflectionClass($class, false)) {
400433
return;
401434
}
402-
if ($this->container instanceof $class) {
435+
if (!$allowContainer && $this->container instanceof $class) {
403436
return;
404437
}
405438
$file = $r->getFileName();
@@ -408,15 +441,15 @@ private function collectLineage($class, array &$lineage)
408441
}
409442

410443
if ($parent = $r->getParentClass()) {
411-
$this->collectLineage($parent->name, $lineage);
444+
$this->collectLineage($parent->name, $lineage, $allowContainer);
412445
}
413446

414447
foreach ($r->getInterfaces() as $parent) {
415-
$this->collectLineage($parent->name, $lineage);
448+
$this->collectLineage($parent->name, $lineage, $allowContainer);
416449
}
417450

418451
foreach ($r->getTraits() as $parent) {
419-
$this->collectLineage($parent->name, $lineage);
452+
$this->collectLineage($parent->name, $lineage, $allowContainer);
420453
}
421454

422455
$lineage[$class] = substr($exportedFile, 1, -1);
@@ -474,13 +507,14 @@ private function addServiceInclude(string $cId, Definition $definition): string
474507
}
475508

476509
foreach (array_diff_key(array_flip($lineage), $this->inlinedRequires) as $file => $class) {
477-
$code .= sprintf(" include_once %s;\n", $file);
510+
$code .= sprintf(" include_once %s;\n", preg_replace('#^\\$this->targetDirs\[(\d++)\]#', sprintf('\dirname(__DIR__, %d + $1)', $this->asFiles), $file));
478511
}
479512
}
480513

481514
foreach ($this->inlinedDefinitions as $def) {
482515
if ($file = $def->getFile()) {
483-
$code .= sprintf(" include_once %s;\n", $this->dumpValue($file));
516+
$file = $this->dumpValue($file);
517+
$code .= sprintf(" include_once %s;\n", preg_replace('#^\\$this->targetDirs\[(\d++)\]#', sprintf('\dirname(__DIR__, %d + $1)', $this->asFiles), $file));
484518
}
485519
}
486520

@@ -958,7 +992,7 @@ private function addNewInstance(Definition $definition, string $return = '', str
958992
return $return.sprintf('new %s(%s)', $this->dumpLiteralClass($this->dumpValue($class)), implode(', ', $arguments)).$tail;
959993
}
960994

961-
private function startClass(string $class, string $baseClass, string $baseClassWithNamespace): string
995+
private function startClass(string $class, string $baseClass, string $baseClassWithNamespace, ?string &$preload): string
962996
{
963997
$namespaceLine = !$this->asFiles && $this->namespace ? "\nnamespace {$this->namespace};\n" : '';
964998

@@ -1026,7 +1060,11 @@ public function __construct()
10261060
$code .= $this->addMethodMap();
10271061
$code .= $this->asFiles ? $this->addFileMap() : '';
10281062
$code .= $this->addAliases();
1029-
$code .= $this->addInlineRequires();
1063+
1064+
if ($preload = $this->getInlineRequires()) {
1065+
$code .= sprintf("\n \$this->privates['service_container'] = static function () {%s\n };\n", $this->asFiles ? "\n require_once __DIR__.'/preload.php';" : $preload);
1066+
}
1067+
10301068
$code .= <<<EOF
10311069
}
10321070
@@ -1227,7 +1265,7 @@ protected function {$methodNameAlias}()
12271265
return $code;
12281266
}
12291267

1230-
private function addInlineRequires(): string
1268+
private function getInlineRequires(): string
12311269
{
12321270
if (!$this->hotPathTag || !$this->inlineRequires) {
12331271
return '';
@@ -1251,11 +1289,11 @@ private function addInlineRequires(): string
12511289
foreach ($lineage as $file) {
12521290
if (!isset($this->inlinedRequires[$file])) {
12531291
$this->inlinedRequires[$file] = true;
1254-
$code .= sprintf("\n include_once %s;", $file);
1292+
$code .= sprintf("\n include_once %s;", preg_replace('#^\\$this->targetDirs\[(\d++)\]#', sprintf('\dirname(__DIR__, %d + $1)', $this->asFiles), $file));
12551293
}
12561294
}
12571295

1258-
return $code ? sprintf("\n \$this->privates['service_container'] = function () {%s\n };\n", $code) : '';
1296+
return $code;
12591297
}
12601298

12611299
private function addDefaultParametersMethod(): string

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_inline_requires.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ public function __construct()
3636

3737
$this->aliases = [];
3838

39-
$this->privates['service_container'] = function () {
40-
include_once $this->targetDirs[1].'/includes/HotPath/I1.php';
41-
include_once $this->targetDirs[1].'/includes/HotPath/P1.php';
42-
include_once $this->targetDirs[1].'/includes/HotPath/T1.php';
43-
include_once $this->targetDirs[1].'/includes/HotPath/C1.php';
39+
$this->privates['service_container'] = static function () {
40+
include_once \dirname(__DIR__, 0 + 1).'/includes/HotPath/I1.php';
41+
include_once \dirname(__DIR__, 0 + 1).'/includes/HotPath/P1.php';
42+
include_once \dirname(__DIR__, 0 + 1).'/includes/HotPath/T1.php';
43+
include_once \dirname(__DIR__, 0 + 1).'/includes/HotPath/C1.php';
4444
};
4545
}
4646

@@ -90,8 +90,8 @@ protected function getC1Service()
9090
*/
9191
protected function getC2Service()
9292
{
93-
include_once $this->targetDirs[1].'/includes/HotPath/C2.php';
94-
include_once $this->targetDirs[1].'/includes/HotPath/C3.php';
93+
include_once \dirname(__DIR__, 0 + 1).'/includes/HotPath/C2.php';
94+
include_once \dirname(__DIR__, 0 + 1).'/includes/HotPath/C3.php';
9595

9696
return $this->services['Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\includes\\HotPath\\C2'] = new \Symfony\Component\DependencyInjection\Tests\Fi 3EEE xtures\includes\HotPath\C2(new \Symfony\Component\DependencyInjection\Tests\Fixtures\includes\HotPath\C3());
9797
}

0 commit comments

Comments
 (0)
0