Skip to content

Commit be1eaaa

Browse files
committed
[DependencyInjection] Avoid call_user_func in dumped containers.
1 parent 67ae8fa commit be1eaaa

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

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

+13
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,12 @@ private function addServiceConfigurator($id, $definition, $variableName = 'insta
487487
return sprintf(" %s->%s(\$%s);\n", $this->dumpValue($callable[0]), $callable[1], $variableName);
488488
}
489489

490+
$class = $this->dumpValue($callable[0]);
491+
// If the class is a string we can optimize call_user_func away
492+
if (strpos($class, "'") === 0) {
493+
return sprintf(" %s::%s(\$%s);\n", substr($class, 1, -1), $callable[1], $variableName);
494+
}
495+
490496
return sprintf(" call_user_func(array(%s, '%s'), \$%s);\n", $this->dumpValue($callable[0]), $callable[1], $variableName);
491497
}
492498

@@ -691,6 +697,13 @@ private function addNewInstance($id, Definition $definition, $return, $instantia
691697

692698
if (null !== $definition->getFactoryMethod()) {
693699
if (null !== $definition->getFactoryClass()) {
700+
$class = $this->dumpValue($definition->getFactoryClass());
701+
702+
// If the class is a string we can optimize call_user_func away
703+
if (strpos($class, "'") === 0) {
704+
return sprintf(" $return{$instantiation}%s::%s(%s);\n", substr($class, 1, -1), $definition->getFactoryMethod(), $arguments ? implode(', ', $arguments) : '');
705+
}
706+
694707
return sprintf(" $return{$instantiation}call_user_func(array(%s, '%s')%s);\n", $this->dumpValue($definition->getFactoryClass()), $definition->getFactoryMethod(), $arguments ? ', '.implode(', ', $arguments) : '');
695708
}
696709

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ protected function getFooService()
138138
{
139139
$a = $this->get('foo.baz');
140140

141-
$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);
141+
$this->services['foo'] = $instance = FooClass::getInstance('foo', $a, array($this->getParameter('foo') => 'foo is '.$this->getParameter('foo').'', 'foobar' => $this->getParameter('foo')), true, $this);
142142

143143
$instance->setBar($this->get('bar'));
144144
$instance->initialize();

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ protected function getFooService()
148148
{
149149
$a = $this->get('foo.baz');
150150

151-
$this->services['foo'] = $instance = call_user_func(array('FooClass', 'getInstance'), 'foo', $a, array('bar' => 'foo is bar', 'foobar' => 'bar'), true, $this);
151+
$this->services['foo'] = $instance = FooClass::getInstance('foo', $a, array('bar' => 'foo is bar', 'foobar' => 'bar'), true, $this);
152152

153153
$instance->setBar($this->get('bar'));
154154
$instance->initialize();
@@ -169,9 +169,9 @@ protected function getFooService()
169169
*/
170170
protected function getFoo_BazService()
171171
{
172-
$this->services['foo.baz'] = $instance = call_user_func(array('BazClass', 'getInstance'));
172+
$this->services['foo.baz'] = $instance = BazClass::getInstance();
173173

174-
call_user_func(array('BazClass', 'configureStatic1'), $instance);
174+
BazClass::configureStatic1($instance);
175175

176176
return $instance;
177177
}

0 commit comments

Comments
 (0)