2015-08-18 00:00:26 +00:00
< ? 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 ;
/**
* Compiler Pass Configuration .
*
* This class has a default configuration embedded .
*
* @ author Johannes M . Schmitt < schmittjoh @ gmail . com >
*/
class PassConfig
{
const TYPE_AFTER_REMOVING = 'afterRemoving' ;
const TYPE_BEFORE_OPTIMIZATION = 'beforeOptimization' ;
const TYPE_BEFORE_REMOVING = 'beforeRemoving' ;
const TYPE_OPTIMIZE = 'optimization' ;
const TYPE_REMOVE = 'removing' ;
private $mergePass ;
private $afterRemovingPasses = array ();
private $beforeOptimizationPasses = array ();
private $beforeRemovingPasses = array ();
private $optimizationPasses ;
private $removingPasses ;
public function __construct ()
{
$this -> mergePass = new MergeExtensionConfigurationPass ();
2018-11-23 12:29:20 +00:00
$this -> beforeOptimizationPasses = array (
100 => array (
$resolveClassPass = new ResolveClassPass (),
new ResolveInstanceofConditionalsPass (),
new RegisterEnvVarProcessorsPass (),
),
- 1000 => array ( new ExtensionCompilerPass ()),
);
$this -> optimizationPasses = array ( array (
new ResolveChildDefinitionsPass (),
new ServiceLocatorTagPass (),
2015-08-18 00:00:26 +00:00
new DecoratorServicePass (),
2018-11-23 12:29:20 +00:00
new ResolveParameterPlaceHoldersPass ( false ),
new ResolveFactoryClassPass (),
new FactoryReturnTypePass ( $resolveClassPass ),
2015-08-18 00:00:26 +00:00
new CheckDefinitionValidityPass (),
2018-11-23 12:29:20 +00:00
new RegisterServiceSubscribersPass (),
new ResolveNamedArgumentsPass (),
new AutowireRequiredMethodsPass (),
new ResolveBindingsPass (),
new AutowirePass ( false ),
new ResolveTaggedIteratorArgumentPass (),
new ResolveServiceSubscribersPass (),
2015-08-18 00:00:26 +00:00
new ResolveReferencesToAliasesPass (),
new ResolveInvalidReferencesPass (),
new AnalyzeServiceReferencesPass ( true ),
new CheckCircularReferencesPass (),
new CheckReferenceValidityPass (),
2018-11-23 12:29:20 +00:00
new CheckArgumentsValidityPass ( false ),
));
$this -> beforeRemovingPasses = array (
- 100 => array (
new ResolvePrivatesPass (),
),
2015-08-18 00:00:26 +00:00
);
2018-11-23 12:29:20 +00:00
$this -> removingPasses = array ( array (
2015-08-18 00:00:26 +00:00
new RemovePrivateAliasesPass (),
new ReplaceAliasByActualDefinitionPass (),
2017-02-03 00:28:38 +00:00
new RemoveAbstractDefinitionsPass (),
2015-08-18 00:00:26 +00:00
new RepeatedPass ( array (
new AnalyzeServiceReferencesPass (),
new InlineServiceDefinitionsPass (),
new AnalyzeServiceReferencesPass (),
new RemoveUnusedDefinitionsPass (),
)),
2018-11-23 12:29:20 +00:00
new DefinitionErrorExceptionPass (),
2015-08-18 00:00:26 +00:00
new CheckExceptionOnInvalidReferenceBehaviorPass (),
2018-11-23 12:29:20 +00:00
new ResolveHotPathPass (),
));
2015-08-18 00:00:26 +00:00
}
/**
* Returns all passes in order to be processed .
*
2018-11-23 12:29:20 +00:00
* @ return CompilerPassInterface []
2015-08-18 00:00:26 +00:00
*/
public function getPasses ()
{
return array_merge (
array ( $this -> mergePass ),
2018-11-23 12:29:20 +00:00
$this -> getBeforeOptimizationPasses (),
$this -> getOptimizationPasses (),
$this -> getBeforeRemovingPasses (),
$this -> getRemovingPasses (),
$this -> getAfterRemovingPasses ()
2015-08-18 00:00:26 +00:00
);
}
/**
* Adds a pass .
*
2018-11-23 12:29:20 +00:00
* @ param CompilerPassInterface $pass A Compiler pass
* @ param string $type The pass type
* @ param int $priority Used to sort the passes
2015-08-18 00:00:26 +00:00
*
* @ throws InvalidArgumentException when a pass type doesn ' t exist
*/
2018-11-23 12:29:20 +00:00
public function addPass ( CompilerPassInterface $pass , $type = self :: TYPE_BEFORE_OPTIMIZATION /*, int $priority = 0*/ )
2015-08-18 00:00:26 +00:00
{
2018-11-23 12:29:20 +00:00
if ( \func_num_args () >= 3 ) {
$priority = func_get_arg ( 2 );
} else {
if ( __CLASS__ !== \get_class ( $this )) {
$r = new \ReflectionMethod ( $this , __FUNCTION__ );
if ( __CLASS__ !== $r -> getDeclaringClass () -> getName ()) {
@ trigger_error ( sprintf ( 'Method %s() will have a third `int $priority = 0` argument in version 4.0. Not defining it is deprecated since Symfony 3.2.' , __METHOD__ ), E_USER_DEPRECATED );
}
}
$priority = 0 ;
}
2015-08-18 00:00:26 +00:00
$property = $type . 'Passes' ;
if ( ! isset ( $this -> $property )) {
throw new InvalidArgumentException ( sprintf ( 'Invalid type "%s".' , $type ));
}
2018-11-23 12:29:20 +00:00
$passes = & $this -> $property ;
if ( ! isset ( $passes [ $priority ])) {
$passes [ $priority ] = array ();
}
$passes [ $priority ][] = $pass ;
2015-08-18 00:00:26 +00:00
}
/**
* Gets all passes for the AfterRemoving pass .
*
2018-11-23 12:29:20 +00:00
* @ return CompilerPassInterface []
2015-08-18 00:00:26 +00:00
*/
public function getAfterRemovingPasses ()
{
2018-11-23 12:29:20 +00:00
return $this -> sortPasses ( $this -> afterRemovingPasses );
2015-08-18 00:00:26 +00:00
}
/**
* Gets all passes for the BeforeOptimization pass .
*
2018-11-23 12:29:20 +00:00
* @ return CompilerPassInterface []
2015-08-18 00:00:26 +00:00
*/
public function getBeforeOptimizationPasses ()
{
2018-11-23 12:29:20 +00:00
return $this -> sortPasses ( $this -> beforeOptimizationPasses );
2015-08-18 00:00:26 +00:00
}
/**
* Gets all passes for the BeforeRemoving pass .
*
2018-11-23 12:29:20 +00:00
* @ return CompilerPassInterface []
2015-08-18 00:00:26 +00:00
*/
public function getBeforeRemovingPasses ()
{
2018-11-23 12:29:20 +00:00
return $this -> sortPasses ( $this -> beforeRemovingPasses );
2015-08-18 00:00:26 +00:00
}
/**
* Gets all passes for the Optimization pass .
*
2018-11-23 12:29:20 +00:00
* @ return CompilerPassInterface []
2015-08-18 00:00:26 +00:00
*/
public function getOptimizationPasses ()
{
2018-11-23 12:29:20 +00:00
return $this -> sortPasses ( $this -> optimizationPasses );
2015-08-18 00:00:26 +00:00
}
/**
* Gets all passes for the Removing pass .
*
2018-11-23 12:29:20 +00:00
* @ return CompilerPassInterface []
2015-08-18 00:00:26 +00:00
*/
public function getRemovingPasses ()
{
2018-11-23 12:29:20 +00:00
return $this -> sortPasses ( $this -> removingPasses );
2015-08-18 00:00:26 +00:00
}
/**
2017-02-03 00:28:38 +00:00
* Gets the Merge pass .
2015-08-18 00:00:26 +00:00
*
2018-11-23 12:29:20 +00:00
* @ return CompilerPassInterface
2015-08-18 00:00:26 +00:00
*/
public function getMergePass ()
{
return $this -> mergePass ;
}
public function setMergePass ( CompilerPassInterface $pass )
{
$this -> mergePass = $pass ;
}
/**
* Sets the AfterRemoving passes .
*
2018-11-23 12:29:20 +00:00
* @ param CompilerPassInterface [] $passes
2015-08-18 00:00:26 +00:00
*/
public function setAfterRemovingPasses ( array $passes )
{
2018-11-23 12:29:20 +00:00
$this -> afterRemovingPasses = array ( $passes );
2015-08-18 00:00:26 +00:00
}
/**
* Sets the BeforeOptimization passes .
*
2018-11-23 12:29:20 +00:00
* @ param CompilerPassInterface [] $passes
2015-08-18 00:00:26 +00:00
*/
public function setBeforeOptimizationPasses ( array $passes )
{
2018-11-23 12:29:20 +00:00
$this -> beforeOptimizationPasses = array ( $passes );
2015-08-18 00:00:26 +00:00
}
/**
* Sets the BeforeRemoving passes .
*
2018-11-23 12:29:20 +00:00
* @ param CompilerPassInterface [] $passes
2015-08-18 00:00:26 +00:00
*/
public function setBeforeRemovingPasses ( array $passes )
{
2018-11-23 12:29:20 +00:00
$this -> beforeRemovingPasses = array ( $passes );
2015-08-18 00:00:26 +00:00
}
/**
* Sets the Optimization passes .
*
2018-11-23 12:29:20 +00:00
* @ param CompilerPassInterface [] $passes
2015-08-18 00:00:26 +00:00
*/
public function setOptimizationPasses ( array $passes )
{
2018-11-23 12:29:20 +00:00
$this -> optimizationPasses = array ( $passes );
2015-08-18 00:00:26 +00:00
}
/**
* Sets the Removing passes .
*
2018-11-23 12:29:20 +00:00
* @ param CompilerPassInterface [] $passes
2015-08-18 00:00:26 +00:00
*/
public function setRemovingPasses ( array $passes )
{
2018-11-23 12:29:20 +00:00
$this -> removingPasses = array ( $passes );
}
/**
* Sort passes by priority .
*
* @ param array $passes CompilerPassInterface instances with their priority as key
*
* @ return CompilerPassInterface []
*/
private function sortPasses ( array $passes )
{
if ( 0 === \count ( $passes )) {
return array ();
}
krsort ( $passes );
// Flatten the array
return \call_user_func_array ( 'array_merge' , $passes );
2015-08-18 00:00:26 +00:00
}
}