Skip to content

Commit e066329

Browse files
bug #59713 [DependencyInjection] Do not preload functions (biozshock)
This PR was squashed before being merged into the 6.4 branch. Discussion ---------- [DependencyInjection] Do not preload functions | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | License | MIT Ensure `preload.php` does not have functions in preloaded class names: ``` $ grep "current" var/cache/prod/App_KernelProdContainer.preload.php $classes[] = 'current'; ``` Happens because the DI declares a factory, which uses built-in `\current`: https://github.com/symfony/symfony/blob/v6.4.18/src/Symfony/Bundle/FrameworkBundle/Resources/config/http_client.php#L39 And then the "class" is added by https://github.com/symfony/symfony/blob/v6.4.18/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php#L2317 Similar to the #36866 Commits ------- b52b760dff2 [DependencyInjection] Do not preload functions
2 parents 3f58cc9 + 495e951 commit e066329

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

Dumper/PhpDumper.php

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ class %s extends {$options['class']}
353353
EOF;
354354

355355
foreach ($this->preload as $class) {
356-
if (!$class || str_contains($class, '$') || \in_array($class, ['int', 'float', 'string', 'bool', 'resource', 'object', 'array', 'null', 'callable', 'iterable', 'mixed', 'void'], true)) {
356+
if (!$class || str_contains($class, '$') || \in_array($class, ['int', 'float', 'string', 'bool', 'resource', 'object', 'array', 'null', 'callable', 'iterable', 'mixed', 'void', 'never'], true)) {
357357
continue;
358358
}
359359
if (!(class_exists($class, false) || interface_exists($class, false) || trait_exists($class, false)) || (new \ReflectionClass($class))->isUserDefined()) {
@@ -846,8 +846,7 @@ private function addService(string $id, Definition $definition): array
846846
if ($class = $definition->getClass()) {
847847
$class = $class instanceof Parameter ? '%'.$class.'%' : $this->container->resolveEnvPlaceholders($class);
848848
$return[] = sprintf(str_starts_with($class, '%') ? '@return object A %1$s instance' : '@return \%s', ltrim($class, '\\'));
849-
} elseif ($definition->getFactory()) {
850-
$factory = $definition->getFactory();
849+
} elseif ($factory = $definition->getFactory()) {
851850
if (\is_string($factory) && !str_starts_with($factory, '@=')) {
852851
$return[] = sprintf('@return object An instance returned by %s()', $factory);
853852
} elseif (\is_array($factory) && (\is_string($factory[0]) || $factory[0] instanceof Definition || $factory[0] instanceof Reference)) {
@@ -1170,9 +1169,7 @@ private function addNewInstance(Definition $definition, string $return = '', ?st
11701169
$arguments[] = (\is_string($i) ? $i.': ' : '').$this->dumpValue($value);
11711170
}
11721171

1173-
if (null !== $definition->getFactory()) {
1174-
$callable = $definition->getFactory();
1175-
1172+
if ($callable = $definition->getFactory()) {
11761173
if ('current' === $callable && [0] === array_keys($definition->getArguments()) && \is_array($value) && [0] === array_keys($value)) {
11771174
return $return.$this->dumpValue($value[0]).$tail;
11781175
}
@@ -2299,7 +2296,6 @@ private function getAutoloadFile(): ?string
22992296
private function getClasses(Definition $definition, string $id): array
23002297
{
23012298
$classes = [];
2302-
$resolve = $this->container->getParameterBag()->resolveValue(...);
23032299

23042300
while ($definition instanceof Definition) {
23052301
foreach ($definition->getTag($this->preloadTags[0]) as $tag) {
@@ -2311,24 +2307,24 @@ private function getClasses(Definition $definition, string $id): array
23112307
}
23122308

23132309
if ($class = $definition->getClass()) {
2314-
$classes[] = trim($resolve($class), '\\');
2310+
$classes[] = trim($class, '\\');
23152311
}
23162312
$factory = $definition->getFactory();
23172313

2314+
if (\is_string($factory) && !str_starts_with($factory, '@=') && str_contains($factory, '::')) {
2315+
$factory = explode('::', $factory);
2316+
}
2317+
23182318
if (!\is_array($factory)) {
2319-
$factory = [$factory];
2319+
$definition = $factory;
2320+
continue;
23202321
}
23212322

2322-
if (\is_string($factory[0])) {
2323-
$factory[0] = $resolve($factory[0]);
2323+
$definition = $factory[0] ?? null;
23242324

2325-
if (false !== $i = strrpos($factory[0], '::')) {
2326-
$factory[0] = substr($factory[0], 0, $i);
2327-
}
2325+
if (\is_string($definition)) {
23282326
$classes[] = trim($factory[0], '\\');
23292327
}
2330-
2331-
$definition = $factory[0];
23322328
}
23332329

23342330
return $classes;

0 commit comments

Comments
 (0)