8000 merged branch fabpot/container-dumper-fixes (PR #6565) · Ninir/symfony@5258edb · GitHub
[go: up one dir, main page]

Skip to content

Commit 5258edb

Browse files
committed
merged branch fabpot/container-dumper-fixes (PR symfony#6565)
This PR was merged into the 2.0 branch. Commits ------- e0923ae [DependencyInjection] fixed PhpDumper optimizations when an inlined service depends on the current one indirectly cd15390 [DependencyInjection] fixed PhpDumper when an inlined service definition has some properties e939a42 [DependencyInjection] added some tests for PhpDumper when the container is compiled 3827e3e [DependencyInjection] fixed CS Discussion ---------- [DependencyInjection] Fixed PhpDumper when compiling inlined services
2 parents 4f7791d + e0923ae commit 5258edb

File tree

9 files changed

+408
-31
lines changed

9 files changed

+408
-31
lines changed

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

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ private function addServiceInlinedDefinitions($id, $definition)
201201
$nbOccurrences->offsetSet($definition, 1);
202202
} else {
203203
$i = $nbOccurrences->offsetGet($definition);
204-
$nbOccurrences->offsetSet($definition, $i+1);
204+
$nbOccurrences->offsetSet($definition, $i + 1);
205205
}
206206
}
207207

@@ -212,7 +212,7 @@ private function addServiceInlinedDefinitions($id, $definition)
212212
$processed->offsetSet($sDefinition);
213213

214214
$class = $this->dumpValue($sDefinition->getClass());
215-
if ($nbOccurrences->offsetGet($sDefinition) > 1 || count($sDefinition->getMethodCalls()) > 0 || $sDefinition->getProperties() || null !== $sDefinition->getConfigurator() || false !== strpos($class, '$')) {
215+
if ($nbOccurrences->offsetGet($sDefinition) > 1 || $sDefinition->getMethodCalls() || $sDefinition->getProperties() || null !== $sDefinition->getConfigurator() || false !== strpos($class, '$')) {
216216
$name = $this->getNextVariableName();
217217
$variableMap->offsetSet($sDefinition, new Variable($name));
218218

@@ -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,16 +415,14 @@ private function addServiceInlinedDefinitionsSetup($id, $definition)
415415
}
416416
$processed->offsetSet($iDefinition);
417417

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

422-
if ($iDefinition->getMethodCalls()) {
423-
$code .= $this->addServiceMethodCalls(null, $iDefinition, (string) $this->definitionVariables->offsetGet($iDefinition));
424-
}
425-
if ($iDefinition->getConfigurator()) {
426-
$code .= $this->addServiceConfigurator(null, $iDefinition, (string) $this->definitionVariables->offsetGet($iDefinition));
427-
}
422+
$name = (string) $this->definitionVariables->offsetGet($iDefinition);
423+
$code .= $this->addServiceMethodCalls(null, $iDefinition, $name);
424+
$code .= $this->addServiceProperties(null, $iDefinition, $name);
425+
$code .= $this->addServiceConfigurator(null, $iDefinition, $name);
428426
}
429427

