Skip to content

Commit bda1843

Browse files
realitykingnicolas-grekas
authored andcommitted
[DependencyInjection] Avoid generating call_user_func in more cases
1 parent b2f4d40 commit bda1843

File tree

8 files changed

+157
-1
lines changed

8 files changed

+157
-1
lines changed

Dumper/PhpDumper.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,10 @@ private function addServiceConfigurator($id, $definition, $variableName = 'insta
537537
return sprintf(" %s::%s(\$%s);\n", $this->dumpLiteralClass($class), $callable[1], $variableName);
538538
}
539539

540+
if (0 === strpos($class, 'new ')) {
541+
return sprintf(" (%s)->%s(\$%s);\n", $this->dumpValue($callable[0]), $callable[1], $variableName);
542+
}
543+
540544
return sprintf(" call_user_func(array(%s, '%s'), \$%s);\n", $this->dumpValue($callable[0]), $callable[1], $variableName);
541545
}
542546

@@ -713,6 +717,10 @@ private function addNewInstance(Definition $definition, $return, $instantiation)
713717
return sprintf(" $return{$instantiation}%s::%s(%s);\n", $this->dumpLiteralClass($class), $callable[1], $arguments ? implode(', ', $arguments) : '');
714718
}
715719

720+
if (0 === strpos($class, 'new ')) {
721+
return sprintf(" $return{$instantiation}(%s)->%s(%s);\n", $this->dumpValue($callable[0]), $callable[1], $arguments ? implode(', ', $arguments) : '');
722+
}
723+
716724
return sprintf(" $return{$instantiation}call_user_func(array(%s, '%s')%s);\n", $this->dumpValue($callable[0]), $callable[1], $arguments ? ', '.implode(', ', $arguments) : '');
717725
}
718726

Tests/Fixtures/containers/container9.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@
7878
->register('configured_service', 'stdClass')
7979
->setConfigurator(array(new Reference('configurator_service'), 'configureStdClass'))
8080
;
81+
$container
82+
->register('configurator_service_simple', 'ConfClass')
83+
->addArgument('bar')
84+
->setPublic(false)
85+
;
86+
$container
87+
->register('configured_service_simple', 'stdClass')
88+
->setConfigurator(array(new Reference('configurator_service_simple'), 'configureStdClass'))
89+
;
8190
$container
8291
->register('decorated', 'stdClass')
8392
;
@@ -111,5 +120,14 @@
111120
->register('service_from_static_method', 'Bar\FooClass')
112121
->setFactory(array('Bar\FooClass', 'getInstance'))
113122
;
123+
$container
124+
->register('factory_simple', 'SimpleFactoryClass')
125+
->addArgument('foo')
126+
->setPublic(false)
127+
;
128+
$container
129+
->register('factory_service_simple', 'Bar')
130+
->setFactory(array(new Reference('factory_simple'), 'getInstance'))
131+
;
114132

115133
return $container;

