Skip to content

Commit d1d8d3c

Browse files
[DI] Dont use Container::get() when fetching private services internally
1 parent 703db1e commit d1d8d3c

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

src/Symfony/Component/DependencyInjection/Container.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,14 @@ class Container implements ResettableContainerInterface
6565

6666
protected $services = array();
6767
protected $methodMap = array();
68-
protected $privates = array();
6968
protected $aliases = array();
7069
protected $loading = array();
7170

71+
/**
72+
* @deprecated since 3.2, to be removed in 4.0
73+
*/
74+
protected $privates = array();
75+
7276
private $underscoreMap = array('_' => '', '.' => '_', '\\' => '_');
7377

7478
/**
@@ -181,6 +185,7 @@ public function set($id, $service)
181185
if (isset($this->privates[$id])) {
182186
if (null === $service) {
183187
@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);
188+
unset($this->privates[$id]);
184189
} else {
185190
@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);
186191
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,6 +1376,11 @@ private function getServiceCall($id, Reference $reference = null)
13761376
return '$this';
13771377
}
13781378

1379+
if ($this->container->hasDefinition($id) && !$this->container->getDefinition($id)->isPublic()) {
1380+
// The following is PHP 5.5 syntax for what could be written as "(\$this->services['$id'] ?? \$this->{$this->generateMethodName($id)}())" on PHP>=7.0
1381+
1382+
return "\${(\$_ = isset(\$this->services['$id']) ? \$this->services['$id'] : \$this->{$this->generateMethodName($id)}()) && false ?: '_'}";
1383+
}
13791384
if (null !== $reference && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $reference->getInvalidBehavior()) {
13801385
return sprintf('$this->get(\'%s\', ContainerInterface::NULL_ON_INVALID_REFERENCE)', $id);
13811386
} else {

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ protected function getConfiguredServiceService()
110110
{
111111
$this->services['configured_service'] = $instance = new \stdClass();
112112

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

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

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

132132
return $instance;
133133
}
@@ -211,7 +211,7 @@ protected function getFactoryServiceService()
211211
*/
212212
protected function getFactoryServiceSimpleService()
213213
{
214-
return $this->services['factory_service_simple'] = $this->get('factory_simple')->getInstance();
214+
return $this->services['factory_service_simple'] = ${($_ = isset($this->services['factory_simple']) ? $this->services['factory_simple'] : $this->getFactorySimpleService()) && false ?: '_'}->getInstance();
215215
}
216216

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

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

284284
return $instance;
285285
}
@@ -321,7 +321,7 @@ protected function getMethodCall1Service()
321321
*/
322322
protected function getNewFactoryServiceService()
323323
{
324-
$this->services['new_factory_service'] = $instance = $this->get('new_factory')->getInstance();
324+
$this->services['new_factory_service'] = $instance = ${($_ = isset($this->services['new_factory']) ? $this->services['new_factory'] : $this->getNewFactoryService()) && false ?: '_'}->getInstance();
325325

326326
$instance->foo = 'bar';
327327

0 commit comments

Comments
 (0)