Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #32032 [DI] generate preload.php file for PHP 7.4 in cache fo…
…lder (nicolas-grekas) This PR was merged into the 4.4 branch. Discussion ---------- [DI] generate preload.php file for PHP 7.4 in cache folder | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #29105 | License | MIT | Doc PR | - This PR makes the PhpDumper generate a preloading file suited for PHP 7.4. On a skeleton app, the generated file is `var/cache/dev/srcApp_KernelDevDebugContainer.preload.php` (of course, this varies by env name + kernel class) One missing thing is listing some classes that are always needed but are not related to services. Typically: `Request` and `Response`. We might need a new mechanism to make this list extensible. I did not measure the benefit of this on PHP 7.4. I would really appreciate if someone could give it a try on PHP 7.4 with preloading enabled. Commits ------- c4dad0d [DI] generate preload.php file for PHP 7.4 in cache folder
- Loading branch information
Showing
8 changed files
with
175 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
src/Symfony/Component/DependencyInjection/Dumper/Preloader.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
|
||
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.