8000 [DependencyInjection] fixed PhpDumper optimizations when an inlined s… · klend/symfony@e0923ae · GitHub
[go: up one dir, main page]

Skip to content

Commit e0923ae

Browse files
committed
[DependencyInjection] fixed PhpDumper optimizations when an inlined service depends on the current one indirectly
1 parent cd15390 commit e0923ae

File tree

8 files changed

+80
-8
lines changed

8 files changed

+80
-8
lines changed

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ private function addServiceInlinedDefinitions($id, $definition)
246246
$code .= sprintf(" \$%s = new \\%s(%s);\n", $name, substr(str_replace('\\\\', '\\', $class), 1, -1), implode(', ', $arguments));
247247
}
248248

249-
if (!$this->hasReference($id, $sDefinition->getMethodCalls()) && !$this->hasReference($id, $sDefinition->getProperties())) {
249+
if (!$this->hasReference($id, $sDefinition->getMethodCalls(), true) && !$this->hasReference($id, $sDefinition->getProperties(), true)) {
250250
$code .= $this->addServiceMethodCalls(null, $sDefinition, $name);
251251
$code .= $this->addServiceProperties(null, $sDefinition, $name);
252252
$code .= $this->addServiceConfigurator(null, $sDefinition, $name);
@@ -415,7 +415,7 @@ private function addServiceInlinedDefinitionsSetup($id, $definition)
415415
}
416416
$processed->offsetSet($iDefinition);
417417

418-
if (!$this->hasReference($id, $iDefinition->getMethodCalls()) && !$this->hasReference($id, $iDefinition->getProperties())) {
418+
if (!$this->hasReference($id, $iDefinition->getMethodCalls(), true) && !$this->hasReference($id, $iDefinition->getProperties(), true)) {
419419
continue;
420420
}
421421

@@ -958,17 +958,26 @@ private function getDefinitionsFromArguments(array $arguments)
958958
*
959959
* @return Boolean
960960
*/
961-
private function hasReference($id, array $arguments)
961+
private function hasReference($id, array $arguments, $deep = false)
962962
{
963963
foreach ($arguments as $argument) {
964964
if (is_array($argument)) {
965-
if ($this->hasReference($id, $argument)) {
965+
if ($this->hasReference($id, $argument, $deep)) {
966966
return true;
967967
}
968968
} elseif ($argument instanceof Reference) {
969969
if ($id === (string) $argument) {
970970
return true;
971971
}
972+
973+
if ($deep) {
974+
$service = $this->container->getDefinition((string) $argument);
975+
$arguments = array_merge($service->getMethodCalls(), $service->getArguments(), $service->getProperties());
976+
977+
if ($this->hasReference($id, $arguments, $deep)) {
978+
return true;
979+
}
980+
}
972981
}
973982
}
974983

tests/Symfony/Tests/Component/DependencyInjection/Fixtures/containers/container9.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,12 @@
6464
$container
6565
->register('inlined', 'Bar')
6666
->setProperty('pub', 'pub')
67-
->addMethodCall('setFoo', array(new Reference('foo_with_inline')))
67+
->addMethodCall('setBaz', array(new Reference('baz')))
6868
->setPublic(false)
6969
;
70+
$container
71+
->register('baz', 'Baz')
72+
->addMethodCall('setFoo', array(new Reference('foo_with_inline')))
73+
;
7074

7175
return $container;

tests/Symfony/Tests/Component/DependencyInjection/Fixtures/graphviz/services9.dot

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ digraph sc {
1111
node_factory_service [label="factory_service\nBar\n", shape=record, fillcolor="#eeeeee", style="filled"];
1212
node_foo_with_inline [label="foo_with_inline\nFoo\n", shape=record, fillcolor="#eeeeee", style="filled"];
1313
node_inlined [label="inlined\nBar\n", shape=record, fillcolor="#eeeeee", style="filled"];
14+
node_baz [label="baz\nBaz\n", shape=record, fillcolor="#eeeeee", style="filled"];
1415
node_service_container [label="service_container\nSymfony\\Component\\DependencyInjection\\ContainerBuilder\n", shape=record, fillcolor="#9999ff", style="filled"];
1516
node_foo2 [label="foo2\n\n", shape=record, fillcolor="#ff9999", style="filled"];
1617
node_foo3 [label="foo3\n\n", shape=record, fillcolor="#ff9999", style="filled"];
@@ -25,5 +26,6 @@ digraph sc {
2526
node_method_call1 -> node_foo3 [label="setBar()" style="dashed"];
2627
node_method_call1 -> node_foobaz [label="setBar()" style="dashed"];
2728
node_foo_with_inline -> node_inlined [label="setBar()" style="dashed"];
28-
node_inlined -> node_foo_with_inline [label="setFoo()" style="dashed"];
29+
node_inlined -> node_baz [label="setBaz()" style="dashed"];
30+
node_baz -> node_foo_with_inline [label="setFoo()" style="dashed"];
2931
}

tests/Symfony/Tests/Component/DependencyInjection/Fixtures/includes/classes.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,23 @@ function sc_configure($instance)
77

88
class BarClass
99
{
10+
protected $baz;
11+
12+
public function setBaz(BazClass $baz)
13+
{
14+
$this->baz = $baz;
15+
}
1016
}
1117

1218
class BazClass
1319
{
20+
protected $foo;
21+
22+
public function setFoo(Foo $foo)
23+
{
24+
$this->foo = $foo;
25+
}
26+
1427
public function configure($instance)
1528
{
1629
$instance->configure();

tests/Symfony/Tests/Component/DependencyInjection/Fixtures/php/services9.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,23 @@ protected function getBarService()
4040
return $instance;
4141
}
4242

43+
/**
44+
* Gets the 'baz' service.
45+
*
46+
* This service is shared.
47+
* This method always returns the same instance of the service.
48+
*
49+
* @return Baz A Baz instance.
50+
*/
51+
protected function getBazService()
52+
{
53+
$this->services['baz'] = $instance = new \Baz();
54+
55+
$instance->setFoo($this->get('foo_with_inline'));
56+
57+
return $instance;
58+
}
59+
4360
/**
4461
* Gets the 'factory_service' service.
4562
*
@@ -173,7 +190,7 @@ protected function getInlinedService()
173190
{
174191
$this->services['inlined'] = $instance = new \Bar();
175192

176-
$instance->setFoo($this->get('foo_with_inline'));
193+
$instance->setBaz($this->get('baz'));
177194
$instance->pub = 'pub';
178195

179196
return $instance;

tests/Symfony/Tests/Component/DependencyInjection/Fixtures/php/services9_compiled.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,23 @@ protected function getBarService()
4949
return $instance;
5050
}
5151

52+
/**
53+
* Gets the 'baz' service.
54+
*
55+
* This service is shared.
56+
* This method always returns the same instance of the service.
57+
*
58+
* @return Baz A Baz instance.
59+
*/
60+
protected function getBazService()
61+
{
62+
$this->services['baz'] = $instance = new \Baz();
63+
64+
$instance->setFoo($this->get('foo_with_inline'));
65+
66+
return $instance;
67+
}
68+
5269
/**
5370
* Gets the 'factory_service' service.
5471
*
@@ -126,7 +143,7 @@ protected function getFooWithInlineService()
126143

127144
$this->services['foo_with_inline'] = $instance = new \Foo();
128145

129-
$a->setFoo($instance);
146+
$a->setBaz($this->get('baz'));
130147
$a->pub = 'pub';
131148

132149
$instance->setBar($a);

tests/Symfony/Tests/Component/DependencyInjection/Fixtures/xml/services9.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@
5858
</service>
5959
<service id="inlined" class="Bar" public="false">
6060
<property name="pub">pub</property>
61+
<call method="setBaz">
62+
<argument type="service" id="baz"/>
63+
</call>
64+
</service>
65+
<service id="baz" class="Baz">
6166
<call method="setFoo">
6267
<argument type="service" id="foo_with_inline"/>
6368
</call>

tests/Symfony/Tests/Component/DependencyInjection/Fixtures/yaml/services9.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ services:
4949
inlined:
5050
class: Bar
5151
properties: { pub: pub }
52+
calls:
53+
- [setBaz, ['@baz']]
54+
55+
baz:
56+
class: Baz
5257
calls:
5358
- [setFoo, ['@foo_with_inline']]
5459

0 commit comments

Comments
 (0)
0