Skip to content

Commit c33a5f6

Browse files
Merge branch '6.1' into 6.2
* 6.1: [Notifier] composer.json cleanup [HttpKernel] AbstractSessionListener should not override the cache lifetime for private responses [DependencyInjection] Shared private services becomes public after a public service is accessed [DI] Fix undefined class in test
2 parents 7dcae05 + bfa7df2 commit c33a5f6

File tree

4 files changed

+25
-17
lines changed

4 files changed

+25
-17
lines changed

ContainerBuilder.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -506,8 +506,8 @@ public function has(string $id): bool
506506
*/
507507
public function get(string $id, int $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE): ?object
508508
{
509-
if ($this->isCompiled() && isset($this->removedIds[$id]) && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE >= $invalidBehavior) {
510-
return parent::get($id);
509+
if ($this->isCompiled() && isset($this->removedIds[$id])) {
510+
return ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE >= $invalidBehavior ? parent::get($id) : null;
511511
}
512512

513513
return $this->doGet($id, $invalidBehavior);
@@ -524,9 +524,9 @@ private function doGet(string $id, int $invalidBehavior = ContainerInterface::EX
524524
}
525525
try {
526526
if (ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $invalidBehavior) {
527-
return parent::get($id, $invalidBehavior);
527+
return $this->privates[$id] ?? parent::get($id, $invalidBehavior);
528528
}
529-
if ($service = parent::get($id, ContainerInterface::NULL_ON_INVALID_REFERENCE)) {
529+
if (null !== $service = $this->privates[$id] ?? parent::get($id, ContainerInterface::NULL_ON_INVALID_REFERENCE)) {
530530
return $service;
531531
}
532532
} catch (ServiceCircularReferenceException $e) {
@@ -1029,8 +1029,8 @@ private function createService(Definition $definition, array &$inlineServices, b
10291029

10301030
$arguments = $this->doResolveServices($parameterBag->unescapeValue($parameterBag->resolveValue($arguments)), $inlineServices, $isConstructorArgument);
10311031

1032-
if (null !== $id && $definition->isShared() && isset($this->services[$id]) && (true === $tryProxy || !$definition->isLazy())) {
1033-
return $this->services[$id];
1032+
if (null !== $id && $definition->isShared() && (isset($this->services[$id]) || isset($this->privates[$id])) && (true === $tryProxy || !$definition->isLazy())) {
1033+
return $this->services[$id] ?? $this->privates[$id];
10341034
}
10351035

10361036
if (null !== $factory) {
@@ -1587,7 +1587,11 @@ private function shareService(Definition $definition, mixed $service, ?string $i
15871587
$inlineServices[$id ?? spl_object_hash($definition)] = $service;
15881588

15891589
if (null !== $id && $definition->isShared()) {
1590-
$this->services[$id] = $service;
1590+
if ($definition->isPrivate() && $this->isCompiled()) {
1591+
$this->privates[$id] = $service;
1592+
} else {
1593+
$this->services[$id] = $service;
1594+
}
15911595
unset($this->loading[$id]);
15921596
}
15931597
}

ReverseContainer.php

Lines 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
}

Tests/Compiler/ServiceLocatorTagPassTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,19 +184,19 @@ public function testIndexedByServiceIdWithDecoration()
184184

185185
$container->setDefinition(Service::class, $service);
186186

187-
$decorated = new Definition(Decorated::class);
187+
$decorated = new Definition(DecoratedService::class);
188188
$decorated->setPublic(true);
189189
$decorated->setDecoratedService(Service::class);
190190

191-
$container->setDefinition(Decorated::class, $decorated);
191+
$container->setDefinition(DecoratedService::class, $decorated);
192192

193193
$container->compile();
194194

195195
/** @var ServiceLocator $locator */
196196
$locator = $container->get(Locator::class)->locator;
197197
static::assertTrue($locator->has(Service::class));
198-
static::assertFalse($locator->has(Decorated::class));
199-
static::assertInstanceOf(Decorated::class, $locator->get(Service::class));
198+
static::assertFalse($locator->has(DecoratedService::class));
199+
static::assertInstanceOf(DecoratedService::class, $locator->get(Service::class));
200200
}
201201

202202
public function testDefinitionOrderIsTheSame()

Tests/ContainerBuilderTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,10 +1127,19 @@ public function testPrivateServiceUser()
11271127
$container->addDefinitions([
11281128
'bar' => $fooDefinition,
11291129
'bar_user' => $fooUserDefinition->setPublic(true),
1130+
'bar_user2' => $fooUserDefinition->setPublic(true),
11301131
]);
11311132

11321133
$container->compile();
1134+
$this->assertNull($container->get('bar', $container::NULL_ON_INVALID_REFERENCE));
11331135
$this->assertInstanceOf(\BarClass::class, $container->get('bar_user')->bar);
1136+
1137+
// Ensure that accessing a public service with a shared private service
1138+
// does not make the private service available.
1139+
$this->assertNull($container->get('bar', $container::NULL_ON_INVALID_REFERENCE));
1140+
1141+
// Ensure the private service is still shared.
1142+
$this->assertSame($container->get('bar_user')->bar, $container->get('bar_user2')->bar);
11341143
}
11351144

11361145
public function testThrowsExceptionWhenSetServiceOnACompiledContainer()

0 commit comments

Comments
 (0)