This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/DependencyInjection/Compiler/PassConfig.php

274 lines
7.4 KiB
PHP
Raw Normal View History

2010-12-29 19:12:24 +00:00
<?php
/*
* This file is part of the Symfony package.
2010-12-29 19:12:24 +00:00
*
* (c) Fabien Potencier <fabien@symfony.com>
2010-12-29 19:12:24 +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
*/
namespace Symfony\Component\DependencyInjection\Compiler;
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
*
* This class has a default configuration embedded.
*
2010-12-29 19:12:24 +00:00
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class PassConfig
{
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';
private $mergePass;
private $afterRemovingPasses = array();
private $beforeOptimizationPasses = array();
private $beforeRemovingPasses = array();
private $optimizationPasses;
private $removingPasses;
2010-12-29 19:12:24 +00:00
public function __construct()
{
$this->mergePass = new MergeExtensionConfigurationPass();
$this->beforeOptimizationPasses = array(
100 => array(
$resolveClassPass = new ResolveClassPass(),
new ResolveDefinitionInheritancePass(),
),
);
$this->optimizationPasses = array(array(
new ExtensionCompilerPass(),
2011-01-26 23:14:31 +00:00
new ResolveDefinitionTemplatesPass(),
new DecoratorServicePass(),
2010-12-29 19:12:24 +00:00
new ResolveParameterPlaceHoldersPass(),
new ResolveFactoryClassPass(),
2017-01-02 13:43:40 +00:00
new FactoryReturnTypePass($resolveClassPass),
new CheckDefinitionValidityPass(),
new ResolveNamedArgumentsPass(),
new AutowirePass(),
2011-01-05 11:13:27 +00:00
new ResolveReferencesToAliasesPass(),
new ResolveInvalidReferencesPass(),
2011-01-17 22:28:59 +00:00
new AnalyzeServiceReferencesPass(true),
new CheckCircularReferencesPass(),
2011-01-26 23:14:31 +00:00
new CheckReferenceValidityPass(),
new CheckArgumentsValidityPass(),
));
2010-12-29 19:12:24 +00:00
$this->removingPasses = array(array(
2011-01-07 14:44:29 +00:00
new RemovePrivateAliasesPass(),
new ReplaceAliasByActualDefinitionPass(),
new RemoveAbstractDefinitionsPass(),
new RepeatedPass(array(
new AnalyzeServiceReferencesPass(),
new InlineServiceDefinitionsPass(),
new AnalyzeServiceReferencesPass(),
new RemoveUnusedDefinitionsPass(),
)),
new CheckExceptionOnInvalidReferenceBehaviorPass(),
));
2010-12-29 19:12:24 +00:00
}
2011-02-13 18:06:41 +00:00
/**
* Returns all passes in order to be processed.
*
* @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),
$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.
*
* @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
*
* @throws InvalidArgumentException when a pass type doesn't exist
2011-02-13 18:06:41 +00:00
*/
public function addPass(CompilerPassInterface $pass, $type = self::TYPE_BEFORE_OPTIMIZATION/*, $priority = 0*/)
2010-12-29 19:12:24 +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 `$priority = 0` argument in version 4.0. Not defining it is deprecated since 3.2.', __METHOD__), E_USER_DEPRECATED);
}
}
$priority = 0;
}
2010-12-29 19:12:24 +00:00
$property = $type.'Passes';
if (!isset($this->$property)) {
throw new InvalidArgumentException(sprintf('Invalid type "%s".', $type));
2010-12-29 19:12:24 +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.
*
* @return CompilerPassInterface[]
2011-02-13 18:06:41 +00:00
*/
2011-01-26 23:14:31 +00:00
public function getAfterRemovingPasses()
{
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.
*
* @return CompilerPassInterface[]
2011-02-13 18:06:41 +00:00
*/
public function getBeforeOptimizationPasses()
{
return $this->sortPasses($this->beforeOptimizationPasses);
}
2011-02-13 18:06:41 +00:00
/**
* Gets all passes for the BeforeRemoving pass.
*
* @return CompilerPassInterface[]
2011-02-13 18:06:41 +00:00
*/
public function getBeforeRemovingPasses()
{
return $this->sortPasses($this->beforeRemovingPasses);
}
2011-02-13 18:06:41 +00:00
/**
* Gets all passes for the Optimization pass.
*
* @return CompilerPassInterface[]
2011-02-13 18:06:41 +00:00
*/
2010-12-29 19:12:24 +00:00
public function getOptimizationPasses()
{
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.
*
* @return CompilerPassInterface[]
2011-02-13 18:06:41 +00:00
*/
2010-12-29 19:12:24 +00:00
public function getRemovingPasses()
{
return $this->sortPasses($this->removingPasses);
2010-12-29 19:12:24 +00:00
}
2011-02-13 18:06:41 +00:00
/**
* Gets the Merge pass.
2011-02-13 18:06:41 +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.
*
* @param CompilerPassInterface[] $passes
2011-02-13 18:06:41 +00:00
*/
2011-01-26 23:14:31 +00:00
public function setAfterRemovingPasses(array $passes)
{
$this->afterRemovingPasses = array($passes);
2011-01-26 23:14:31 +00:00
}
2011-02-13 18:06:41 +00:00
/**
* Sets the BeforeOptimization passes.
*
* @param CompilerPassInterface[] $passes
2011-02-13 18:06:41 +00:00
*/
public function setBeforeOptimizationPasses(array $passes)
{
$this->beforeOptimizationPasses = array($passes);
}
2011-02-13 18:06:41 +00:00
/**
* Sets the BeforeRemoving passes.
*
* @param CompilerPassInterface[] $passes
2011-02-13 18:06:41 +00:00
*/
public function setBeforeRemovingPasses(array $passes)
{
$this->beforeRemovingPasses = array($passes);
}
2011-02-13 18:06:41 +00:00
/**
* Sets the Optimization passes.
*
* @param CompilerPassInterface[] $passes
2011-02-13 18:06:41 +00:00
*/
2010-12-29 19:12:24 +00:00
public function setOptimizationPasses(array $passes)
{
$this->optimizationPasses = array($passes);
2010-12-29 19:12:24 +00:00
}
2011-02-13 18:06:41 +00:00
/**
* Sets the Removing passes.
*
* @param CompilerPassInterface[] $passes
2011-02-13 18:06:41 +00:00
*/
2010-12-29 19:12:24 +00:00
public function setRemovingPasses(array $passes)
{
$this->removingPasses = array($passes);
}
/**
* Sort passes by priority.
*
2016-06-29 06:43:32 +01:00
* @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
}
}