<?php
namespace Symfony\Component\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Reference;
class ServiceReferenceGraph
{
private array $nodes = [];
public function hasNode(string $id): bool
{
return isset($this->nodes[$id]);
}
public function getNode(string $id): ServiceReferenceGraphNode
{
if (!isset($this->nodes[$id])) {
throw new InvalidArgumentException(\sprintf('There is no node with id "%s".', $id));
}
return $this->nodes[$id];
}
public function getNodes(): array
{
return $this->nodes;
}
public function clear(): void
{
foreach ($this->nodes as $node) {
$node->clear();
}
$this->nodes = [];
}
public function connect(?string $sourceId, mixed $sourceValue, ?string $destId, mixed $destValue = null, ?Reference $reference = null, bool $lazy = false, bool $weak = false, bool $byConstructor = false, bool $byMultiUseArgument = false): void
{
if (null === $sourceId || null === $destId) {
return;
}
$sourceNode = $this->createNode($sourceId, $sourceValue);
$destNode = $this->createNode($destId, $destValue);
$edge = new ServiceReferenceGraphEdge($sourceNode, $destNode, $reference, $lazy, $weak, $byConstructor, $byMultiUseArgument);
$sourceNode->addOutEdge($edge);
$destNode->addInEdge($edge);
}
private function createNode(string $id, mixed $value): ServiceReferenceGraphNode
{
if (isset($this->nodes[$id]) && $this->nodes[$id]->getValue() === $value) {
return $this->nodes[$id];
}
return $this->nodes[$id] = new ServiceReferenceGraphNode($id, $value);
}
}