Skip to content

Commit

Permalink
[DI] Dont use Container::get() when fetching private services internally
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Aug 23, 2016
1 parent 703db1e commit d1d8d3c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
7 changes: 6 additions & 1 deletion src/Symfony/Component/DependencyInjection/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,14 @@ class Container implements ResettableContainerInterface

protected $services = array();
protected $methodMap = array();
protected $privates = array();
protected $aliases = array();
protected $loading = array();

/**
* @deprecated since 3.2, to be removed in 4.0
*/
protected $privates = array();

private $underscoreMap = array('_' => '', '.' => '_', '\\' => '_');

/**
Expand Down Expand Up @@ -181,6 +185,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
Original file line number Diff line number Diff line change
Expand Up @@ -1376,6 +1376,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
Original file line number Diff line number Diff line change
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 d1d8d3c

Please sign in to comment.