-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DI] generate preload.php file for PHP 7.4 in cache folder #32032
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,6 +82,7 @@ class PhpDumper extends Dumper | |
private $locatedIds = []; | ||
private $serviceLocatorTag; | ||
private $exportedVariables = []; | ||
private $baseClass; | ||
|
||
/** | ||
* @var ProxyDumper | ||
|
@@ -151,11 +152,11 @@ public function dump(array $options = []) | |
|
||
if (0 !== strpos($baseClass = $options['base_class'], '\\') && 'Container' !== $baseClass) { | ||
$baseClass = sprintf('%s\%s', $options['namespace'] ? '\\'.$options['namespace'] : '', $baseClass); | ||
$baseClassWithNamespace = $baseClass; | ||
$this->baseClass = $baseClass; | ||
} elseif ('Container' === $baseClass) { | ||
$baseClassWithNamespace = Container::class; | ||
$this->baseClass = Container::class; | ||
} else { | ||
$baseClassWithNamespace = $baseClass; | ||
$this->baseClass = $baseClass; | ||
} | ||
|
||
$this->initializeMethodNamesMap('Container' === $baseClass ? Container::class : $baseClass); | ||
|
@@ -222,7 +223,7 @@ public function dump(array $options = []) | |
$proxyClasses = $this->inlineFactories ? $this->generateProxyClasses() : null; | ||
|
||
$code = | ||
$this->startClass($options['class'], $baseClass, $baseClassWithNamespace). | ||
$this->startClass($options['class'], $baseClass, $preload). | ||
$this->addServices($services). | ||
$this->addDeprecatedAliases(). | ||
$this->addDefaultParametersMethod() | ||
|
@@ -296,6 +297,33 @@ public function dump(array $options = []) | |
$time = $options['build_time']; | ||
$id = hash('crc32', $hash.$time); | ||
|
||
if ($preload) { | ||
$code[$options['class'].'.preload.php'] = <<<EOF | ||
<?php | ||
|
||
// This file has been auto-generated by the Symfony Dependency Injection Component | ||
// You can reference it in the "opcache.preload" php.ini setting on PHP >= 7.4 when preloading is desired | ||
|
||
use Symfony\Component\DependencyInjection\Dumper\Preloader; | ||
|
||
require dirname(__DIR__, 3).'/vendor/autoload.php'; | ||
require __DIR__.'/Container{$hash}/{$options['class']}.php'; | ||
|
||
\$classes = []; | ||
|
||
EOF; | ||
|
||
foreach ($preload as $class) { | ||
$code[$options['class'].'.preload.php'] .= sprintf("\$classes[] = '%s';\n", $class); | ||
} | ||
|
||
$code[$options['class'].'.preload.php'] .= <<<'EOF' | ||
|
||
Preloader::preload($classes); | ||
|
||
EOF; | ||
} | ||
|
||
$code[$options['class'].'.php'] = <<<EOF | ||
<?php | ||
{$namespaceLine} | ||
|
@@ -426,14 +454,16 @@ private function collectLineage(string $class, array &$lineage) | |
if (!$r = $this->container->getReflectionClass($class, false)) { | ||
return; | ||
} | ||
if ($this->container instanceof $class) { | ||
if (is_a($class, $this->baseClass, true)) { | ||
return; | ||
} | ||
$file = $r->getFileName(); | ||
if (!$file || $this->doExport($file) === $exportedFile = $this->export($file)) { | ||
return; | ||
} | ||
|
||
$lineage[$class] = substr($exportedFile, 1, -1); | ||
|
||
if ($parent = $r->getParentClass()) { | ||
$this->collectLineage($parent->name, $lineage); | ||
} | ||
|
@@ -446,6 +476,7 @@ private function collectLineage(string $class, array &$lineage) | |
$this->collectLineage($parent->name, $lineage); | ||
} | ||
|
||
unset($lineage[$class]); | ||
$lineage[$class] = substr($exportedFile, 1, -1); | ||
} | ||
|
||
|
@@ -522,13 +553,17 @@ private function addServiceInclude(string $cId, Definition $definition): string | |
} | ||
|
||
foreach (array_diff_key(array_flip($lineage), $this->inlinedRequires) as $file => $class) { | ||
$file = preg_replace('#^\\$this->targetDirs\[(\d++)\]#', sprintf('\dirname(__DIR__, %d + $1)', $this->asFiles), $file); | ||
$code .= sprintf(" include_once %s;\n", $file); | ||
} | ||
} | ||
|
||
foreach ($this->inlinedDefinitions as $def) { | ||
if ($file = $def->getFile()) { | ||
$code .= sprintf(" include_once %s;\n", $this->dumpValue($file)); | ||
$file = $this->dumpValue($file); | ||
$file = '(' === $file[0] ? substr($file, 1, -1) : $file; | ||
$file = preg_replace('#^\\$this->targetDirs\[(\d++)\]#', sprintf('\dirname(__DIR__, %d + $1)', $this->asFiles), $file); | ||
$code .= sprintf(" include_once %s;\n", $file); | ||
} | ||
} | ||
|
||
|
@@ -1016,7 +1051,7 @@ private function addNewInstance(Definition $definition, string $return = '', str | |
return $return.sprintf('new %s(%s)', $this->dumpLiteralClass($this->dumpValue($class)), implode(', ', $arguments)).$tail; | ||
} | ||
|
||
private function startClass(string $class, string $baseClass, string $baseClassWithNamespace): string | ||
private function startClass(string $class, string $baseClass, ?array &$preload): string | ||
{ | ||
$namespaceLine = !$this->asFiles && $this->namespace ? "\nnamespace {$this->namespace};\n" : ''; | ||
|
||
|
@@ -1064,8 +1099,8 @@ public function __construct() | |
$code .= " \$this->containerDir = \$containerDir;\n"; | ||
} | ||
|
||
if (Container::class !== $baseClassWithNamespace) { | ||
$r = $this->container->getReflectionClass($baseClassWithNamespace, false); | ||
if (Container::class !== $this->baseClass) { | ||
$r = $this->container->getReflectionClass($this->baseClass, false); | ||
if (null !== $r | ||
&& (null !== $constructor = $r->getConstructor()) | ||
&& 0 === $constructor->getNumberOfRequiredParameters() | ||
|
@@ -1085,7 +1120,7 @@ public function __construct() | |
$code .= $this->addMethodMap(); | ||
$code .= $this->asFiles && !$this->inlineFactories ? $this->addFileMap() : ''; | ||
$code .= $this->addAliases(); | ||
$code .= $this->addInlineRequires(); | ||
$code .= $this->addInlineRequires($preload); | ||
$code .= <<<EOF | ||
} | ||
|
||
|
@@ -1285,7 +1320,7 @@ protected function {$methodNameAlias}() | |
return $code; | ||
} | ||
|
||
private function addInlineRequires(): string | ||
private function addInlineRequires(?array &$preload): string | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to allow calling the method with an uninitialized variable |
||
{ | ||
if (!$this->hotPathTag || !$this->inlineRequires) { | ||
return ''; | ||
|
@@ -1304,6 +1339,7 @@ private function addInlineRequires(): string | |
|
||
foreach ($inlinedDefinitions as $def) { | ||
if (\is_string($class = \is_array($factory = $def->getFactory()) && \is_string($factory[0]) ? $factory[0] : $def->getClass())) { | ||
$preload[$class] = $class; | ||
$this->collectLineage($class, $lineage); | ||
} | ||
} | ||
|
@@ -1314,11 +1350,12 @@ private function addInlineRequires(): string | |
foreach ($lineage as $file) { | ||
if (!isset($this->inlinedRequires[$file])) { | ||
$this->inlinedRequires[$file] = true; | ||
$file = preg_replace('#^\\$this->targetDirs\[(\d++)\]#', sprintf('\dirname(__DIR__, %d + $1)', $this->asFiles), $file); | ||
$code .= sprintf("\n include_once %s;", $file); | ||
} | ||
} | ||
|
||
return $code ? sprintf("\n \$this->privates['service_container'] = function () {%s\n };\n", $code) : ''; | ||
return $code ? sprintf("\n \$this->privates['service_container'] = static function () {%s\n };\n", $code) : ''; | ||
} | ||
|
||
private function addDefaultParametersMethod(): string | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\DependencyInjection\Dumper; | ||
|
||
/** | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
* | ||
* @internal | ||
*/ | ||
class Preloader | ||
{ | ||
public static function preload(array $classes) | ||
{ | ||
set_error_handler(function ($t, $m, $f, $l) { | ||
if (error_reporting() & $t) { | ||
if (__FILE__ !== $f) { | ||
throw new \ErrorException($m, 0, $t, $f, $l); | ||
} | ||
|
||
throw new \ReflectionException($m); | ||
} | ||
}); | ||
|
||
$prev = []; | ||
$preloaded = []; | ||
|
||
try { | ||
while ($prev !== $classes) { | ||
$prev = $classes; | ||
foreach ($classes as $c) { | ||
if (!isset($preloaded[$c])) { | ||
$preloaded[$c] = true; | ||
self::doPreload($c); | ||
} | ||
} | ||
$classes = array_merge(get_declared_classes(), get_declared_interfaces(), get_declared_traits()); | ||
} | ||
} finally { | ||
restore_error_handler(); | ||
} | ||
} | ||
|
||
private static function doPreload(string $class) | ||
{ | ||
if (\in_array($class, ['self', 'static', 'parent'], true)) { | ||
return; | ||
} | ||
|
||
try { | ||
$r = new \ReflectionClass($class); | ||
|
||
if ($r->isInternal()) { | ||
return; | ||
} | ||
|
||
$r->getConstants(); | ||
$r->getDefaultProperties(); | ||
|
||
if (\PHP_VERSION_ID >= 70400) { | ||
foreach ($r->getProperties() as $p) { | ||
if (($t = $p->getType()) && !$t->isBuiltin()) { | ||
self::doPreload($t->getName()); | ||
} | ||
} | ||
} | ||
|
||
foreach ($r->getMethods() as $m) { | ||
foreach ($m->getParameters() as $p) { | ||
if ($p->isDefaultValueAvailable() && $p->isDefaultValueConstant()) { | ||
$c = $p->getDefaultValueConstantName(); | ||
|
||
if ($i = strpos($c, '::')) { | ||
self::doPreload(substr($c, 0, $i)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if you reuse the same class multiple types in different parameters, wouldn't this preload it multiple times ? Also, it would not register it in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed, fixed in #33525 |
||
} | ||
} | ||
|
||
if (($t = $p->getType()) && !$t->isBuiltin()) { | ||
self::doPreload($t->getName()); | ||
} | ||
} | ||
|
||
if (($t = $m->getReturnType()) && !$t->isBuiltin()) { | ||
self::doPreload($t->getName()); | ||
} | ||
} | ||
} catch (\ReflectionException $e) { | ||
// ignore missing classes | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why generating code using
$this->targetDirs
first, to replace it later withdirname(__DIR__)
? We would generate the right code directly.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer not changing this when it's not needed: the code starts with
$this->targetDirs[0] = \dirname($containerDir)
: target dirs are dynamic. This is used when calling cache clear and is stable, no reason to change.Using
dirname()
is really useful when referencing files in thesrc/
or in thevendor/
folder, which is where it is used.Lowest risk strategy :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Second look, I did something, please check #33529 :)