Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a9c79fb

Browse filesBrowse files
[DI] Dont use Container::get() when fetching private services internally
1 parent 703db1e commit a9c79fb
Copy full SHA for a9c79fb

File tree

3 files changed

+11
-5
lines changed
Filter options

3 files changed

+11
-5
lines changed

‎src/Symfony/Component/DependencyInjection/Container.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Container.php
+1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
+5Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php
+5-5Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.