Skip to content

Commit

Permalink
Merge branch '5.4' into 6.4
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
nicolas-grekas committed Feb 22, 2024
2 parents ab84c89 + cc1fb23 commit 6236e5e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Compiler/AbstractRecursivePass.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ protected function processValue(mixed $value, bool $isRoot = false)
continue;
}
if ($isRoot) {
if ($v->hasTag('container.excluded')) {
if ($v instanceof Definition && $v->hasTag('container.excluded')) {
continue;
}
$this->currentId = $k;
Expand Down
45 changes: 33 additions & 12 deletions Compiler/CheckExceptionOnInvalidReferenceBehaviorPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,7 @@ protected function processValue(mixed $value, bool $isRoot = false): mixed
if (isset($this->serviceLocatorContextIds[$currentId])) {
$currentId = $this->serviceLocatorContextIds[$currentId];
$locator = $this->container->getDefinition($this->currentId)->getFactory()[0];

foreach ($locator->getArgument(0) as $k => $v) {
if ($v->getValues()[0] === $value) {
if ($k !== $id) {
$currentId = $k.'" in the container provided to "'.$currentId;
}
throw new ServiceNotFoundException($id, $currentId, null, $this->getAlternatives($id));
}
}
$this->throwServiceNotFoundException($value, $currentId, $locator->getArgument(0));
}

if ('.' === $currentId[0] && $graph->hasNode($currentId)) {
Expand All @@ -82,14 +74,21 @@ protected function processValue(mixed $value, bool $isRoot = false): mixed
$currentId = $sourceId;
break;
}

if (isset($this->serviceLocatorContextIds[$sourceId])) {
$currentId = $this->serviceLocatorContextIds[$sourceId];
$locator = $this->container->getDefinition($this->currentId);
$this->throwServiceNotFoundException($value, $currentId, $locator->getArgument(0));
}
}
}

throw new ServiceNotFoundException($id, $currentId, null, $this->getAlternatives($id));
$this->throwServiceNotFoundException($value, $currentId, $value);
}

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

return $alternatives;
$pass = new class() extends AbstractRecursivePass {
public Reference $ref;
public string $sourceId;
public array $alternatives;

public function processValue(mixed $value, bool $isRoot = false): mixed
{
if ($this->ref !== $value) {
return parent::processValue($value, $isRoot);
}
$sourceId = $this->sourceId;
if (null !== $this->currentId && $this->currentId !== (string) $value) {
$sourceId = $this->currentId.'" in the container provided to "'.$sourceId;
}

throw new ServiceNotFoundException((string) $value, $sourceId, null, $this->alternatives);
}
};
$pass->ref = $ref;
$pass->sourceId = $sourceId;
$pass->alternatives = $alternatives;

$pass->processValue($value, true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,29 +84,41 @@ public function testProcessDefinitionWithBindings()
$this->addToAssertionCount(1);
}

public function testWithErroredServiceLocator()
/**
* @testWith [true]
* [false]
*/
public function testWithErroredServiceLocator(bool $inline)
{
$container = new ContainerBuilder();

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

(new AnalyzeServiceReferencesPass())->process($container);
(new InlineServiceDefinitionsPass())->process($container);
if ($inline) {
(new InlineServiceDefinitionsPass())->process($container);
}

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

$this->process($container);
}

public function testWithErroredHiddenService()
/**
* @testWith [true]
* [false]
*/
public function testWithErroredHiddenService(bool $inline)
{
$container = new ContainerBuilder();

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

(new AnalyzeServiceReferencesPass())->process($container);
(new InlineServiceDefinitionsPass())->process($container);
if ($inline) {
(new InlineServiceDefinitionsPass())->process($container);
}

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

0 comments on commit 6236e5e

Please sign in to comment.