Skip to content

Commit 91b31a3

Browse files
Merge branch '5.4' into 6.0
* 5.4: [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 0d71037 + 4fc0a1b commit 91b31a3

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) {
@@ -1018,8 +1018,8 @@ private function createService(Definition $definition, array &$inlineServices, b
10181018
}
10191019
}
10201020

1021-
if (null !== $id && $definition->isShared() && isset($this->services[$id]) && ($tryProxy || !$definition->isLazy())) {
1022-
return $this->services[$id];
1021+
if (null !== $id && $definition->isShared() && (isset($this->services[$id]) || isset($this->privates[$id])) && ($tryProxy || !$definition->isLazy())) {
1022+
return $this->services[$id] ?? $this->privates[$id];
10231023
}
10241024

10251025
if (null !== $factory) {
@@ -1575,7 +1575,11 @@ private function shareService(Definition $definition, mixed $service, ?string $i
15751575
$inlineServices[$id ?? spl_object_hash($definition)] = $service;
15761576

15771577
if (null !== $id && $definition->isShared()) {
1578-
$this->services[$id] = $service;
1578+
if ($definition->isPrivate() && $this->isCompiled()) {
1579+
$this->privates[$id] = $service;
1580+
} else {
1581+
$this->services[$id] = $service;
1582+
}
15791583
unset($this->loading[$id]);
15801584
}
15811585
}

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
@@ -1066,10 +1066,19 @@ public function testPrivateServiceUser()
10661066
$container->addDefinitions([
10671067
'bar' => $fooDefinition,
10681068
'bar_user' => $fooUserDefinition->setPublic(true),
1069+
'bar_user2' => $fooUserDefinition->setPublic(true),
10691070
]);
10701071

10711072
$container->compile();
1073+
$this->assertNull($container->get('bar', $container::NULL_ON_INVALID_REFERENCE));
10721074
$this->assertInstanceOf(\BarClass::class, $container->get('bar_user')->bar);
1075+
1076+
// Ensure that accessing a public service with a shared private service
1077+
// does not make the private service available.
1078+
$this->assertNull($container->get('bar', $container::NULL_ON_INVALID_REFERENCE));
1079+
1080+
// Ensure the private service is still shared.
1081+
$this->assertSame($container->get('bar_user')->bar, $container->get('bar_user2')->bar);
10731082
}
10741083

10751084
public function testThrowsExceptionWhenSetServiceOnACompiledContainer()

0 commit comments

Comments
 (0)