430428
if ('' !== $code) {
@@ -960,17 +958,26 @@ private function getDefinitionsFromArguments(array $arguments)
960958
*
961959
* @return Boolean
962960
*/
963-
private function hasReference($id, array $arguments)
961+
private function hasReference($id, array $arguments, $deep = false)
964962
{
965963
foreach ($arguments as $argument) {
966964
if (is_array($argument)) {
967-
if ($this->hasReference($id, $argument)) {
965+
if ($this->hasReference($id, $argument, $deep)) {
968966
return true;
969967
}
970968
} elseif ($argument instanceof Reference) {
971969
if ($id === (string) $argument) {
972970
return true;
973971
}
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+
}
974981
}
975982
}
976983

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,17 @@ public function testAddParameters()
9696

9797
public function testAddService()
9898
{
99+
// without compilation
99100
$container = include self::$fixturesPath.'/containers/container9.php';
100101
$dumper = new PhpDumper($container);
101102
$this->assertEquals(str_replace('%path%', str_replace('\\','\\\\',self::$fixturesPath.DIRECTORY_SEPARATOR.'includes'.DIRECTORY_SEPARATOR), file_get_contents(self::$fixturesPath.'/php/services9.php')), $dumper->dump(), '->dump() dumps services');
102103

104+
// with compilation
105+
$container = include self::$fixturesPath.'/containers/container9.php';
106+
$container->compile();
107+
$dumper = new PhpDumper($container);
108+
$this->assertEquals(str_replace('%path%', str_replace('\\','\\\\',self::$fixturesPath.DIRECTORY_SEPARATOR.'includes'.DIRECTORY_SEPARATOR), file_get_contents(self::$fixturesPath.'/php/services9_compiled.php')), $dumper->dump(), '->dump() dumps services');
109+
103110
$dumper = new PhpDumper($container = new ContainerBuilder());
104111
$container->register('foo', 'FooClass')->addArgument(new \stdClass());
105112
try {

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414
addTag('foo', array('bar' => 'bar'))->
1515
setFactoryClass('FooClass')->
1616
setFactoryMethod('getInstance')->
17-
setArguments(array('foo', new Reference('foo.baz'), array('%foo%' => 'foo is %foo%', 'bar' => '%foo%'), true, new Reference('service_container')))->
17+
setArguments(array('foo', new Reference('foo.baz'), array('%foo%' => 'foo is %foo%', 'foobar' => '%foo%'), true, new Reference('service_container')))->
1818
setProperties(array('foo' => 'bar', 'moo' => new Reference('foo.baz')))->
19-
setScope('prototype')->
2019
addMethodCall('setBar', array(new Reference('bar')))->
2120
addMethodCall('initialize')->
2221
setConfigurator('sc_configure')
@@ -33,7 +32,10 @@
3332
setFactoryMethod('getInstance')->
3433
setConfigurator(array('%baz_class%', 'configureStatic1'))
3534
;
36-
$container->register('foo_bar', '%foo_class%');
35+
$container->
36+
register('foo_bar', '%foo_class%')->
37+
setScope('prototype')
38+
;
3739
$container->getParameterBag()->clear();
3840
$container->getParameterBag()->add(array(
3941
'baz_class' => 'BazClass',
@@ -50,9 +52,24 @@
5052
addMethodCall('setBar', array(new Reference('foobaz', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)))
5153
;
5254
$container->
53-
register('factory_service')->
55+
register('factory_service', 'Bar')->
5456
setFactoryService('foo.baz')->
5557
setFactoryMethod('getInstance')
5658
;
5759

60+
$container
61+
->register('foo_with_inline', 'Foo')
62+
->addMethodCall('setBar', array(new Reference('inlined')))
63+
;
64+
$container
65+
->register('inlined', 'Bar')
66+
->setProperty('pub', 'pub')
67+
->addMethodCall('setBaz', array(new Reference('baz')))
68+
->setPublic(false)
69+
;
70+
$container
71+
->register('baz', 'Baz')
72+
->addMethodCall('setFoo', array(new Reference('foo_with_inline')))
73+
;
74+
5875
return $container;

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ digraph sc {
33
node [fontsize="11" fontname="Arial" shape="record"];
44
edge [fontsize="9" fontname="Arial" color="grey" arrowhead="open" arrowsize="0.5"];
55

6-
node_foo [label="foo (alias_for_foo)\nFooClass\n", shape=record, fillcolor="#eeeeee", style="dotted"];
6+
node_foo [label="foo (alias_for_foo)\nFooClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
77
node_bar [label="bar\nFooClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
88
node_foo_baz [label="foo.baz\nBazClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
9-
node_foo_bar [label="foo_bar\nFooClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
9+
node_foo_bar [label="foo_bar\nFooClass\n", shape=record, fillcolor="#eeeeee", style="dotted"];
1010
node_method_call1 [label="method_call1\nFooClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
11-
node_factory_service [label="factory_service\n\n", shape=record, fillcolor="#eeeeee", style="filled"];
11+
node_factory_service [label="factory_service\nBar\n", shape=record, fillcolor="#eeeeee", style="filled"];
12+
node_foo_with_inline [label="foo_with_inline\nFoo\n", shape=record, fillcolor="#eeeeee", style="filled"];
13+
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"];
1215
node_service_container [label="service_container\nSymfony\\Component\\DependencyInjection\\ContainerBuilder\n", shape=record, fillcolor="#9999ff", style="filled"];
1316
node_foo2 [label="foo2\n\n", shape=record, fillcolor="#ff9999", style="filled"];
1417
node_foo3 [label="foo3\n\n", shape=record, fillcolor="#ff9999", style="filled"];
@@ -22,4 +25,7 @@ digraph sc {
2225
node_method_call1 -> node_foo2 [label="setBar()" style="dashed"];
2326
node_method_call1 -> node_foo3 [label="setBar()" style="dashed"];
2427
node_method_call1 -> node_foobaz [label="setBar()" style="dashed"];
28+
node_foo_with_inline -> node_inlined [label="setBar()" style="dashed"];
29+
node_inlined -> node_baz [label="setBaz()" style="dashed"];
30+
node_baz -> node_foo_with_inline [label="setFoo()" style="dashed"];
2531
}

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: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,30 @@ 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
*
4663
* This service is shared.
4764
* This method always returns the same instance of the service.
4865
*
49-
* @return Object An instance returned by foo.baz::getInstance().
66+
* @return Bar A Bar instance.
5067
*/
5168
protected function getFactoryServiceService()
5269
{
@@ -56,13 +73,16 @@ protected function getFactoryServiceService()
5673
/**
5774
* Gets the 'foo' service.
5875
*
76+
* This service is shared.
77+
* This method always returns the same instance of the service.
78+
*
5979
* @return FooClass A FooClass instance.
6080
*/
6181
protected function getFooService()
6282
{
6383
$a = $this->get('foo.baz');
6484

65-
$instance = call_user_func(array('FooClass', 'getInstance'), 'foo', $a, array($this->getParameter('foo') => 'foo is '.$this->getParameter('foo'), 'bar' => $this->getParameter('foo')), true, $this);
85+
$this->services['foo'] = $instance = call_user_func(array('FooClass', 'getInstance'), 'foo', $a, array($this->getParameter('foo') => 'foo is '.$this->getParameter('foo'), 'foobar' => $this->getParameter('foo')), true, $this);
6686

6787
$instance->setBar($this->get('bar'));
6888
$instance->initialize();
@@ -93,15 +113,29 @@ protected function getFoo_BazService()
93113
/**
94114
* Gets the 'foo_bar' service.
95115
*
96-
* This service is shared.
97-
* This method always returns the same instance of the service.
98-
*
99116
* @return Object A %foo_class% instance.
100117
*/
101118
protected function getFooBarService()
102119
{
103120
$class = $this->getParameter('foo_class');
104-
return $this->services['foo_bar'] = new $class();
121+
return new $class();
122+
}
123+
124+
/**
125+
* Gets the 'foo_with_inline' service.
126+
*
127+
* This service is shared.
128+
* This method always returns the same instance of the service.
129+
*
130+
* @return Foo A Foo instance.
131+
*/
132+
protected function getFooWithInlineService()
133+
{
134+
$this->services['foo_with_inline'] = $instance = new \Foo();
135+
136+
$instance->setBar($this->get('inlined'));
137+
138+
return $instance;
105139
}
106140

107141
/**
@@ -140,6 +174,28 @@ protected function getAliasForFooService()
140174
return $this->get('foo');
141175
}
142176

177+
/**
178+
* Gets the 'inlined' service.
179+
*
180+
* This service is shared.
181+
* This method always returns the same instance of the service.
182+
*
183+
* This service is private.
184+
* If you want to be able to request this service from the container directly,
185+
* make it public, otherwise you might end up with broken code.
186+
*
187+
* @return Bar A Bar instance.
188+
*/
189+
protected function getInlinedService()
190+
{
191+
$this->services['inlined'] = $instance = new \Bar();
192+
193+
$instance->setBaz($this->get('baz'));
194+
$instance->pub = 'pub';
195+
196+
return $instance;
197+
}
198+
143199
/**
144200
* Gets the default parameters.
145201
*

0 commit comments

Comments
 (0)
0