Skip to content

Commit bfa7df2

Browse files
Merge branch '6.0' into 6.1
* 6.0: [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 c66a4fd + 91b31a3 commit bfa7df2

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
@@ -505,8 +505,8 @@ public function has(string $id): bool
505505
*/
506506
public function get(string $id, int $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE): ?object
507507
{
508-
if ($this->isCompiled() && isset($this->removedIds[$id]) && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE >= $invalidBehavior) {
509-
return parent::get($id);
508+
if ($this->isCompiled() && isset($this->removedIds[$id])) {
509+
return ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE >= $invalidBehavior ? parent::get($id) : null;
510510
}
511511

512512
return $this->doGet($id, $invalidBehavior);
@@ -523,9 +523,9 @@ private function doGet(string $id, int $invalidBehavior = ContainerInterface::EX
523523
}
524524
try {
525525
if (ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $invalidBehavior) {
526-
return parent::get($id, $invalidBehavior);
526+
return $this->privates[$id] ?? parent::get($id, $invalidBehavior);
527527
}
528-
if ($service = parent::get($id, ContainerInterface::NULL_ON_INVALID_REFERENCE)) {
528+
if (null !== $service = $this->privates[$id] ?? parent::get($id, ContainerInterface::NULL_ON_INVALID_REFERENCE)) {
529529
return $service;
530530
}
531531
} catch (ServiceCircularReferenceException $e) {
@@ -1025,8 +1025,8 @@ private function createService(Definition $definition, array &$inlineServices, b
10251025

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

1028-
if (null !== $id && $definition->isShared() && isset($this->services[$id]) && ($tryProxy || !$definition->isLazy())) {
1029-
return $this->services[$id];
1028+
if (null !== $id && $definition->isShared() && (isset($this->services[$id]) || isset($this->privates[$id])) && ($tryProxy || !$definition->isLazy())) {
1029+
return $this->services[$id] ?? $this->privates[$id];
10301030
}
10311031

10321032
if (null !== $factory) {
@@ -1580,7 +1580,11 @@ private function shareService(Definition $definition, mixed $service, ?string $i
15801580
$inlineServices[$id ?? spl_object_hash($definition)] = $service;
15811581

15821582
if (null !== $id && $definition->isShared()) {
1583-
$this->services[$id] = $service;
1583+
if ($definition->isPrivate() && $this->isCompiled()) {
1584+
$this->privates[$id] = $service;
1585+
} else {
1586+
$this->services[$id] = $service;
1587+
}
15841588
unset($this->loading[$id]);
15851589
}
15861590
}

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
@@ -1079,10 +1079,19 @@ public function testPrivateServiceUser()
10791079
$container->addDefinitions([
10801080
'bar' => $fooDefinition,
10811081
'bar_user' => $fooUserDefinition->setPublic(true),
1082+
'bar_user2' => $fooUserDefinition->setPublic(true),
10821083
]);
10831084

10841085
$container->compile();
1086+
$this->assertNull($container->get('bar', $container::NULL_ON_INVALID_REFERENCE));
10851087
$this->assertInstanceOf(\BarClass::class, $container->get('bar_user')->bar);
1088+
1089+
// Ensure that accessing a public service with a shared private service
1090+
// does not make the private service available.
1091+
$this->assertNull($container->get('bar', $container::NULL_ON_INVALID_REFERENCE));
1092+
1093+
// Ensure the private service is still shared.
1094+
$this->assertSame($container->get('bar_user')->bar, $container->get('bar_user2')->bar);
10861095
}
10871096

10881097
public function testThrowsExceptionWhenSetServiceOnACompiledContainer()

0 commit comments

Comments
 (0)