Skip to content

Commit 15cb83b

Browse files
committed
bug #19708 [DI] Dont use Container::get() when fetching private services 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
2 parents 03a824a + a9c79fb commit 15cb83b

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

src/Symfony/Component/DependencyInjection/Container.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ public function set($id, $service)
181181
if (isset($this->privates[$id])) {
182182
if (null === $service) {
183183
@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);
184+
unset($this->privates[$id]);
184185
} else {
185186
@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);
186187
}

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

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

1400+
if ($this->container->hasDefinition($id) && !$this->container->getDefinition($id)->isPublic()) {
1401+
// The following is PHP 5.5 syntax for what could be written as "(\$this->services['$id'] ?? \$this->{$this->generateMethodName($id)}())" on PHP>=7.0
1402+
1403+
return "\${(\$_ = isset(\$this->services['$id']) ? \$this->services['$id'] : \$this->{$this->generateMethodName($id)}()) && false ?: '_'}";
1404+
}
14001405
if (null !== $reference && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $reference->getInvalidBehavior()) {
14011406
return sprintf('$this->get(\'%s\', ContainerInterface::NULL_ON_INVALID_REFERENCE)', $id);
14021407
} 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)