vendor/symfony/src/Symfony/Component/DependencyInjection/Compiler/ServiceReferenceGraph.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * This file is part of the Symfony package.
       
     5  *
       
     6  * (c) Fabien Potencier <fabien@symfony.com>
       
     7  *
       
     8  * For the full copyright and license information, please view the LICENSE
       
     9  * file that was distributed with this source code.
       
    10  */
       
    11 
       
    12 namespace Symfony\Component\DependencyInjection\Compiler;
       
    13 
       
    14 /**
       
    15  * This is a directed graph of your services.
       
    16  *
       
    17  * This information can be used by your compiler passes instead of collecting
       
    18  * it themselves which improves performance quite a lot.
       
    19  *
       
    20  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
       
    21  */
       
    22 class ServiceReferenceGraph
       
    23 {
       
    24     private $nodes;
       
    25 
       
    26     /**
       
    27      * Constructor.
       
    28      */
       
    29     public function __construct()
       
    30     {
       
    31         $this->nodes = array();
       
    32     }
       
    33 
       
    34     /**
       
    35      * Checks if the graph has a specific node.
       
    36      *
       
    37      * @param string $id Id to check
       
    38      */
       
    39     public function hasNode($id)
       
    40     {
       
    41         return isset($this->nodes[$id]);
       
    42     }
       
    43 
       
    44     /**
       
    45      * Gets a node by identifier.
       
    46      *
       
    47      * @param string $id The id to retrieve
       
    48      * @return ServiceReferenceGraphNode The node matching the supplied identifier
       
    49      * @throws \InvalidArgumentException
       
    50      */
       
    51     public function getNode($id)
       
    52     {
       
    53         if (!isset($this->nodes[$id])) {
       
    54             throw new \InvalidArgumentException(sprintf('There is no node with id "%s".', $id));
       
    55         }
       
    56 
       
    57         return $this->nodes[$id];
       
    58     }
       
    59 
       
    60     /**
       
    61      * Returns all nodes.
       
    62      *
       
    63      * @return array An array of all ServiceReferenceGraphNode objects
       
    64      */
       
    65     public function getNodes()
       
    66     {
       
    67         return $this->nodes;
       
    68     }
       
    69 
       
    70     /**
       
    71      * Clears all nodes.
       
    72      */
       
    73     public function clear()
       
    74     {
       
    75         $this->nodes = array();
       
    76     }
       
    77 
       
    78     /**
       
    79      * Connects 2 nodes together in the Graph.
       
    80      *
       
    81      * @param string $sourceId
       
    82      * @param string $sourceValue
       
    83      * @param string $destId
       
    84      * @param string $destValue
       
    85      * @param string $reference
       
    86      */
       
    87     public function connect($sourceId, $sourceValue, $destId, $destValue = null, $reference = null)
       
    88     {
       
    89         $sourceNode = $this->createNode($sourceId, $sourceValue);
       
    90         $destNode = $this->createNode($destId, $destValue);
       
    91         $edge = new ServiceReferenceGraphEdge($sourceNode, $destNode, $reference);
       
    92 
       
    93         $sourceNode->addOutEdge($edge);
       
    94         $destNode->addInEdge($edge);
       
    95     }
       
    96 
       
    97     /**
       
    98      * Creates a graph node.
       
    99      *
       
   100      * @param string $id
       
   101      * @param string $value
       
   102      * @return ServiceReferenceGraphNode
       
   103      */
       
   104     private function createNode($id, $value)
       
   105     {
       
   106         if (isset($this->nodes[$id]) && $this->nodes[$id]->getValue() === $value) {
       
   107             return $this->nodes[$id];
       
   108         }
       
   109 
       
   110         return $this->nodes[$id] = new ServiceReferenceGraphNode($id, $value);
       
   111     }
       
   112 }