Skip to content

[DependencyInjection] Skip errored definitions deep-referenced as runtime exceptions #50763

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
Jun 24, 2023
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 @@ -11,6 +11,8 @@

namespace Symfony\Component\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Argument\ArgumentInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
Expand All @@ -23,34 +25,101 @@
*/
class DefinitionErrorExceptionPass extends AbstractRecursivePass
{
private $erroredDefinitions = [];
private $targetReferences = [];
private $sourceReferences = [];

/**
* @return void
*/
public function process(ContainerBuilder $container)
{
try {
parent::process($container);

if (!$this->erroredDefinitions) {
return;
}

$runtimeIds = [];

foreach ($this->sourceReferences as $id => $sourceIds) {
foreach ($sourceIds as $sourceId => $isRuntime) {
if (!$isRuntime) {
continue 2;
}
}

unset($this->erroredDefinitions[$id]);
$runtimeIds[$id] = $id;
}

if (!$this->erroredDefinitions) {
return;
}

foreach ($this->targetReferences as $id => $targetIds) {
if (!isset($this->sourceReferences[$id]) || isset($runtimeIds[$id]) || isset($this->erroredDefinitions[$id])) {
continue;
}
foreach ($this->targetReferences[$id] as $targetId => $isRuntime) {
foreach ($this->sourceReferences[$id] as $sourceId => $isRuntime) {
if ($sourceId !== $targetId) {
$this->sourceReferences[$targetId][$sourceId] = false;
$this->targetReferences[$sourceId][$targetId] = false;
}
}
}

unset($this->sourceReferences[$id]);
}

foreach ($this->erroredDefinitions as $id => $definition) {
if (isset($this->sourceReferences[$id])) {
continue;
}

// only show the first error so the user can focus on it
$errors = $definition->getErrors();

throw new RuntimeException(reset($errors));
}
} finally {
$this->erroredDefinitions = [];
$this->targetReferences = [];
$this->sourceReferences = [];
}
}

/**
* {@inheritdoc}
*/
protected function processValue($value, bool $isRoot = false)
{
if (!$value instanceof Definition || !$value->hasErrors()) {
return parent::processValue($value, $isRoot);
if ($value instanceof ArgumentInterface) {
parent::processValue($value->getValues());

return $value;
}

if ($isRoot && !$value->isPublic()) {
$graph = $this->container->getCompiler()->getServiceReferenceGraph();
$runtimeException = false;
foreach ($graph->getNode($this->currentId)->getInEdges() as $edge) {
if (!$edge->getValue() instanceof Reference || ContainerInterface::RUNTIME_EXCEPTION_ON_INVALID_REFERENCE !== $edge->getValue()->getInvalidBehavior()) {
$runtimeException = false;
break;
}
$runtimeException = true;
}
if ($runtimeException) {
return parent::processValue($value, $isRoot);
if ($value instanceof Reference && $this->currentId !== $targetId = (string) $value) {
if (ContainerInterface::RUNTIME_EXCEPTION_ON_INVALID_REFERENCE === $value->getInvalidBehavior()) {
$this->sourceReferences[$targetId][$this->currentId] ?? $this->sourceReferences[$targetId][$this->currentId] = true;
$this->targetReferences[$this->currentId][$targetId] ?? $this->targetReferences[$this->currentId][$targetId] = true;
} else {
$this->sourceReferences[$targetId][$this->currentId] = false;
$this->targetReferences[$this->currentId][$targetId] = false;
}

return $value;
}

if (!$value instanceof Definition || !$value->hasErrors()) {
return parent::processValue($value, $isRoot);
}

// only show the first error so the user can focus on it
$errors = $value->getErrors();
$message = reset($errors);
$this->erroredDefinitions[$this->currentId] = $value;

throw new RuntimeException($message);
return parent::processValue($value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Reference;

class DefinitionErrorExceptionPassTest extends TestCase
{
Expand Down Expand Up @@ -49,4 +50,25 @@ public function testNoExceptionThrown()
$pass->process($container);
$this->assertSame($def, $container->getDefinition('foo_service_id')->getArgument(0));
}

public function testSkipNestedErrors()
{
$container = new ContainerBuilder();

$container->register('nested_error', 'stdClass')
->addError('Things went wrong!');

$container->register('bar', 'stdClass')
->addArgument(new Reference('nested_error'));

$container->register('foo', 'stdClass')
->addArgument(new Reference('bar', ContainerBuilder::RUNTIME_EXCEPTION_ON_INVALID_REFERENCE));

$pass = new DefinitionErrorExceptionPass();
$pass->process($container);

$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Things went wrong!');
$container->get('foo');
}
}