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 ;
use Symfony\Component\DependencyInjection\ContainerBuilder ;
2016-08-12 19:34:27 +01:00
use Symfony\Component\DependencyInjection\Exception\EnvParameterException ;
2011-01-09 09:53:46 +00:00
/**
* This class is used to remove circular dependencies between individual passes .
*
* @ author Johannes M . Schmitt < schmittjoh @ gmail . com >
*/
class Compiler
{
2011-03-11 13:50:46 +00:00
private $passConfig ;
2013-11-10 17:06:47 +00:00
private $log = array ();
2011-04-12 23:51:22 +01:00
private $loggingFormatter ;
2011-03-11 13:50:46 +00:00
private $serviceReferenceGraph ;
2011-01-09 09:53:46 +00:00
public function __construct ()
{
$this -> passConfig = new PassConfig ();
$this -> serviceReferenceGraph = new ServiceReferenceGraph ();
2011-04-12 23:51:22 +01:00
$this -> loggingFormatter = new LoggingFormatter ();
2011-01-09 09:53:46 +00:00
}
2011-02-13 18:06:41 +00:00
/**
* Returns the PassConfig .
*
* @ return PassConfig The PassConfig instance
*/
2011-01-09 09:53:46 +00:00
public function getPassConfig ()
{
return $this -> passConfig ;
}
2011-02-13 18:06:41 +00:00
/**
* Returns the ServiceReferenceGraph .
*
* @ return ServiceReferenceGraph The ServiceReferenceGraph instance
*/
2011-01-09 09:53:46 +00:00
public function getServiceReferenceGraph ()
{
return $this -> serviceReferenceGraph ;
}
2011-04-12 23:51:22 +01:00
/**
* Returns the logging formatter which can be used by compilation passes .
*
* @ return LoggingFormatter
*/
public function getLoggingFormatter ()
{
return $this -> loggingFormatter ;
}
2011-02-13 18:06:41 +00:00
/**
* Adds a pass to the PassConfig .
*
2016-03-05 12:39:12 +00:00
* @ param CompilerPassInterface $pass A compiler pass
* @ param string $type The type of the pass
* @ param int $priority Used to sort the passes
2011-02-13 18:06:41 +00:00
*/
2016-11-04 13:53:25 +00:00
public function addPass ( CompilerPassInterface $pass , $type = PassConfig :: TYPE_BEFORE_OPTIMIZATION /*, $priority = 0*/ )
2011-01-09 09:53:46 +00:00
{
2016-03-05 12:39:12 +00:00
if ( func_num_args () >= 3 ) {
$priority = func_get_arg ( 2 );
} else {
2016-11-04 13:53:25 +00:00
if ( __CLASS__ !== get_class ( $this )) {
$r = new \ReflectionMethod ( $this , __FUNCTION__ );
if ( __CLASS__ !== $r -> getDeclaringClass () -> getName ()) {
@ trigger_error ( sprintf ( 'Method %s() will have a third `$priority = 0` argument in version 4.0. Not defining it is deprecated since 3.2.' , get_class ( $this ), __FUNCTION__ ), E_USER_DEPRECATED );
}
}
2016-03-05 12:39:12 +00:00
$priority = 0 ;
}
$this -> passConfig -> addPass ( $pass , $type , $priority );
2011-01-09 09:53:46 +00:00
}
2011-02-13 18:06:41 +00:00
/**
* Adds a log message .
*
2011-04-12 23:51:22 +01:00
* @ param string $string The log message
2011-02-13 18:06:41 +00:00
*/
2011-01-09 09:53:46 +00:00
public function addLogMessage ( $string )
{
$this -> log [] = $string ;
}
2011-02-13 18:06:41 +00:00
/**
* Returns the log .
*
* @ return array Log array
*/
2011-01-09 09:53:46 +00:00
public function getLog ()
{
return $this -> log ;
}
2011-02-13 18:06:41 +00:00
/**
* Run the Compiler and process all Passes .
*
2011-04-12 23:51:22 +01:00
* @ param ContainerBuilder $container
2011-02-13 18:06:41 +00:00
*/
2011-01-09 09:53:46 +00:00
public function compile ( ContainerBuilder $container )
{
2016-08-12 19:34:27 +01:00
try {
foreach ( $this -> passConfig -> getPasses () as $pass ) {
$pass -> process ( $container );
}
} catch ( \Exception $e ) {
$usedEnvs = array ();
$prev = $e ;
do {
$msg = $prev -> getMessage ();
if ( $msg !== $resolvedMsg = $container -> resolveEnvPlaceholders ( $msg , null , $usedEnvs )) {
$r = new \ReflectionProperty ( $prev , 'message' );
$r -> setAccessible ( true );
$r -> setValue ( $prev , $resolvedMsg );
}
} while ( $prev = $prev -> getPrevious ());
if ( $usedEnvs ) {
$e = new EnvParameterException ( $usedEnvs , $e );
}
throw $e ;
2011-01-09 09:53:46 +00:00
}
}
2011-06-08 11:16:48 +01:00
}