4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection\Compiler;
14 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
17 * Compiler Pass Configuration.
19 * This class has a default configuration embedded.
21 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
25 const TYPE_AFTER_REMOVING = 'afterRemoving';
26 const TYPE_BEFORE_OPTIMIZATION = 'beforeOptimization';
27 const TYPE_BEFORE_REMOVING = 'beforeRemoving';
28 const TYPE_OPTIMIZE = 'optimization';
29 const TYPE_REMOVE = 'removing';
32 private $afterRemovingPasses = array();
33 private $beforeOptimizationPasses = array();
34 private $beforeRemovingPasses = array();
35 private $optimizationPasses;
36 private $removingPasses;
38 public function __construct()
40 $this->mergePass = new MergeExtensionConfigurationPass();
42 $this->beforeOptimizationPasses = array(
44 $resolveClassPass = new ResolveClassPass(),
45 new ResolveInstanceofConditionalsPass(),
46 new RegisterEnvVarProcessorsPass(),
48 -1000 => array(new ExtensionCompilerPass()),
51 $this->optimizationPasses = array(array(
52 new ResolveChildDefinitionsPass(),
53 new ServiceLocatorTagPass(),
54 new DecoratorServicePass(),
55 new ResolveParameterPlaceHoldersPass(false),
56 new ResolveFactoryClassPass(),
57 new FactoryReturnTypePass($resolveClassPass),
58 new CheckDefinitionValidityPass(),
59 new RegisterServiceSubscribersPass(),
60 new ResolveNamedArgumentsPass(),
61 new AutowireRequiredMethodsPass(),
62 new ResolveBindingsPass(),
63 new AutowirePass(false),
64 new ResolveTaggedIteratorArgumentPass(),
65 new ResolveServiceSubscribersPass(),
66 new ResolveReferencesToAliasesPass(),
67 new ResolveInvalidReferencesPass(),
68 new AnalyzeServiceReferencesPass(true),
69 new CheckCircularReferencesPass(),
70 new CheckReferenceValidityPass(),
71 new CheckArgumentsValidityPass(false),
74 $this->beforeRemovingPasses = array(
76 new ResolvePrivatesPass(),
80 $this->removingPasses = array(array(
81 new RemovePrivateAliasesPass(),
82 new ReplaceAliasByActualDefinitionPass(),
83 new RemoveAbstractDefinitionsPass(),
84 new RepeatedPass(array(
85 new AnalyzeServiceReferencesPass(),
86 new InlineServiceDefinitionsPass(),
87 new AnalyzeServiceReferencesPass(),
88 new RemoveUnusedDefinitionsPass(),
90 new DefinitionErrorExceptionPass(),
91 new CheckExceptionOnInvalidReferenceBehaviorPass(),
92 new ResolveHotPathPass(),
97 * Returns all passes in order to be processed.
99 * @return CompilerPassInterface[]
101 public function getPasses()
104 array($this->mergePass),
105 $this->getBeforeOptimizationPasses(),
106 $this->getOptimizationPasses(),
107 $this->getBeforeRemovingPasses(),
108 $this->getRemovingPasses(),
109 $this->getAfterRemovingPasses()
116 * @param CompilerPassInterface $pass A Compiler pass
117 * @param string $type The pass type
118 * @param int $priority Used to sort the passes
120 * @throws InvalidArgumentException when a pass type doesn't exist
122 public function addPass(CompilerPassInterface $pass, $type = self::TYPE_BEFORE_OPTIMIZATION/*, int $priority = 0*/)
124 if (\func_num_args() >= 3) {
125 $priority = func_get_arg(2);
127 if (__CLASS__ !== \get_class($this)) {
128 $r = new \ReflectionMethod($this, __FUNCTION__);
129 if (__CLASS__ !== $r->getDeclaringClass()->getName()) {
130 @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);
137 $property = $type.'Passes';
138 if (!isset($this->$property)) {
139 throw new InvalidArgumentException(sprintf('Invalid type "%s".', $type));
142 $passes = &$this->$property;
144 if (!isset($passes[$priority])) {
145 $passes[$priority] = array();
147 $passes[$priority][] = $pass;
151 * Gets all passes for the AfterRemoving pass.
153 * @return CompilerPassInterface[]
155 public function getAfterRemovingPasses()
157 return $this->sortPasses($this->afterRemovingPasses);
161 * Gets all passes for the BeforeOptimization pass.
163 * @return CompilerPassInterface[]
165 public function getBeforeOptimizationPasses()
167 return $this->sortPasses($this->beforeOptimizationPasses);
171 * Gets all passes for the BeforeRemoving pass.
173 * @return CompilerPassInterface[]
175 public function getBeforeRemovingPasses()
177 return $this->sortPasses($this->beforeRemovingPasses);
181 * Gets all passes for the Optimization pass.
183 * @return CompilerPassInterface[]
185 public function getOptimizationPasses()
187 return $this->sortPasses($this->optimizationPasses);
191 * Gets all passes for the Removing pass.
193 * @return CompilerPassInterface[]
195 public function getRemovingPasses()
197 return $this->sortPasses($this->removingPasses);
201 * Gets the Merge pass.
203 * @return CompilerPassInterface
205 public function getMergePass()
207 return $this->mergePass;
210 public function setMergePass(CompilerPassInterface $pass)
212 $this->mergePass = $pass;
216 * Sets the AfterRemoving passes.
218 * @param CompilerPassInterface[] $passes
220 public function setAfterRemovingPasses(array $passes)
222 $this->afterRemovingPasses = array($passes);
226 * Sets the BeforeOptimization passes.
228 * @param CompilerPassInterface[] $passes
230 public function setBeforeOptimizationPasses(array $passes)
232 $this->beforeOptimizationPasses = array($passes);
236 * Sets the BeforeRemoving passes.
238 * @param CompilerPassInterface[] $passes
240 public function setBeforeRemovingPasses(array $passes)
242 $this->beforeRemovingPasses = array($passes);
246 * Sets the Optimization passes.
248 * @param CompilerPassInterface[] $passes
250 public function setOptimizationPasses(array $passes)
252 $this->optimizationPasses = array($passes);
256 * Sets the Removing passes.
258 * @param CompilerPassInterface[] $passes
260 public function setRemovingPasses(array $passes)
262 $this->removingPasses = array($passes);
266 * Sort passes by priority.
268 * @param array $passes CompilerPassInterface instances with their priority as key
270 * @return CompilerPassInterface[]
272 private function sortPasses(array $passes)
274 if (0 === \count($passes)) {
281 return \call_user_func_array('array_merge', $passes);