Skip to content

Commit 375a2c7

Browse files
committed
minor #9807 [DependencyInjection] Avoid call_user_func in dumped containers. (realityking)
This PR was merged into the 2.5-dev branch. Discussion ---------- [DependencyInjection] Avoid call_user_func in dumped containers. | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - This is the second commit from #9432. When using static methods for either the factory or as the configurator we can avoid using call_user_func and directly use the class:method notation. This is faster (about 5 times, but we're talking milliseconds here) but I think the resulting code is also much easier to read. The code to use call_user_func has to remain in PhpDumper because in the uncompiled container they still get used. Commits ------- be1eaaa [DependencyInjection] Avoid call_user_func in dumped containers.
2 parents 67ae8fa + be1eaaa commit 375a2c7

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

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

Lines changed: 13 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 3 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy