Security update for Core, with self-updated composer
[yaffs-website] / vendor / symfony / dependency-injection / Compiler / PassConfig.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\DependencyInjection\Compiler;
13
14 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
15
16 /**
17  * Compiler Pass Configuration.
18  *
19  * This class has a default configuration embedded.
20  *
21  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
22  */
23 class PassConfig
24 {
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';
30
31     private $mergePass;
32     private $afterRemovingPasses = array();
33     private $beforeOptimizationPasses = array();
34     private $beforeRemovingPasses = array();
35     private $optimizationPasses;
36     private $removingPasses;
37
38     public function __construct()
39     {
40         $this->mergePass = new MergeExtensionConfigurationPass();
41
42         $this->optimizationPasses = array(array(
43             new ExtensionCompilerPass(),
44             new ResolveDefinitionTemplatesPass(),
45             new DecoratorServicePass(),
46             new ResolveParameterPlaceHoldersPass(),
47             new FactoryReturnTypePass(),
48             new CheckDefinitionValidityPass(),
49             new ResolveReferencesToAliasesPass(),
50             new ResolveInvalidReferencesPass(),
51             new AutowirePass(),
52             new AnalyzeServiceReferencesPass(true),
53             new CheckCircularReferencesPass(),
54             new CheckReferenceValidityPass(),
55         ));
56
57         $this->removingPasses = array(array(
58             new RemovePrivateAliasesPass(),
59             new ReplaceAliasByActualDefinitionPass(),
60             new RemoveAbstractDefinitionsPass(),
61             new RepeatedPass(array(
62                 new AnalyzeServiceReferencesPass(),
63                 new InlineServiceDefinitionsPass(),
64                 new AnalyzeServiceReferencesPass(),
65                 new RemoveUnusedDefinitionsPass(),
66             )),
67             new CheckExceptionOnInvalidReferenceBehaviorPass(),
68         ));
69     }
70
71     /**
72      * Returns all passes in order to be processed.
73      *
74      * @return CompilerPassInterface[]
75      */
76     public function getPasses()
77     {
78         return array_merge(
79             array($this->mergePass),
80             $this->getBeforeOptimizationPasses(),
81             $this->getOptimizationPasses(),
82             $this->getBeforeRemovingPasses(),
83             $this->getRemovingPasses(),
84             $this->getAfterRemovingPasses()
85         );
86     }
87
88     /**
89      * Adds a pass.
90      *
91      * @param CompilerPassInterface $pass     A Compiler pass
92      * @param string                $type     The pass type
93      * @param int                   $priority Used to sort the passes
94      *
95      * @throws InvalidArgumentException when a pass type doesn't exist
96      */
97     public function addPass(CompilerPassInterface $pass, $type = self::TYPE_BEFORE_OPTIMIZATION/*, $priority = 0*/)
98     {
99         if (func_num_args() >= 3) {
100             $priority = func_get_arg(2);
101         } else {
102             if (__CLASS__ !== get_class($this)) {
103                 $r = new \ReflectionMethod($this, __FUNCTION__);
104                 if (__CLASS__ !== $r->getDeclaringClass()->getName()) {
105                     @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);
106                 }
107             }
108
109             $priority = 0;
110         }
111
112         $property = $type.'Passes';
113         if (!isset($this->$property)) {
114             throw new InvalidArgumentException(sprintf('Invalid type "%s".', $type));
115         }
116
117         $passes = &$this->$property;
118
119         if (!isset($passes[$priority])) {
120             $passes[$priority] = array();
121         }
122         $passes[$priority][] = $pass;
123     }
124
125     /**
126      * Gets all passes for the AfterRemoving pass.
127      *
128      * @return CompilerPassInterface[]
129      */
130     public function getAfterRemovingPasses()
131     {
132         return $this->sortPasses($this->afterRemovingPasses);
133     }
134
135     /**
136      * Gets all passes for the BeforeOptimization pass.
137      *
138      * @return CompilerPassInterface[]
139      */
140     public function getBeforeOptimizationPasses()
141     {
142         return $this->sortPasses($this->beforeOptimizationPasses);
143     }
144
145     /**
146      * Gets all passes for the BeforeRemoving pass.
147      *
148      * @return CompilerPassInterface[]
149      */
150     public function getBeforeRemovingPasses()
151     {
152         return $this->sortPasses($this->beforeRemovingPasses);
153     }
154
155     /**
156      * Gets all passes for the Optimization pass.
157      *
158      * @return CompilerPassInterface[]
159      */
160     public function getOptimizationPasses()
161     {
162         return $this->sortPasses($this->optimizationPasses);
163     }
164
165     /**
166      * Gets all passes for the Removing pass.
167      *
168      * @return CompilerPassInterface[]
169      */
170     public function getRemovingPasses()
171     {
172         return $this->sortPasses($this->removingPasses);
173     }
174
175     /**
176      * Gets the Merge pass.
177      *
178      * @return CompilerPassInterface
179      */
180     public function getMergePass()
181     {
182         return $this->mergePass;
183     }
184
185     /**
186      * Sets the Merge Pass.
187      *
188      * @param CompilerPassInterface $pass The merge pass
189      */
190     public function setMergePass(CompilerPassInterface $pass)
191     {
192         $this->mergePass = $pass;
193     }
194
195     /**
196      * Sets the AfterRemoving passes.
197      *
198      * @param CompilerPassInterface[] $passes
199      */
200     public function setAfterRemovingPasses(array $passes)
201     {
202         $this->afterRemovingPasses = array($passes);
203     }
204
205     /**
206      * Sets the BeforeOptimization passes.
207      *
208      * @param CompilerPassInterface[] $passes
209      */
210     public function setBeforeOptimizationPasses(array $passes)
211     {
212         $this->beforeOptimizationPasses = array($passes);
213     }
214
215     /**
216      * Sets the BeforeRemoving passes.
217      *
218      * @param CompilerPassInterface[] $passes
219      */
220     public function setBeforeRemovingPasses(array $passes)
221     {
222         $this->beforeRemovingPasses = array($passes);
223     }
224
225     /**
226      * Sets the Optimization passes.
227      *
228      * @param CompilerPassInterface[] $passes
229      */
230     public function setOptimizationPasses(array $passes)
231     {
232         $this->optimizationPasses = array($passes);
233     }
234
235     /**
236      * Sets the Removing passes.
237      *
238      * @param CompilerPassInterface[] $passes
239      */
240     public function setRemovingPasses(array $passes)
241     {
242         $this->removingPasses = array($passes);
243     }
244
245     /**
246      * Sort passes by priority.
247      *
248      * @param array $passes CompilerPassInterface instances with their priority as key
249      *
250      * @return CompilerPassInterface[]
251      */
252     private function sortPasses(array $passes)
253     {
254         if (0 === count($passes)) {
255             return array();
256         }
257
258         krsort($passes);
259
260         // Flatten the array
261         return call_user_func_array('array_merge', $passes);
262     }
263 }