Skip to content

Commit ecf54d5

Browse files
committed
bug symfony#25354 [DI] Fix non-string class handling in PhpDumper (nicolas-grekas, sroze)
This PR was merged into the 3.4 branch. Discussion ---------- [DI] Fix non-string class handling in PhpDumper | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#25353 | License | MIT | Doc PR | - Commits ------- 28f0086 Ensure that inlined services with parameterized class name can be dumped 730b156 [DI] Fix non-string class handling in PhpDumper
2 parents 6129ae2 + 28f0086 commit ecf54d5

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ private function addServiceInclude($cId, Definition $definition, \SplObjectStora
413413
if ($this->inlineRequires && !$this->isHotPath($definition)) {
414414
$lineage = $calls = $behavior = array();
415415
foreach ($inlinedDefinitions as $def) {
416-
if (!$def->isDeprecated() && $class = is_array($factory = $def->getFactory()) && is_string($factory[0]) ? $factory[0] : $def->getClass()) {
416+
if (!$def->isDeprecated() && is_string($class = is_array($factory = $def->getFactory()) && is_string($factory[0]) ? $factory[0] : $def->getClass())) {
417417
$this->collectLineage($class, $lineage);
418418
}
419419
$arguments = array($def->getArguments(), $def->getFactory(), $def->getProperties(), $def->getMethodCalls(), $def->getConfigurator());
@@ -425,7 +425,7 @@ private function addServiceInclude($cId, Definition $definition, \SplObjectStora
425425
&& ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE !== $behavior[$id]
426426
&& $this->container->has($id)
427427
&& $this->isTrivialInstance($def = $this->container->findDefinition($id))
428-
&& $class = is_array($factory = $def->getFactory()) && is_string($factory[0]) ? $factory[0] : $def->getClass()
428+
&& is_string($class = is_array($factory = $def->getFactory()) && is_string($factory[0]) ? $factory[0] : $def->getClass())
429429
) {
430430
$this->collectLineage($class, $lineage);
431431
}
@@ -1226,7 +1226,7 @@ private function addInlineRequires()
12261226
$inlinedDefinitions = $this->getDefinitionsFromArguments(array($definition));
12271227

12281228
foreach ($inlinedDefinitions as $def) {
1229-
if ($class = is_array($factory = $def->getFactory()) && is_string($factory[0]) ? $factory[0] : $def->getClass()) {
1229+
if (is_string($class = is_array($factory = $def->getFactory()) && is_string($factory[0]) ? $factory[0] : $def->getClass())) {
12301230
$this->collectLineage($class, $lineage);
12311231
}
12321232
}

src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Symfony\Component\DependencyInjection\ContainerInterface as SymfonyContainerInterface;
2424
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
2525
use Symfony\Component\DependencyInjection\EnvVarProcessorInterface;
26+
use Symfony\Component\DependencyInjection\Parameter;
2627
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
2728
use Symfony\Component\DependencyInjection\Reference;
2829
use Symfony\Component\DependencyInjection\Tests\Fixtures\StubbedTranslator;
@@ -822,6 +823,31 @@ public function testDumpHandlesLiteralClassWithRootNamespace()
822823
$this->assertInstanceOf('stdClass', $container->get('foo'));
823824
}
824825

826+
public function testDumpHandlesObjectClassNames()
827+
{
828+
$container = new ContainerBuilder(new ParameterBag(array(
829+
'class' => 'stdClass',
830+
)));
831+
832+
$container->setDefinition('foo', new Definition(new Parameter('class')));
833+
$container->setDefinition('bar', new Definition('stdClass', array(
834+
new Reference('foo'),
835+
)))->setPublic(true);
836+
837+
$container->setParameter('inline_requires', true);
838+
$container->compile();
839+
840+
$dumper = new PhpDumper($container);
841+
eval('?>'.$dumper->dump(array(
842+
'class' => 'Symfony_DI_PhpDumper_Test_Object_Class_Name',
843+
'inline_class_loader_parameter' => 'inline_requires',
844+
)));
845+
846+
$container = new \Symfony_DI_PhpDumper_Test_Object_Class_Name();
847+
848+
$this->assertInstanceOf('stdClass', $container->get('bar'));
849+
}
850+
825851
/**
826852
* @group legacy
827853
* @expectedDeprecation The "private" service is private, getting it from the container is deprecated since Symfony 3.2 and will fail in 4.0. You should either make the service public, or stop using the container directly and use dependency injection instead.

0 commit comments

Comments
 (0)