2010-12-29 19:12:24 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2011-01-15 13:29:43 +00:00
|
|
|
* This file is part of the Symfony package.
|
2010-12-29 19:12:24 +00:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-12-29 19:12:24 +00:00
|
|
|
*
|
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.
|
2010-12-29 19:12:24 +00:00
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\DependencyInjection\Compiler;
|
|
|
|
|
2011-12-04 23:51:22 +00:00
|
|
|
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
|
|
|
|
2010-12-29 19:12:24 +00:00
|
|
|
/**
|
2014-12-21 17:00:50 +00:00
|
|
|
* Compiler Pass Configuration.
|
2010-12-29 19:12:24 +00:00
|
|
|
*
|
2011-01-09 09:53:46 +00:00
|
|
|
* This class has a default configuration embedded.
|
|
|
|
*
|
2010-12-29 19:12:24 +00:00
|
|
|
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
|
|
|
*/
|
|
|
|
class PassConfig
|
|
|
|
{
|
2011-01-09 09:53:46 +00:00
|
|
|
const TYPE_AFTER_REMOVING = 'afterRemoving';
|
|
|
|
const TYPE_BEFORE_OPTIMIZATION = 'beforeOptimization';
|
|
|
|
const TYPE_BEFORE_REMOVING = 'beforeRemoving';
|
2010-12-29 19:12:24 +00:00
|
|
|
const TYPE_OPTIMIZE = 'optimization';
|
|
|
|
const TYPE_REMOVE = 'removing';
|
|
|
|
|
2011-03-11 13:50:46 +00:00
|
|
|
private $mergePass;
|
2013-11-10 17:06:47 +00:00
|
|
|
private $afterRemovingPasses = array();
|
|
|
|
private $beforeOptimizationPasses = array();
|
|
|
|
private $beforeRemovingPasses = array();
|
2011-03-11 13:50:46 +00:00
|
|
|
private $optimizationPasses;
|
|
|
|
private $removingPasses;
|
2010-12-29 19:12:24 +00:00
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->mergePass = new MergeExtensionConfigurationPass();
|
|
|
|
|
2016-03-05 12:39:12 +00:00
|
|
|
$this->optimizationPasses = array(array(
|
2014-11-30 20:23:32 +00:00
|
|
|
new ExtensionCompilerPass(),
|
2011-01-26 23:14:31 +00:00
|
|
|
new ResolveDefinitionTemplatesPass(),
|
2013-09-12 11:15:48 +01:00
|
|
|
new DecoratorServicePass(),
|
2010-12-29 19:12:24 +00:00
|
|
|
new ResolveParameterPlaceHoldersPass(),
|
2011-01-24 18:44:27 +00:00
|
|
|
new CheckDefinitionValidityPass(),
|
2011-01-05 11:13:27 +00:00
|
|
|
new ResolveReferencesToAliasesPass(),
|
|
|
|
new ResolveInvalidReferencesPass(),
|
2015-08-24 02:36:41 +01:00
|
|
|
new AutowirePass(),
|
2011-01-17 22:28:59 +00:00
|
|
|
new AnalyzeServiceReferencesPass(true),
|
|
|
|
new CheckCircularReferencesPass(),
|
2011-01-26 23:14:31 +00:00
|
|
|
new CheckReferenceValidityPass(),
|
2016-03-05 12:39:12 +00:00
|
|
|
));
|
2010-12-29 19:12:24 +00:00
|
|
|
|
2016-03-05 12:39:12 +00:00
|
|
|
$this->removingPasses = array(array(
|
2011-01-07 14:44:29 +00:00
|
|
|
new RemovePrivateAliasesPass(),
|
|
|
|
new ReplaceAliasByActualDefinitionPass(),
|
2016-04-05 14:46:37 +01:00
|
|
|
new RemoveAbstractDefinitionsPass(),
|
2011-01-08 08:39:53 +00:00
|
|
|
new RepeatedPass(array(
|
2011-01-09 09:53:46 +00:00
|
|
|
new AnalyzeServiceReferencesPass(),
|
2011-01-08 08:39:53 +00:00
|
|
|
new InlineServiceDefinitionsPass(),
|
2011-01-09 09:53:46 +00:00
|
|
|
new AnalyzeServiceReferencesPass(),
|
2011-01-08 08:39:53 +00:00
|
|
|
new RemoveUnusedDefinitionsPass(),
|
|
|
|
)),
|
2011-04-14 23:11:50 +01:00
|
|
|
new CheckExceptionOnInvalidReferenceBehaviorPass(),
|
2016-03-05 12:39:12 +00:00
|
|
|
));
|
2010-12-29 19:12:24 +00:00
|
|
|
}
|
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Returns all passes in order to be processed.
|
|
|
|
*
|
2016-03-05 12:39:12 +00:00
|
|
|
* @return CompilerPassInterface[]
|
2011-02-13 18:06:41 +00:00
|
|
|
*/
|
2010-12-29 19:12:24 +00:00
|
|
|
public function getPasses()
|
|
|
|
{
|
|
|
|
return array_merge(
|
|
|
|
array($this->mergePass),
|
2016-03-05 12:39:12 +00:00
|
|
|
$this->getBeforeOptimizationPasses(),
|
|
|
|
$this->getOptimizationPasses(),
|
|
|
|
$this->getBeforeRemovingPasses(),
|
|
|
|
$this->getRemovingPasses(),
|
|
|
|
$this->getAfterRemovingPasses()
|
2010-12-29 19:12:24 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Adds a pass.
|
|
|
|
*
|
2016-03-05 12:39:12 +00:00
|
|
|
* @param CompilerPassInterface $pass A Compiler pass
|
|
|
|
* @param string $type The pass type
|
|
|
|
* @param int $priority Used to sort the passes
|
2011-12-13 07:50:54 +00:00
|
|
|
*
|
2011-12-04 23:51:22 +00:00
|
|
|
* @throws InvalidArgumentException when a pass type doesn't exist
|
2011-02-13 18:06:41 +00:00
|
|
|
*/
|
2016-03-05 12:39:12 +00:00
|
|
|
public function addPass(CompilerPassInterface $pass, $type = self::TYPE_BEFORE_OPTIMIZATION/*, $priority = 0*/)
|
2010-12-29 19:12:24 +00:00
|
|
|
{
|
2016-03-05 12:39:12 +00:00
|
|
|
// For BC
|
|
|
|
if (func_num_args() >= 3) {
|
|
|
|
$priority = func_get_arg(2);
|
|
|
|
} else {
|
|
|
|
$priority = 0;
|
|
|
|
}
|
|
|
|
|
2010-12-29 19:12:24 +00:00
|
|
|
$property = $type.'Passes';
|
|
|
|
if (!isset($this->$property)) {
|
2011-12-04 23:51:22 +00:00
|
|
|
throw new InvalidArgumentException(sprintf('Invalid type "%s".', $type));
|
2010-12-29 19:12:24 +00:00
|
|
|
}
|
|
|
|
|
2016-03-05 12:39:12 +00:00
|
|
|
$passes = &$this->$property;
|
|
|
|
|
|
|
|
if (!isset($passes[$priority])) {
|
|
|
|
$passes[$priority] = array();
|
|
|
|
}
|
|
|
|
$passes[$priority][] = $pass;
|
2010-12-29 19:12:24 +00:00
|
|
|
}
|
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Gets all passes for the AfterRemoving pass.
|
|
|
|
*
|
2016-03-05 12:39:12 +00:00
|
|
|
* @return CompilerPassInterface[]
|
2011-02-13 18:06:41 +00:00
|
|
|
*/
|
2011-01-26 23:14:31 +00:00
|
|
|
public function getAfterRemovingPasses()
|
|
|
|
{
|
2016-03-05 12:39:12 +00:00
|
|
|
return $this->sortPasses($this->afterRemovingPasses);
|
2011-01-26 23:14:31 +00:00
|
|
|
}
|
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Gets all passes for the BeforeOptimization pass.
|
|
|
|
*
|
2016-03-05 12:39:12 +00:00
|
|
|
* @return CompilerPassInterface[]
|
2011-02-13 18:06:41 +00:00
|
|
|
*/
|
2011-01-09 09:53:46 +00:00
|
|
|
public function getBeforeOptimizationPasses()
|
|
|
|
{
|
2016-03-05 12:39:12 +00:00
|
|
|
return $this->sortPasses($this->beforeOptimizationPasses);
|
2011-01-09 09:53:46 +00:00
|
|
|
}
|
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Gets all passes for the BeforeRemoving pass.
|
|
|
|
*
|
2016-03-05 12:39:12 +00:00
|
|
|
* @return CompilerPassInterface[]
|
2011-02-13 18:06:41 +00:00
|
|
|
*/
|
2011-01-09 09:53:46 +00:00
|
|
|
public function getBeforeRemovingPasses()
|
|
|
|
{
|
2016-03-05 12:39:12 +00:00
|
|
|
return $this->sortPasses($this->beforeRemovingPasses);
|
2011-01-09 09:53:46 +00:00
|
|
|
}
|
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Gets all passes for the Optimization pass.
|
|
|
|
*
|
2016-03-05 12:39:12 +00:00
|
|
|
* @return CompilerPassInterface[]
|
2011-02-13 18:06:41 +00:00
|
|
|
*/
|
2010-12-29 19:12:24 +00:00
|
|
|
public function getOptimizationPasses()
|
|
|
|
{
|
2016-03-05 12:39:12 +00:00
|
|
|
return $this->sortPasses($this->optimizationPasses);
|
2010-12-29 19:12:24 +00:00
|
|
|
}
|
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Gets all passes for the Removing pass.
|
|
|
|
*
|
2016-03-05 12:39:12 +00:00
|
|
|
* @return CompilerPassInterface[]
|
2011-02-13 18:06:41 +00:00
|
|
|
*/
|
2010-12-29 19:12:24 +00:00
|
|
|
public function getRemovingPasses()
|
|
|
|
{
|
2016-03-05 12:39:12 +00:00
|
|
|
return $this->sortPasses($this->removingPasses);
|
2010-12-29 19:12:24 +00:00
|
|
|
}
|
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Gets all passes for the Merge pass.
|
|
|
|
*
|
2016-03-05 12:39:12 +00:00
|
|
|
* @return CompilerPassInterface
|
2011-02-13 18:06:41 +00:00
|
|
|
*/
|
2010-12-29 19:12:24 +00:00
|
|
|
public function getMergePass()
|
|
|
|
{
|
|
|
|
return $this->mergePass;
|
|
|
|
}
|
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Sets the Merge Pass.
|
|
|
|
*
|
|
|
|
* @param CompilerPassInterface $pass The merge pass
|
|
|
|
*/
|
2010-12-29 19:12:24 +00:00
|
|
|
public function setMergePass(CompilerPassInterface $pass)
|
|
|
|
{
|
|
|
|
$this->mergePass = $pass;
|
|
|
|
}
|
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Sets the AfterRemoving passes.
|
|
|
|
*
|
2016-03-05 12:39:12 +00:00
|
|
|
* @param CompilerPassInterface[] $passes
|
2011-02-13 18:06:41 +00:00
|
|
|
*/
|
2011-01-26 23:14:31 +00:00
|
|
|
public function setAfterRemovingPasses(array $passes)
|
|
|
|
{
|
2016-03-05 12:39:12 +00:00
|
|
|
$this->afterRemovingPasses = array($passes);
|
2011-01-26 23:14:31 +00:00
|
|
|
}
|
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Sets the BeforeOptimization passes.
|
|
|
|
*
|
2016-03-05 12:39:12 +00:00
|
|
|
* @param CompilerPassInterface[] $passes
|
2011-02-13 18:06:41 +00:00
|
|
|
*/
|
2011-01-09 09:53:46 +00:00
|
|
|
public function setBeforeOptimizationPasses(array $passes)
|
|
|
|
{
|
2016-03-05 12:39:12 +00:00
|
|
|
$this->beforeOptimizationPasses = array($passes);
|
2011-01-09 09:53:46 +00:00
|
|
|
}
|
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Sets the BeforeRemoving passes.
|
|
|
|
*
|
2016-03-05 12:39:12 +00:00
|
|
|
* @param CompilerPassInterface[] $passes
|
2011-02-13 18:06:41 +00:00
|
|
|
*/
|
2011-01-09 09:53:46 +00:00
|
|
|
public function setBeforeRemovingPasses(array $passes)
|
|
|
|
{
|
2016-03-05 12:39:12 +00:00
|
|
|
$this->beforeRemovingPasses = array($passes);
|
2011-01-09 09:53:46 +00:00
|
|
|
}
|
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Sets the Optimization passes.
|
|
|
|
*
|
2016-03-05 12:39:12 +00:00
|
|
|
* @param CompilerPassInterface[] $passes
|
2011-02-13 18:06:41 +00:00
|
|
|
*/
|
2010-12-29 19:12:24 +00:00
|
|
|
public function setOptimizationPasses(array $passes)
|
|
|
|
{
|
2016-03-05 12:39:12 +00:00
|
|
|
$this->optimizationPasses = array($passes);
|
2010-12-29 19:12:24 +00:00
|
|
|
}
|
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Sets the Removing passes.
|
|
|
|
*
|
2016-03-05 12:39:12 +00:00
|
|
|
* @param CompilerPassInterface[] $passes
|
2011-02-13 18:06:41 +00:00
|
|
|
*/
|
2010-12-29 19:12:24 +00:00
|
|
|
public function setRemovingPasses(array $passes)
|
|
|
|
{
|
2016-03-05 12:39:12 +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);
|
2010-12-29 19:12:24 +00:00
|
|
|
}
|
2011-04-23 10:42:31 +01:00
|
|
|
}
|