This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/DependencyInjection/Compiler/ServiceReferenceGraph.php

131 lines
3.3 KiB
PHP
Raw Normal View History

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
/**
* This is a directed graph of your services.
*
* This information can be used by your compiler passes instead of collecting
* it themselves which improves performance quite a lot.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*
* @final since version 3.4
*/
class ServiceReferenceGraph
{
/**
* @var ServiceReferenceGraphNode[]
*/
private $nodes = array();
2011-02-13 18:06:41 +00:00
/**
* Checks if the graph has a specific node.
*
* @param string $id Id to check
*
2014-04-16 11:30:19 +01:00
* @return bool
2011-02-13 18:06:41 +00:00
*/
public function hasNode($id)
{
return isset($this->nodes[$id]);
}
2011-02-13 18:06:41 +00:00
/**
* Gets a node by identifier.
*
* @param string $id The id to retrieve
2011-12-13 07:50:54 +00:00
*
* @return ServiceReferenceGraphNode
2011-12-13 07:50:54 +00:00
*
* @throws InvalidArgumentException if no node matches the supplied identifier
2011-02-13 18:06:41 +00:00
*/
public function getNode($id)
{
if (!isset($this->nodes[$id])) {
throw new InvalidArgumentException(sprintf('There is no node with id "%s".', $id));
}
return $this->nodes[$id];
}
2011-02-13 18:06:41 +00:00
/**
* Returns all nodes.
*
* @return ServiceReferenceGraphNode[]
2011-02-13 18:06:41 +00:00
*/
public function getNodes()
{
return $this->nodes;
}
2011-02-13 18:06:41 +00:00
/**
* Clears all nodes.
*/
public function clear()
{
2017-11-27 20:00:44 +00:00
foreach ($this->nodes as $node) {
$node->clear();
}
$this->nodes = array();
}
2011-02-13 18:06:41 +00:00
/**
* Connects 2 nodes together in the Graph.
*
2011-04-15 20:12:02 +01:00
* @param string $sourceId
2017-10-22 17:42:21 +01:00
* @param mixed $sourceValue
2011-04-15 20:12:02 +01:00
* @param string $destId
2017-10-22 17:42:21 +01:00
* @param mixed $destValue
2011-04-15 20:12:02 +01:00
* @param string $reference
* @param bool $lazy
* @param bool $weak
* @param bool $byConstructor
2011-02-13 18:06:41 +00:00
*/
public function connect($sourceId, $sourceValue, $destId, $destValue = null, $reference = null/*, bool $lazy = false, bool $weak = false, bool $byConstructor = false*/)
{
$lazy = \func_num_args() >= 6 ? func_get_arg(5) : false;
$weak = \func_num_args() >= 7 ? func_get_arg(6) : false;
$byConstructor = \func_num_args() >= 8 ? func_get_arg(7) : false;
2017-10-22 17:42:21 +01:00
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);
$sourceNode->addOutEdge($edge);
$destNode->addInEdge($edge);
}
2011-02-13 18:06:41 +00:00
/**
* Creates a graph node.
*
2011-04-15 20:12:02 +01:00
* @param string $id
2017-10-22 17:42:21 +01:00
* @param mixed $value
2011-12-13 07:50:54 +00:00
*
2011-02-13 18:06:41 +00:00
* @return ServiceReferenceGraphNode
*/
private function createNode($id, $value)
{
if (isset($this->nodes[$id]) && $this->nodes[$id]->getValue() === $value) {
return $this->nodes[$id];
}
return $this->nodes[$id] = new ServiceReferenceGraphNode($id, $value);
}
2011-06-08 11:16:48 +01:00
}