Skip to content

Commit b984650

Browse files
Robin Chalasnicolas-grekas
authored andcommitted
[DI] Fix dumping expressions accessing single-use private services
1 parent 9199e2e commit b984650

16 files changed

+31
-32
lines changed

Dumper/PhpDumper.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -594,9 +594,7 @@ private function addServiceConfigurator(Definition $definition, $variableName =
594594
if ($callable[0] instanceof Reference
595595
|| ($callable[0] instanceof Definition && $this->definitionVariables->contains($callable[0]))
596596
) {
597-
$callable[0] = $this->dumpValue($callable[0]);
598-
599-
return sprintf(' '.('$' === $callable[0][0] ? '%s' : '(%s)')."->%s(\$%s);\n", $callable[0], $callable[1], $variableName);
597+
return sprintf(" %s->%s(\$%s);\n", $this->dumpValue($callable[0]), $callable[1], $variableName);
600598
}
601599

602600
$class = $this->dumpValue($callable[0]);
@@ -1824,6 +1822,7 @@ private function getServiceCall($id, Reference $reference = null)
18241822
if ($definition->isShared()) {
18251823
$code = sprintf('$this->services[\'%s\'] = %s', $id, $code);
18261824
}
1825+
$code = "($code)";
18271826
} elseif ($this->asFiles && $definition->isShared() && !$this->isHotPath($definition)) {
18281827
$code = sprintf("\$this->load('%s.php')", $this->generateMethodName($id));
18291828
} else {

Tests/Dumper/PhpDumperTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ public function testExpressionReferencingPrivateService()
752752
->setPublic(false);
753753
$container->register('public_foo', 'stdClass')
754754
->setPublic(true)
755-
->addArgument(new Expression('service("private_foo")'));
755+
->addArgument(new Expression('service("private_foo").bar'));
756756

757757
$container->compile();
758758
$dumper = new PhpDumper($container);

Tests/Fixtures/php/services9.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ protected function getConfiguredServiceSimpleService()
126126
{
127127
$this->services['configured_service_simple'] = $instance = new \stdClass();
128128

129-
${($_ = isset($this->services['configurator_service_simple']) ? $this->services['configurator_service_simple'] : $this->services['configurator_service_simple'] = new \ConfClass('bar')) && false ?: '_'}->configureStdClass($instance);
129+
${($_ = isset($this->services['configurator_service_simple']) ? $this->services['configurator_service_simple'] : ($this->services['configurator_service_simple'] = new \ConfClass('bar'))) && false ?: '_'}->configureStdClass($instance);
130130

131131
return $instance;
132132
}

Tests/Fixtures/php/services9_as_files.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
243243

244244
return $this->services['tagged_iterator'] = new \Bar(new RewindableGenerator(function () {
245245
yield 0 => ${($_ = isset($this->services['foo']) ? $this->services['foo'] : $this->load('getFooService.php')) && false ?: '_'};
246-
yield 1 => ${($_ = isset($this->services['tagged_iterator_foo']) ? $this->services['tagged_iterator_foo'] : $this->services['tagged_iterator_foo'] = new \Bar()) && false ?: '_'};
246+
yield 1 => ${($_ = isset($this->services['tagged_iterator_foo']) ? $this->services['tagged_iterator_foo'] : ($this->services['tagged_iterator_foo'] = new \Bar())) && false ?: '_'};
247247
}, 2));
248248

249249
[Container%s/getTaggedIteratorFooService.php] => <?php

Tests/Fixtures/php/services9_compiled.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ protected function getTaggedIteratorService()
355355
{
356356
return $this->services['tagged_iterator'] = new \Bar(new RewindableGenerator(function () {
357357
yield 0 => ${($_ = isset($this->services['foo']) ? $this->services['foo'] : $this->getFooService()) && false ?: '_'};
358-
yield 1 => ${($_ = isset($this->services['tagged_iterator_foo']) ? $this->services['tagged_iterator_foo'] : $this->services['tagged_iterator_foo'] = new \Bar()) && false ?: '_'};
358+
yield 1 => ${($_ = isset($this->services['tagged_iterator_foo']) ? $this->services['tagged_iterator_foo'] : ($this->services['tagged_iterator_foo'] = new \Bar())) && false ?: '_'};
359359
}, 2));
360360
}
361361

Tests/Fixtures/php/services_almost_circular_private.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ protected function getManager2Service()
331331
*/
332332
protected function getRootService()
333333
{
334-
return $this->services['root'] = new \stdClass(${($_ = isset($this->services['level2']) ? $this->services['level2'] : $this->getLevel2Service()) && false ?: '_'}, ${($_ = isset($this->services['multiuse1']) ? $this->services['multiuse1'] : $this->services['multiuse1'] = new \stdClass()) && false ?: '_'});
334+
return $this->services['root'] = new \stdClass(${($_ = isset($this->services['level2']) ? $this->services['level2'] : $this->getLevel2Service()) && false ?: '_'}, ${($_ = isset($this->services['multiuse1']) ? $this->services['multiuse1'] : ($this->services['multiuse1'] = new \stdClass())) && false ?: '_'});
335335
}
336336

337337
/**
@@ -397,7 +397,7 @@ protected function getLevel3Service()
397397
*/
398398
protected function getLevel4Service()
399399
{
400-
return $this->services['level4'] = new \stdClass(${($_ = isset($this->services['multiuse1']) ? $this->services['multiuse1'] : $this->services['multiuse1'] = new \stdClass()) && false ?: '_'}, ${($_ = isset($this->services['level5']) ? $this->services['level5'] : $this->getLevel5Service()) && false ?: '_'});
400+
return $this->services['level4'] = new \stdClass(${($_ = isset($this->services['multiuse1']) ? $this->services['multiuse1'] : ($this->services['multiuse1'] = new \stdClass())) && false ?: '_'}, ${($_ = isset($this->services['level5']) ? $this->services['level5'] : $this->getLevel5Service()) && false ?: '_'});
401401
}
402402

403403
/**

Tests/Fixtures/php/services_almost_circular_public.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ protected function getBar3Service()
126126
{
127127
$this->services['bar3'] = $instance = new \BarCircular();
128128

129-
$a = ${($_ = isset($this->services['foobar3']) ? $this->services['foobar3'] : $this->services['foobar3'] = new \FoobarCircular()) && false ?: '_'};
129+
$a = ${($_ = isset($this->services['foobar3']) ? $this->services['foobar3'] : ($this->services['foobar3'] = new \FoobarCircular())) && false ?: '_'};
130130

131131
$instance->addFoobar($a, $a);
132132

@@ -431,7 +431,7 @@ protected function getManager2Service()
431431
*/
432432
protected function getRootService()
433433
{
434-
return $this->services['root'] = new \stdClass(${($_ = isset($this->services['level2']) ? $this->services['level2'] : $this->getLevel2Service()) && false ?: '_'}, ${($_ = isset($this->services['multiuse1']) ? $this->services['multiuse1'] : $this->services['multiuse1'] = new \stdClass()) && false ?: '_'});
434+
return $this->services['root'] = new \stdClass(${($_ = isset($this->services['level2']) ? $this->services['level2'] : $this->getLevel2Service()) && false ?: '_'}, ${($_ = isset($this->services['multiuse1']) ? $this->services['multiuse1'] : ($this->services['multiuse1'] = new \stdClass())) && false ?: '_'});
435435
}
436436

437437
/**
@@ -497,7 +497,7 @@ protected function getLevel3Service()
497497
*/
498498
protected function getLevel4Service()
499499
{
500-
return $this->services['level4'] = new \stdClass(${($_ = isset($this->services['multiuse1']) ? $this->services['multiuse1'] : $this->services['multiuse1'] = new \stdClass()) && false ?: '_'}, ${($_ = isset($this->services['level5']) ? $this->services['level5'] : $this->getLevel5Service()) && false ?: '_'});
500+
return $this->services['level4'] = new \stdClass(${($_ = isset($this->services['multiuse1']) ? $this->services['multiuse1'] : ($this->services['multiuse1'] = new \stdClass())) && false ?: '_'}, ${($_ = isset($this->services['level5']) ? $this->services['level5'] : $this->getLevel5Service()) && false ?: '_'});
501501
}
502502

503503
/**

Tests/Fixtures/php/services_env_in_id.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function isFrozen()
7373
*/
7474
protected function getBarService()
7575
{
76-
return $this->services['bar'] = new \stdClass(${($_ = isset($this->services['bar_%env(BAR)%']) ? $this->services['bar_%env(BAR)%'] : $this->services['bar_%env(BAR)%'] = new \stdClass()) && false ?: '_'});
76+
return $this->services['bar'] = new \stdClass(${($_ = isset($this->services['bar_%env(BAR)%']) ? $this->services['bar_%env(BAR)%'] : ($this->services['bar_%env(BAR)%'] = new \stdClass())) && false ?: '_'});
7777
}
7878

7979
/**
@@ -83,7 +83,7 @@ protected function getBarService()
8383
*/
8484
protected function getFooService()
8585
{
86-
return $this->services['foo'] = new \stdClass(${($_ = isset($this->services['bar_%env(BAR)%']) ? $this->services['bar_%env(BAR)%'] : $this->services['bar_%env(BAR)%'] = new \stdClass()) && false ?: '_'}, array('baz_'.$this->getEnv('string:BAR') => new \stdClass()));
86+
return $this->services['foo'] = new \stdClass(${($_ = isset($this->services['bar_%env(BAR)%']) ? $this->services['bar_%env(BAR)%'] : ($this->services['bar_%env(BAR)%'] = new \stdClass())) && false ?: '_'}, array('baz_'.$this->getEnv('string:BAR') => new \stdClass()));
8787
}
8888

8989
/**

Tests/Fixtures/php/services_inline_requires.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ protected function getC2Service()
110110
include_once $this->targetDirs[1].'/includes/HotPath/C2.php';
111111
include_once $this->targetDirs[1].'/includes/HotPath/C3.php';
112112

113-
return $this->services['Symfony\Component\DependencyInjection\Tests\Fixtures\includes\HotPath\C2'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\includes\HotPath\C2(${($_ = isset($this->services['Symfony\Component\DependencyInjection\Tests\Fixtures\includes\HotPath\C3']) ? $this->services['Symfony\Component\DependencyInjection\Tests\Fixtures\includes\HotPath\C3'] : $this->services['Symfony\Component\DependencyInjection\Tests\Fixtures\includes\HotPath\C3'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\includes\HotPath\C3()) && false ?: '_'});
113+
return $this->services['Symfony\Component\DependencyInjection\Tests\Fixtures\includes\HotPath\C2'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\includes\HotPath\C2(${($_ = isset($this->services['Symfony\Component\DependencyInjection\Tests\Fixtures\includes\HotPath\C3']) ? $this->services['Symfony\Component\DependencyInjection\Tests\Fixtures\includes\HotPath\C3'] : ($this->services['Symfony\Component\DependencyInjection\Tests\Fixtures\includes\HotPath\C3'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\includes\HotPath\C3())) && false ?: '_'});
114114
}
115115

116116
/**

Tests/Fixtures/php/services_legacy_privates.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function isFrozen()
9898
*/
9999
protected function getBarService()
100100
{
101-
return $this->services['bar'] = new \stdClass(${($_ = isset($this->services['private_not_inlined']) ? $this->services['private_not_inlined'] : $this->services['private_not_inlined'] = new \stdClass()) && false ?: '_'});
101+
return $this->services['bar'] = new \stdClass(${($_ = isset($this->services['private_not_inlined']) ? $this->services['private_not_inlined'] : ($this->services['private_not_inlined'] = new \stdClass())) && false ?: '_'});
102102
}
103103

104104
/**

0 commit comments

Comments
 (0)