Tests/Fixtures/graphviz/services9.dot

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ digraph sc {
1414
node_request [label="request\nRequest\n", shape=record, fillcolor="#eeeeee", style="filled"];
1515
node_configurator_service [label="configurator_service\nConfClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
1616
node_configured_service [label="configured_service\nstdClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
17+
node_configurator_service_simple [label="configurator_service_simple\nConfClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
18+
node_configured_service_simple [label="configured_service_simple\nstdClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
1719
node_decorated [label="decorated\nstdClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
1820
node_decorator_service [label="decorator_service\nstdClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
1921
node_decorator_service_with_name [label="decorator_service_with_name\nstdClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
@@ -22,6 +24,8 @@ digraph sc {
2224
node_factory_service [label="factory_service\nBar\n", shape=record, fillcolor="#eeeeee", style="filled"];
2325
node_new_factory_service [label="new_factory_service\nFooBarBaz\n", shape=record, fillcolor="#eeeeee", style="filled"];
2426
node_service_from_static_method [label="service_from_static_method\nBar\\FooClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
27+
node_factory_simple [label="factory_simple\nSimpleFactoryClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
28+
node_factory_service_simple [label="factory_service_simple\nBar\n", shape=record, fillcolor="#eeeeee", style="filled"];
2529
node_service_container [label="service_container\nSymfony\\Component\\DependencyInjection\\ContainerBuilder\n", shape=record, fillcolor="#9999ff", style="filled"];
2630
node_foo2 [label="foo2\n\n", shape=record, fillcolor="#ff9999", style="filled"];
2731
node_foo3 [label="foo3\n\n", shape=record, fillcolor="#ff9999", style="filled"];

Tests/Fixtures/php/services19.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function __construct()
4040
*/
4141
protected function getServiceFromAnonymousFactoryService()
4242
{
43-
return $this->services['service_from_anonymous_factory'] = call_user_func(array(new \Bar\FooClass(), 'getInstance'));
43+
return $this->services['service_from_anonymous_factory'] = (new \Bar\FooClass())->getInstance();
4444
}
4545

4646
/**

Tests/Fixtures/php/services9.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,16 @@ public function __construct()
2828
'bar' => 'getBarService',
2929
'baz' => 'getBazService',
3030
'configurator_service' => 'getConfiguratorServiceService',
31+
'configurator_service_simple' => 'getConfiguratorServiceSimpleService',
3132
'configured_service' => 'getConfiguredServiceService',
33+
'configured_service_simple' => 'getConfiguredServiceSimpleService',
3234
'decorated' => 'getDecoratedService',
3335
'decorator_service' => 'getDecoratorServiceService',
3436
'decorator_service_with_name' => 'getDecoratorServiceWithNameService',
3537
'deprecated_service' => 'getDeprecatedServiceService',
3638
'factory_service' => 'getFactoryServiceService',
39+
'factory_service_simple' => 'getFactoryServiceSimpleService',
40+
'factory_simple' => 'getFactorySimpleService',
3741
'foo' => 'getFooService',
3842
'foo.baz' => 'getFoo_BazService',
3943
'foo_bar' => 'getFooBarService',
@@ -104,6 +108,23 @@ protected function getConfiguredServiceService()
104108
return $instance;
105109
}
106110

111+
/**
112+
* Gets the 'configured_service_simple' service.
113+
*
114+
* This service is shared.
115+
* This method always returns the same instance of the service.
116+
*
117+
* @return \stdClass A stdClass instance.
118+
*/
119+
protected function getConfiguredServiceSimpleService()
120+
{
121+
$this->services['configured_service_simple'] = $instance = new \stdClass();
122+
123+
$this->get('configurator_service_simple')->configureStdClass($instance);
124+
125+
return $instance;
126+
}
127+
107128
/**
108129
* Gets the 'decorated' service.
109130
*
@@ -173,6 +194,19 @@ protected function getFactoryServiceService()
173194
return $this->services['factory_service'] = $this->get('foo.baz')->getInstance();
174195
}
175196

197+
/**
198+
* Gets the 'factory_service_simple' service.
199+
*
200+
* This service is shared.
201+
* This method always returns the same instance of the service.
202+
*
203+
* @return \Bar A Bar instance.
204+
*/
205+
protected function getFactoryServiceSimpleService()
206+
{
207+
return $this->services['factory_service_simple'] = $this->get('factory_simple')->getInstance();
208+
}
209+
176210
/**
177211
* Gets the 'foo' service.
178212
*
@@ -334,6 +368,40 @@ protected function getConfiguratorServiceService()
334368
return $instance;
335369
}
336370

371+
/**
372+
* Gets the 'configurator_service_simple' service.
373+
*
374+
* This service is shared.
375+
* This method always returns the same instance of the service.
376+
*
377+
* This service is private.
378+
* If you want to be able to request this service from the container directly,
379+
* make it public, otherwise you might end up with broken code.
380+
*
381+
* @return \ConfClass A ConfClass instance.
382+
*/
383+
protected function getConfiguratorServiceSimpleService()
384+
{
385+
return $this->services['configurator_service_simple'] = new \ConfClass('bar');
386+
}
387+
388+
/**
389+
* Gets the 'factory_simple' service.
390+
*
391+
* This service is shared.
392+
* This method always returns the same instance of the service.
393+
*
394+
* This service is private.
395+
* If you want to be able to request this service from the container directly,
396+
* make it public, otherwise you might end up with broken code.
397+
*
398+
* @return \SimpleFactoryClass A SimpleFactoryClass instance.
399+
*/
400+
protected function getFactorySimpleService()
401+
{
402+
return $this->services['factory_simple'] = new \SimpleFactoryClass('foo');
403+
}
404+
337405
/**
338406
* Gets the 'inlined' service.
339407
*

Tests/Fixtures/php/services9_compiled.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ public function __construct()
3030
'bar' => 'getBarService',
3131
'baz' => 'getBazService',
3232
'configured_service' => 'getConfiguredServiceService',
33+
'configured_service_simple' => 'getConfiguredServiceSimpleService',
3334
'decorator_service' => 'getDecoratorServiceService',
3435
'decorator_service_with_name' => 'getDecoratorServiceWithNameService',
3536
'deprecated_service' => 'getDeprecatedServiceService',
3637
'factory_service' => 'getFactoryServiceService',
38+
'factory_service_simple' => 'getFactoryServiceSimpleService',
3739
'foo' => 'getFooService',
3840
'foo.baz' => 'getFoo_BazService',
3941
'foo_bar' => 'getFooBarService',
@@ -114,6 +116,23 @@ protected function getConfiguredServiceService()
114116
return $instance;
115117
}
116118

119+
/**
120+
* Gets the 'configured_service_simple' service.
121+
*
122+
* This service is shared.
123+
* This method always returns the same instance of the service.
124+
*
125+
* @return \stdClass A stdClass instance.
126+
*/
127+
protected function getConfiguredServiceSimpleService()
128+
{
129+
$this->services['configured_service_simple'] = $instance = new \stdClass();
130+
131+
(new \ConfClass('bar'))->configureStdClass($instance);
132+
133+
return $instance;
134+
}
135+
117136
/**
118137
* Gets the 'decorator_service' service.
119138
*
@@ -170,6 +189,19 @@ protected function getFactoryServiceService()
170189
return $this->services['factory_service'] = $this->get('foo.baz')->getInstance();
171190
}
172191

192+
/**
193+
* Gets the 'factory_service_simple' service.
194+
*
195+
* This service is shared.
196+
* This method always returns the same instance of the service.
197+
*
198+
* @return \Bar A Bar instance.
199+
*/
200+
protected function getFactoryServiceSimpleService()
201+
{
202+
return $this->services['factory_service_simple'] = (new \SimpleFactoryClass('foo'))->getInstance();
203+
}
204+
173205
/**
174206
* Gets the 'foo' service.
175207
*

Tests/Fixtures/xml/services9.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@
8484
<service id="configured_service" class="stdClass">
8585
<configurator service="configurator_service" method="configureStdClass"/>
8686
</service>
87+
<service id="configurator_service_simple" class="ConfClass" public="false">
88+
<argument>bar</argument>
89+
</service>
90+
<service id="configured_service_simple" class="stdClass">
91+
<configurator service="configurator_service_simple" method="configureStdClass"/>
92+
</service>
8793
<service id="decorated" class="stdClass"/>
8894
<service id="decorator_service" class="stdClass" decorates="decorated"/>
8995
<service id="decorator_service_with_name" class="stdClass" decorates="decorated" decoration-inner-name="decorated.pif-pouf"/>
@@ -103,6 +109,12 @@
103109
<service id="service_from_static_method" class="Bar\FooClass">
104110
<factory class="Bar\FooClass" method="getInstance"/>
105111
</service>
112+
<service id="factory_simple" class="SimpleFactoryClass" public="false">
113+
<argument>foo</argument>
114+
</service>
115+
<service id="factory_service_simple" class="Bar">
116+
<factory service="factory_simple" method="getInstance"/>
117+
</service>
106118
<service id="alias_for_foo" alias="foo"/>
107119
<service id="alias_for_alias" alias="foo"/>
108120
</services>

Tests/Fixtures/yaml/services9.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ services:
6767
configured_service:
6868
class: stdClass
6969
configurator: ['@configurator_service', configureStdClass]
70+
configurator_service_simple:
71+
class: ConfClass
72+
public: false
73+
arguments: ['bar']
74+
configured_service_simple:
75+
class: stdClass
76+
configurator: ['@configurator_service_simple', configureStdClass]
7077
decorated:
7178
class: stdClass
7279
decorator_service:
@@ -93,5 +100,12 @@ services:
93100
service_from_static_method:
94101
class: Bar\FooClass
95102
factory: [Bar\FooClass, getInstance]
103+
factory_simple:
104+
class: SimpleFactoryClass
105+
public: false
106+
arguments: ['foo']
107+
factory_service_simple:
108+
class: Bar
109+
factory: ['@factory_simple', getInstance]
96110
alias_for_foo: '@foo'
97111
alias_for_alias: '@foo'

0 commit comments

Comments
 (0)