Skip to content

[DependencyInjection] Fix computing error messages involving service locators #54035

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ class CheckExceptionOnInvalidReferenceBehaviorPass extends AbstractRecursivePass
{
private $serviceLocatorContextIds = [];

/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
$this->serviceLocatorContextIds = [];
Expand Down Expand Up @@ -58,15 +55,7 @@ protected function processValue($value, bool $isRoot = false)
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 @@ -80,14 +69,21 @@ protected function processValue($value, bool $isRoot = false)
$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]) {
Expand All @@ -100,6 +96,31 @@ private function getAlternatives(string $id): array
}
}

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

/**
* @return mixed
*/
public function processValue($value, bool $isRoot = false)
{
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 @@ -82,7 +82,11 @@ public function testProcessDefinitionWithBindings()
$this->addToAssertionCount(1);
}

public function testWithErroredServiceLocator()
/**
* @testWith [true]
* [false]
*/
public function testWithErroredServiceLocator(bool $inline)
{
$this->expectException(ServiceNotFoundException::class);
$this->expectExceptionMessage('The service "foo" in the container provided to "bar" has a dependency on a non-existent service "baz".');
Expand All @@ -91,11 +95,17 @@ public function testWithErroredServiceLocator()
ServiceLocatorTagPass::register($container, ['foo' => new Reference('baz')], 'bar');

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

public function testWithErroredHiddenService()
/**
* @testWith [true]
* [false]
*/
public function testWithErroredHiddenService(bool $inline)
{
$this->expectException(ServiceNotFoundException::class);
$this->expectExceptionMessage('The service "bar" has a dependency on a non-existent service "foo".');
Expand All @@ -104,7 +114,9 @@ public function testWithErroredHiddenService()
ServiceLocatorTagPass::register($container, ['foo' => new Reference('foo')], 'bar');

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

Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy