Skip to content

Commit 6236e5e

Browse files
Merge branch '5.4' into 6.4
* 5.4: [DependencyInjection] Fix computing error messages involving service locators [Serializer] Fix unknown types normalization type when know type [ErrorHandler] Fix parsing messages that contain anonymous classes on PHP >= 8.3.3 [Validator] Review Romanian (ro) translations [Console] Fix display of Table on Windows OS [FrameworkBundle] Fix config builder with extensions extended in `build()` [WebProfilerBundle] disable turbo in web profiler toolbar to avoid link prefetching explicitly cast boolean SSL stream options return the unchanged text if preg_replace_callback() fails the 'use_notify' option is on the factory, not on the postgres connection class review translations
2 parents ab84c89 + cc1fb23 commit 6236e5e

File tree

3 files changed

+50
-17
lines changed

3 files changed

+50
-17
lines changed

Compiler/AbstractRecursivePass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ protected function processValue(mixed $value, bool $isRoot = false)
8282
continue;
8383
}
8484
if ($isRoot) {
85-
if ($v->hasTag('container.excluded')) {
85+
if ($v instanceof Definition && $v->hasTag('container.excluded')) {
8686
continue;
8787
}
8888
$this->currentId = $k;

Compiler/CheckExceptionOnInvalidReferenceBehaviorPass.php

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,7 @@ protected function processValue(mixed $value, bool $isRoot = false): mixed
6060
if (isset($this->serviceLocatorContextIds[$currentId])) {
6161
$currentId = $this->serviceLocatorContextIds[$currentId];
6262
$locator = $this->container->getDefinition($this->currentId)->getFactory()[0];
63-
64-
foreach ($locator->getArgument(0) as $k => $v) {
65-
if ($v->getValues()[0] === $value) {
66-
if ($k !== $id) {
67-
$currentId = $k.'" in the container provided to "'.$currentId;
68-
}
69-
throw new ServiceNotFoundException($id, $currentId, null, $this->getAlternatives($id));
70-
}
71-
}
63+
$this->throwServiceNotFoundException($value, $currentId, $locator->getArgument(0));
7264
}
7365

7466
if ('.' === $currentId[0] && $graph->hasNode($currentId)) {
@@ -82,14 +74,21 @@ protected function processValue(mixed $value, bool $isRoot = false): mixed
8274
$currentId = $sourceId;
8375
break;
8476
}
77+
78+
if (isset($this->serviceLocatorContextIds[$sourceId])) {
79+
$currentId = $this->serviceLocatorContextIds[$sourceId];
80+
$locator = $this->container->getDefinition($this->currentId);
81+
$this->throwServiceNotFoundException($value, $currentId, $locator->getArgument(0));
82+
}
8583
}
8684
}
8785

88-
throw new ServiceNotFoundException($id, $currentId, null, $this->getAlternatives($id));
86+
$this->throwServiceNotFoundException($value, $currentId, $value);
8987
}
9088

91-
private function getAlternatives(string $id): array
89+
private function throwServiceNotFoundException(Reference $ref, string $sourceId, $value): void
9290
{
91+
$id = (string) $ref;
9392
$alternatives = [];
9493
foreach ($this->container->getServiceIds() as $knownId) {
9594
if ('' === $knownId || '.' === $knownId[0] || $knownId === $this->currentId) {
@@ -102,6 +101,28 @@ private function getAlternatives(string $id): array
102101
}
103102
}
104103

105-
return $alternatives;
104+
$pass = new class() extends AbstractRecursivePass {
105+
public Reference $ref;
106+
public string $sourceId;
107+
public array $alternatives;
108+
109+
public function processValue(mixed $value, bool $isRoot = false): mixed
110+
{
111+
if ($this->ref !== $value) {
112+
return parent::processValue($value, $isRoot);
113+
}
114+
$sourceId = $this->sourceId;
115+
if (null !== $this->currentId && $this->currentId !== (string) $value) {
116+
$sourceId = $this->currentId.'" in the container provided to "'.$sourceId;
117+
}
118+
119+
throw new ServiceNotFoundException((string) $value, $sourceId, null, $this->alternatives);
120+
}
121+
};
122+
$pass->ref = $ref;
123+
$pass->sourceId = $sourceId;
124+
$pass->alternatives = $alternatives;
125+
126+
$pass->processValue($value, true);
106127
}
107128
}

Tests/Compiler/CheckExceptionOnInvalidReferenceBehaviorPassTest.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,29 +84,41 @@ public function testProcessDefinitionWithBindings()
8484
$this->addToAssertionCount(1);
8585
}
8686

87-
public function testWithErroredServiceLocator()
87+
/**
88+
* @testWith [true]
89+
* [false]
90+
*/
91+
public function testWithErroredServiceLocator(bool $inline)
8892
{
8993
$container = new ContainerBuilder();
9094

9195
ServiceLocatorTagPass::register($container, ['foo' => new Reference('baz')], 'bar');
9296

9397
(new AnalyzeServiceReferencesPass())->process($container);
94-
(new InlineServiceDefinitionsPass())->process($container);
98+
if ($inline) {
99+
(new InlineServiceDefinitionsPass())->process($container);
100+
}
95101

96102
$this->expectException(ServiceNotFoundException::class);
97103
$this->expectExceptionMessage('The service "foo" in the container provided to "bar" has a dependency on a non-existent service "baz".');
98104

99105
$this->process($container);
100106
}
101107

102-
public function testWithErroredHiddenService()
108+
/**
109+
* @testWith [true]
110+
* [false]
111+
*/
112+
public function testWithErroredHiddenService(bool $inline)
103113
{
104114
$container = new ContainerBuilder();
105115

106116
ServiceLocatorTagPass::register($container, ['foo' => new Reference('foo')], 'bar');
107117

108118
(new AnalyzeServiceReferencesPass())->process($container);
109-
(new InlineServiceDefinitionsPass())->process($container);
119+
if ($inline) {
120+
(new InlineServiceDefinitionsPass())->process($container);
121+
}
110122

111123
$this->expectException(ServiceNotFoundException::class);
112124
$this->expectExceptionMessage('The service "bar" has a dependency on a non-existent service "foo".');

0 commit comments

Comments
 (0)