10000 [DependencyInjection] Avoid call_user_func in dumped containers. by realityking · Pull Request #9807 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DependencyInjection] Avoid call_user_func in dumped containers. #9807

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 18, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,12 @@ private function addServiceConfigurator($id, $definition, $variableName = 'insta
return sprintf(" %s->%s(\$%s);\n", $this->dumpValue($callable[0]), $callable[1], $variableName);
}

$class = $this->dumpValue($callable[0]);
// If the class is a string we can optimize call_user_func away
if (strpos($class, "'") === 0) {
return sprintf(" %s::%s(\$%s);\n", substr($class, 1, -1), $callable[1], $variableName);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The class name should be dumped as a fully qualified class name, not as a class name relative to the global namespace, to support dumping containers in a namespace

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean just adding a \ in front of the class. Correct?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes. It is not needed when it is dumped in a string for call_user_func, but it is needed for the direct call

}

return sprintf(" call_user_func(array(%s, '%s'), \$%s);\n", $this->dumpValue($callable[0]), $callable[1], $variableName);
}

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

if (null !== $definition->getFactoryMethod()) {
if (null !== $definition->getFactoryClass()) {
$class = $this->dumpValue($definition->getFactoryClass());

// If the class is a string we can optimize call_user_func away
if (strpos($class, "'") === 0) {
return sprintf(" $return{$instantiation}%s::%s(%s);\n", substr($class, 1, -1), $definition->getFactoryMethod(), $arguments ? implode(', ', $arguments) : '');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@realityking: This change has caused Doctrine ODM Bundle to break, and any similar bundles that use factory class.

Class: Doctrine\ODM\MongoDB\DocumentManager
var_export($value, true): Doctrine\\ODM\\MongoDB\\DocumentManager

Notice that var_export which is used in dumpValue causes a double escape of the class.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the info. There's a fix in progress here: #9816

}

return sprintf(" $return{$instantiation}call_user_func(array(%s, '%s')%s);\n", $this->dumpValue($definition->getFactoryClass()), $definition->getFactoryMethod(), $arguments ? ', '.implode(', ', $arguments) : '');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ protected function getFooService()
{
$a = $this->get('foo.baz');

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

$instance->setBar($this->get('bar'));
$instance->initialize();
Expand Down
Original file line number Dif 8545 f line number Diff line change
Expand Up @@ -148,7 +148,7 @@ protected function getFooService()
{
$a = $this->get('foo.baz');

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

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

call_user_func(array('BazClass', 'configureStatic1'), $instance);
BazClass::configureStatic1($instance);

return $instance;
}
Expand Down
0