Skip to content

Commit

Permalink
bug #19708 [DI] Dont use Container::get() when fetching private servi…
Browse files Browse the repository at this point in the history
…ces internally (nicolas-grekas)

This PR was merged into the 3.2-dev branch.

Discussion
----------

[DI] Dont use Container::get() when fetching private services internally

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #19683, #19682, #19680
| License       | MIT

As spotted by @wouterj, we forgot to remove the deprecation notice when doing internal calls to get private services.

Yet, we don't need to get through this `get()` method, because we can already resolve many things at compile time for private services. This will provide another small performance optim, and fix the issue.

Commits
-------

a9c79fb [DI] Dont use Container::get() when fetching private services internally
  • Loading branch information
fabpot committed Sep 1, 2016
2 parents 03a824a + a9c79fb commit 15cb83b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/Symfony/Component/DependencyInjection/Container.php
Expand Up @@ -181,6 +181,7 @@ public function set($id, $service)
if (isset($this->privates[$id])) {
if (null === $service) {
@trigger_error(sprintf('Unsetting the "%s" private service is deprecated since Symfony 3.2 and won\'t be supported anymore in Symfony 4.0.', $id), E_USER_DEPRECATED);
unset($this->privates[$id]);
} else {
@trigger_error(sprintf('Setting the "%s" private service is deprecated since Symfony 3.2 and won\'t be supported anymore in Symfony 4.0. A new public service will be created instead.', $id), E_USER_DEPRECATED);
}
Expand Down
Expand Up @@ -1397,6 +1397,11 @@ private function getServiceCall($id, Reference $reference = null)
return '$this';
}

if ($this->container->hasDefinition($id) && !$this->container->getDefinition($id)->isPublic()) {
// The following is PHP 5.5 syntax for what could be written as "(\$this->services['$id'] ?? \$this->{$this->generateMethodName($id)}())" on PHP>=7.0

return "\${(\$_ = isset(\$this->services['$id']) ? \$this->services['$id'] : \$this->{$this->generateMethodName($id)}()) && false ?: '_'}";
}
if (null !== $reference && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $reference->getInvalidBehavior()) {
return sprintf('$this->get(\'%s\', ContainerInterface::NULL_ON_INVALID_REFERENCE)', $id);
} else {
Expand Down
Expand Up @@ -110,7 +110,7 @@ protected function getConfiguredServiceService()
{
$this->services['configured_service'] = $instance = new \stdClass();

$this->get('configurator_service')->configureStdClass($instance);
${($_ = isset($this->services['configurator_service']) ? $this->services['configurator_service'] : $this->getConfiguratorServiceService()) && false ?: '_'}->configureStdClass($instance);

return $instance;
}
Expand All @@ -127,7 +127,7 @@ protected function getConfiguredServiceSimpleService()
{
$this->services['configured_service_simple'] = $instance = new \stdClass();

$this->get('configurator_service_simple')->configureStdClass($instance);
${($_ = isset($this->services['configurator_service_simple']) ? $this->services['configurator_service_simple'] : $this->getConfiguratorServiceSimpleService()) && false ?: '_'}->configureStdClass($instance);

return $instance;
}
Expand Down Expand Up @@ -211,7 +211,7 @@ protected function getFactoryServiceService()
*/
protected function getFactoryServiceSimpleService()
{
return $this->services['factory_service_simple'] = $this->get('factory_simple')->getInstance();
return $this->services['factory_service_simple'] = ${($_ = isset($this->services['factory_simple']) ? $this->services['factory_simple'] : $this->getFactorySimpleService()) && false ?: '_'}->getInstance();
}

/**
Expand Down Expand Up @@ -279,7 +279,7 @@ protected function getFooWithInlineService()
{
$this->services['foo_with_inline'] = $instance = new \Foo();

$instance->setBar($this->get('inlined'));
$instance->setBar(${($_ = isset($this->services['inlined']) ? $this->services['inlined'] : $this->getInlinedService()) && false ?: '_'});

return $instance;
}
Expand Down Expand Up @@ -321,7 +321,7 @@ protected function getMethodCall1Service()
*/
protected function getNewFactoryServiceService()
{
$this->services['new_factory_service'] = $instance = $this->get('new_factory')->getInstance();
$this->services['new_factory_service'] = $instance = ${($_ = isset($this->services['new_factory']) ? $this->services['new_factory'] : $this->getNewFactoryService()) && false ?: '_'}->getInstance();

$instance->foo = 'bar';

Expand Down

0 comments on commit 15cb83b

Please sign in to comment.