Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[DepdencyInjection] Fix costly logic when checking errored definitions #50905

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
Jul 7, 2023
Merged
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 @@ -26,7 +26,6 @@
class DefinitionErrorExceptionPass extends AbstractRecursivePass
{
private $erroredDefinitions = [];
private $targetReferences = [];
private $sourceReferences = [];

/**
Expand All @@ -37,45 +36,10 @@ 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]);
}
$visitedIds = [];

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

Expand All @@ -86,7 +50,6 @@ public function process(ContainerBuilder $container)
}
} finally {
$this->erroredDefinitions = [];
$this->targetReferences = [];
$this->sourceReferences = [];
}
}
Expand All @@ -105,10 +68,8 @@ protected function processValue($value, bool $isRoot = false)
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;
Expand All @@ -122,4 +83,29 @@ protected function processValue($value, bool $isRoot = false)

return parent::processValue($value);
}

private function isErrorForRuntime(string $id, array &$visitedIds): bool
{
if (!isset($this->sourceReferences[$id])) {
return false;
}

if (isset($visitedIds[$id])) {
return $visitedIds[$id];
}

$visitedIds[$id] = true;

foreach ($this->sourceReferences[$id] as $sourceId => $isRuntime) {
if ($visitedIds[$sourceId] ?? $visitedIds[$sourceId] = $this->isErrorForRuntime($sourceId, $visitedIds)) {
continue;
}

if (!$isRuntime) {
return false;
}
}

return true;
}
}