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 ece0c89

Browse filesBrowse files
alexpottnicolas-grekas
authored andcommitted
[DependencyInjection] Shared private services becomes public after a public service is accessed
1 parent 5ac1693 commit ece0c89
Copy full SHA for ece0c89

File tree

3 files changed

+25
-13
lines changed
Filter options

3 files changed

+25
-13
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/ContainerBuilder.php
+15-7Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -546,8 +546,12 @@ public function has(string $id)
546546
*/
547547
public function get(string $id, int $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE)
548548
{
549-
if ($this->isCompiled() && isset($this->removedIds[$id]) && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE >= $invalidBehavior) {
550-
return parent::get($id);
549+
if ($this->isCompiled() && isset($this->removedIds[$id])) {
550+
if (ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE >= $invalidBehavior) {
551+
return parent::get($id);
552+
}
553+
554+
return null;
551555
}
552556

553557
return $this->doGet($id, $invalidBehavior);
@@ -564,9 +568,9 @@ private function doGet(string $id, int $invalidBehavior = ContainerInterface::EX
564568
}
565569
try {
566570
if (ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $invalidBehavior) {
567-
return parent::get($id, $invalidBehavior);
571+
return $this->privates[$id] ?? parent::get($id, $invalidBehavior);
568572
}
569-
if ($service = parent::get($id, ContainerInterface::NULL_ON_INVALID_REFERENCE)) {
573+
if (null !== $service = $this->privates[$id] ?? parent::get($id, ContainerInterface::NULL_ON_INVALID_REFERENCE)) {
570574
return $service;
571575
}
572576
} catch (ServiceCircularReferenceException $e) {
@@ -1085,8 +1089,8 @@ private function createService(Definition $definition, array &$inlineServices, b
10851089
}
10861090
}
10871091

1088-
if (null !== $id && $definition->isShared() && isset($this->services[$id]) && ($tryProxy || !$definition->isLazy())) {
1089-
return $this->services[$id];
1092+
if (null !== $id && $definition->isShared() && (isset($this->services[$id]) || isset($this->privates[$id])) && ($tryProxy || !$definition->isLazy())) {
1093+
return $this->services[$id] ?? $this->privates[$id];
10901094
}
10911095

10921096
if (null !== $factory) {
@@ -1665,7 +1669,11 @@ private function shareService(Definition $definition, $service, ?string $id, arr
16651669
$inlineServices[$id ?? spl_object_hash($definition)] = $service;
16661670

16671671
if (null !== $id && $definition->isShared()) {
1668-
$this->services[$id] = $service;
1672+
if ($definition->isPrivate() && $this->isCompiled()) {
1673+
$this->privates[$id] = $service;
1674+
} else {
1675+
$this->services[$id] = $service;
1676+
}
16691677
unset($this->loading[$id]);
16701678
}
16711679
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/ReverseContainer.php
+1-6Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,6 @@ public function getId(object $service): ?string
6363
*/
6464
public function getService(string $id): object
6565
{
66-
if ($this->serviceContainer->has($id)) {
67-
return $this->serviceContainer->get($id);
68-
}
69-
7066
if ($this->reversibleLocator->has($id)) {
7167
return $this->reversibleLocator->get($id);
7268
}
@@ -75,7 +71,6 @@ public function getService(string $id): object
7571
throw new ServiceNotFoundException($id, null, null, [], sprintf('The "%s" service is private and cannot be accessed by reference. You should either make it public, or tag it as "%s".', $id, $this->tagName));
7672
}
7773

78-
// will throw a ServiceNotFoundException
79-
$this->serviceContainer->get($id);
74+
return $this->serviceContainer->get($id);
8075
}
8176
}

‎src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,10 +1105,19 @@ public function testPrivateServiceUser()
11051105
$container->addDefinitions([
11061106
'bar' => $fooDefinition,
11071107
'bar_user' => $fooUserDefinition->setPublic(true),
1108+
'bar_user2' => $fooUserDefinition->setPublic(true),
11081109
]);
11091110

11101111
$container->compile();
1112+
$this->assertNull($container->get('bar', $container::NULL_ON_INVALID_REFERENCE));
11111113
$this->assertInstanceOf(\BarClass::class, $container->get('bar_user')->bar);
1114+
1115+
// Ensure that accessing a public service with a shared private service
1116+
// does not make the private service available.
1117+
$this->assertNull($container->get('bar', $container::NULL_ON_INVALID_REFERENCE));
1118+
1119+
// Ensure the private service is still shared.
1120+
$this->assertSame($container->get('bar_user')->bar, $container->get('bar_user2')->bar);
11121121
}
11131122

11141123
public function testThrowsExceptionWhenSetServiceOnACompiledContainer()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.