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
*
2016-12-26 07:50:27 +00:00
* @ return ServiceReferenceGraphNode
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 .
*
2016-12-26 07:50:27 +00:00
* @ return ServiceReferenceGraphNode []
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
2016-12-13 17:37:51 +00:00
* @ param bool $lazy
2011-02-13 18:06:41 +00:00
*/
2017-05-18 18:20:35 +01:00
public function connect ( $sourceId , $sourceValue , $destId , $destValue = null , $reference = null /*, bool $lazy = false*/ )
2011-01-09 09:53:46 +00:00
{
2016-12-13 17:37:51 +00:00
if ( func_num_args () >= 6 ) {
$lazy = func_get_arg ( 5 );
} else {
if ( __CLASS__ !== get_class ( $this )) {
$r = new \ReflectionMethod ( $this , __FUNCTION__ );
if ( __CLASS__ !== $r -> getDeclaringClass () -> getName ()) {
2017-05-18 18:20:35 +01:00
@ trigger_error ( sprintf ( 'Method %s() will have a 6th `bool $lazy = false` argument in version 4.0. Not defining it is deprecated since 3.3.' , __METHOD__ ), E_USER_DEPRECATED );
2016-12-13 17:37:51 +00:00
}
}
$lazy = false ;
}
2011-01-09 09:53:46 +00:00
$sourceNode = $this -> createNode ( $sourceId , $sourceValue );
$destNode = $this -> createNode ( $destId , $destValue );
2016-12-13 17:37:51 +00:00
$edge = new ServiceReferenceGraphEdge ( $sourceNode , $destNode , $reference , $lazy );
2011-01-09 09:53:46 +00:00
$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
}