Skip to content

Commit d0a15d1

Browse files
Merge branch '3.4' into 4.1
* 3.4: [DI] Fix dumping expressions accessing single-use private services
2 parents d34cf48 + b984650 commit d0a15d1

13 files changed

+25
-26
lines changed

Dumper/PhpDumper.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -564,9 +564,7 @@ private function addServiceConfigurator(Definition $definition, string $variable
564564
if ($callable[0] instanceof Reference
565565
|| ($callable[0] instanceof Definition && $this->definitionVariables->contains($callable[0]))
566566
) {
567-
$callable[0] = $this->dumpValue($callable[0]);
568-
569-
return sprintf(' '.('$' === $callable[0][0] ? '%s' : '(%s)')."->%s(\$%s);\n", $callable[0], $callable[1], $variableName);
567+
return sprintf(" %s->%s(\$%s);\n", $this->dumpValue($callable[0]), $callable[1], $variableName);
570568
}
571569

572570
$class = $this->dumpValue($callable[0]);
@@ -1610,6 +1608,7 @@ private function getServiceCall(string $id, Reference $reference = null): string
16101608
if ($definition->isShared()) {
16111609
$code = sprintf('$this->%s[\'%s\'] = %s', $definition->isPublic() ? 'services' : 'privates', $id, $code);
16121610
}
1611+
$code = "($code)";
16131612
} elseif ($this->asFiles && !$this->isHotPath($definition)) {
16141613
$code = sprintf("\$this->load('%s.php')", $this->generateMethodName($id));
16151614
if (!$definition->isShared()) {

Tests/Dumper/PhpDumperTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,7 @@ public function testExpressionReferencingPrivateService()
792792
->setPublic(false);
793793
$container->register('public_foo', 'stdClass')
794794
->setPublic(true)
795-
->addArgument(new Expression('service("private_foo")'));
795+
->addArgument(new Expression('service("private_foo").bar'));
796796

797797
$container->compile();
798798
$dumper = new PhpDumper($container);

Tests/Fixtures/php/services9_as_files.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ use Symfony\Component\DependencyInjection\Exception\RuntimeException;
347347

348348
return $this->services['tagged_iterator'] = new \Bar(new RewindableGenerator(function () {
349349
yield 0 => ($this->services['foo'] ?? $this->load('getFooService.php'));
350-
yield 1 => ($this->privates['tagged_iterator_foo'] ?? $this->privates['tagged_iterator_foo'] = new \Bar());
350+
yield 1 => ($this->privates['tagged_iterator_foo'] ?? ($this->privates['tagged_iterator_foo'] = new \Bar()));
351351
}, 2));
352352

353353
[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
@@ -402,7 +402,7 @@ protected function getTaggedIteratorService()
402402
{
403403
return $this->services['tagged_iterator'] = new \Bar(new RewindableGenerator(function () {
404404
yield 0 => ($this->services['foo'] ?? $this->getFooService());
405-
yield 1 => ($this->privates['tagged_iterator_foo'] ?? $this->privates['tagged_iterator_foo'] = new \Bar());
405+
yield 1 => ($this->privates['tagged_iterator_foo'] ?? ($this->privates['tagged_iterator_foo'] = new \Bar()));
406406
}, 2));
407407
}
408408

Tests/Fixtures/php/services_almost_circular_public.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ protected function getBar3Service()
114114
{
115115
$this->services['bar3'] = $instance = new \BarCircular();
116116

117-
$a = ($this->services['foobar3'] ?? $this->services['foobar3'] = new \FoobarCircular());
117+
$a = ($this->services['foobar3'] ?? ($this->services['foobar3'] = new \FoobarCircular()));
118118

119119
$instance->addFoobar($a, $a);
120120

Tests/Fixtures/php/services_env_in_id.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function getRemovedIds()
7070
*/
7171
protected function getBarService()
7272
{
73-
return $this->services['bar'] = new \stdClass(($this->privates['bar_%env(BAR)%'] ?? $this->privates['bar_%env(BAR)%'] = new \stdClass()));
73+
return $this->services['bar'] = new \stdClass(($this->privates['bar_%env(BAR)%'] ?? ($this->privates['bar_%env(BAR)%'] = new \stdClass())));
7474
}
7575

7676
/**
@@ -80,7 +80,7 @@ protected function getBarService()
8080
*/
8181
protected function getFooService()
8282
{
83-
return $this->services['foo'] = new \stdClass(($this->privates['bar_%env(BAR)%'] ?? $this->privates['bar_%env(BAR)%'] = new \stdClass()), array('baz_'.$this->getEnv('string:BAR') => new \stdClass()));
83+
return $this->services['foo'] = new \stdClass(($this->privates['bar_%env(BAR)%'] ?? ($this->privates['bar_%env(BAR)%'] = new \stdClass())), array('baz_'.$this->getEnv('string:BAR') => new \stdClass()));
8484
}
8585

8686
public function getParameter($name)

Tests/Fixtures/php/services_errored_definition.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ protected function getTaggedIteratorService()
402402
{
403403
return $this->services['tagged_iterator'] = new \Bar(new RewindableGenerator(function () {
404404
yield 0 => ($this->services['foo'] ?? $this->getFooService());
405-
yield 1 => ($this->privates['tagged_iterator_foo'] ?? $this->privates['tagged_iterator_foo'] = new \Bar());
405+
yield 1 => ($this->privates['tagged_iterator_foo'] ?? ($this->privates['tagged_iterator_foo'] = new \Bar()));
406406
}, 2));
407407
}
408408

Tests/Fixtures/php/services_locator.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function getRemovedIds()
7676
*/
7777
protected function getBarServiceService()
7878
{
79-
return $this->services['bar_service'] = new \stdClass(($this->privates['baz_service'] ?? $this->privates['baz_service'] = new \stdClass()));
79+
return $this->services['bar_service'] = new \stdClass(($this->privates['baz_service'] ?? ($this->privates['baz_service'] = new \stdClass())));
8080
}
8181

8282
/**
@@ -89,7 +89,7 @@ protected function getFooServiceService()
8989
return $this->services['foo_service'] = new \Symfony\Component\DependencyInjection\ServiceLocator(array('bar' => function () {
9090
return ($this->services['bar_service'] ?? $this->getBarServiceService());
9191
}, 'baz' => function (): \stdClass {
92-
return ($this->privates['baz_service'] ?? $this->privates['baz_service'] = new \stdClass());
92+
return ($this->privates['baz_service'] ?? ($this->privates['baz_service'] = new \stdClass()));
9393
}, 'nil' => function () {
9494
return NULL;
9595
}));
@@ -133,7 +133,7 @@ protected function getTranslator_Loader3Service()
133133
protected function getTranslator1Service()
134134
{
135135
return $this->services['translator_1'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\StubbedTranslator(new \Symfony\Component\DependencyInjection\ServiceLocator(array('translator.loader_1' => function () {
136-
return ($this->services['translator.loader_1'] ?? $this->services['translator.loader_1'] = new \stdClass());
136+
return ($this->services['translator.loader_1'] ?? ($this->services['translator.loader_1'] = new \stdClass()));
137137
})));
138138
}
139139

@@ -145,10 +145,10 @@ protected function getTranslator1Service()
145145
protected function getTranslator2Service()
146146
{
147147
$this->services['translator_2'] = $instance = new \Symfony\Component\DependencyInjection\Tests\Fixtures\StubbedTranslator(new \Symfony\Component\DependencyInjection\ServiceLocator(array('translator.loader_2' => function () {
148-
return ($this->services['translator.loader_2'] ?? $this->services['translator.loader_2'] = new \stdClass());
148+
return ($this->services['translator.loader_2'] ?? ($this->services['translator.loader_2'] = new \stdClass()));
149149
})));
150150

151-
$instance->addResource('db', ($this->services['translator.loader_2'] ?? $this->services['translator.loader_2'] = new \stdClass()), 'nl');
151+
$instance->addResource('db', ($this->services['translator.loader_2'] ?? ($this->services['translator.loader_2'] = new \stdClass())), 'nl');
152152

153153
return $instance;
154154
}
@@ -161,10 +161,10 @@ protected function getTranslator2Service()
161161
protected function getTranslator3Service()
162162
{
163163
$this->services['translator_3'] = $instance = new \Symfony\Component\DependencyInjection\Tests\Fixtures\StubbedTranslator(new \Symfony\Component\DependencyInjection\ServiceLocator(array('translator.loader_3' => function () {
164-
return ($this->services['translator.loader_3'] ?? $this->services['translator.loader_3'] = new \stdClass());
164+
return ($this->services['translator.loader_3'] ?? ($this->services['translator.loader_3'] = new \stdClass()));
165165
})));
166166

167-
$a = ($this->services['translator.loader_3'] ?? $this->services['translator.loader_3'] = new \stdClass());
167+
$a = ($this->services['translator.loader_3'] ?? ($this->services['translator.loader_3'] = new \stdClass()));
168168

169169
$instance->addResource('db', $a, 'nl');
170170
$instance->addResource('db', $a, 'en');

Tests/Fixtures/php/services_private_frozen.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function getRemovedIds()
6767
*/
6868
protected function getBarServiceService()
6969
{
70-
return $this->services['bar_service'] = new \stdClass(($this->privates['baz_service'] ?? $this->privates['baz_service'] = new \stdClass()));
70+
return $this->services['bar_service'] = new \stdClass(($this->privates['baz_service'] ?? ($this->privates['baz_service'] = new \stdClass())));
7171
}
7272

7373
/**
@@ -77,6 +77,6 @@ protected function getBarServiceService()
7777
*/
7878
protected function getFooServiceService()
7979
{
80-
return $this->services['foo_service'] = new \stdClass(($this->privates['baz_service'] ?? $this->privates['baz_service'] = new \stdClass()));
80+
return $this->services['foo_service'] = new \stdClass(($this->privates['baz_service'] ?? ($this->privates['baz_service'] = new \stdClass())));
8181
}
8282
}

Tests/Fixtures/php/services_private_in_expression.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,6 @@ public function getRemovedIds()
6767
*/
6868
protected function getPublicFooService()
6969
{
70-
return $this->services['public_foo'] = new \stdClass(($this->privates['private_foo'] ?? $this->privates['private_foo'] = new \stdClass()));
70+
return $this->services['public_foo'] = new \stdClass(($this->privates['private_foo'] ?? ($this->privates['private_foo'] = new \stdClass()))->bar);
7171
}
7272
}

0 commit comments

Comments
 (0)