2011-01-09 09:53:46 +00:00
|
|
|
<?php
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-01-15 13:29:43 +00:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2011-01-09 09:53:46 +00:00
|
|
|
namespace Symfony\Component\DependencyInjection\Compiler;
|
|
|
|
|
2011-12-04 23:51:22 +00:00
|
|
|
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
|
|
|
|
2011-01-09 09:53:46 +00:00
|
|
|
/**
|
|
|
|
* 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>
|
|
|
|
*/
|
|
|
|
class ServiceReferenceGraph
|
|
|
|
{
|
2012-11-01 15:08:59 +00:00
|
|
|
/**
|
|
|
|
* @var ServiceReferenceGraphNode[]
|
|
|
|
*/
|
2013-11-10 17:06:47 +00:00
|
|
|
private $nodes = array();
|
2011-01-09 09:53:46 +00:00
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Checks if the graph has a specific node.
|
|
|
|
*
|
|
|
|
* @param string $id Id to check
|
2012-09-07 23:56:45 +01:00
|
|
|
*
|
2014-04-16 11:30:19 +01:00
|
|
|
* @return bool
|
2011-02-13 18:06:41 +00:00
|
|
|
*/
|
2011-01-09 09:53:46 +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
|
|
|
*
|
2011-02-13 18:06:41 +00:00
|
|
|
* @return ServiceReferenceGraphNode The node matching the supplied identifier
|
2011-12-13 07:50:54 +00:00
|
|
|
*
|
2011-12-04 23:57:59 +00:00
|
|
|
* @throws InvalidArgumentException if no node matches the supplied identifier
|
2011-02-13 18:06:41 +00:00
|
|
|
*/
|
2011-01-09 09:53:46 +00:00
|
|
|
public function getNode($id)
|
|
|
|
{
|
|
|
|
if (!isset($this->nodes[$id])) {
|
2011-12-04 23:51:22 +00:00
|
|
|
throw new InvalidArgumentException(sprintf('There is no node with id "%s".', $id));
|
2011-01-09 09:53:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->nodes[$id];
|
|
|
|
}
|
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Returns all nodes.
|
|
|
|
*
|
2012-11-01 15:08:59 +00:00
|
|
|
* @return ServiceReferenceGraphNode[] An array of all ServiceReferenceGraphNode objects
|
2011-02-13 18:06:41 +00:00
|
|
|
*/
|
2011-01-09 09:53:46 +00:00
|
|
|
public function getNodes()
|
|
|
|
{
|
|
|
|
return $this->nodes;
|
|
|
|
}
|
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Clears all nodes.
|
|
|
|
*/
|
2011-01-09 09:53:46 +00:00
|
|
|
public function 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
|
|
|
|
* @param string $sourceValue
|
|
|
|
* @param string $destId
|
|
|
|
* @param string $destValue
|
|
|
|
* @param string $reference
|
2011-02-13 18:06:41 +00:00
|
|
|
*/
|
2011-01-09 09:53:46 +00:00
|
|
|
public function connect($sourceId, $sourceValue, $destId, $destValue = null, $reference = null)
|
|
|
|
{
|
|
|
|
$sourceNode = $this->createNode($sourceId, $sourceValue);
|
|
|
|
$destNode = $this->createNode($destId, $destValue);
|
|
|
|
$edge = new ServiceReferenceGraphEdge($sourceNode, $destNode, $reference);
|
|
|
|
|
|
|
|
$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
|
|
|
|
* @param string $value
|
2011-12-13 07:50:54 +00:00
|
|
|
*
|
2011-02-13 18:06:41 +00:00
|
|
|
* @return ServiceReferenceGraphNode
|
|
|
|
*/
|
2011-03-11 13:50:46 +00:00
|
|
|
private function createNode($id, $value)
|
2011-01-09 09:53:46 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|