Updated to Drupal 8.5. Core Media not yet in use.
[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->beforeOptimizationPasses = array(
43             100 => array(
44                 $resolveClassPass = new ResolveClassPass(),
45                 new ResolveInstanceofConditionalsPass(),
46                 new RegisterEnvVarProcessorsPass(),
47             ),
48             -1000 => array(new ExtensionCompilerPass()),
49         );
50
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),
72         ));
73
74         $this->beforeRemovingPasses = array(
75             -100 => array(
76                 new ResolvePrivatesPass(),
77             ),
78         );
79
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(),
89             )),
90             new DefinitionErrorExceptionPass(),
91             new CheckExceptionOnInvalidReferenceBehaviorPass(),
92             new ResolveHotPathPass(),
93         ));
94     }
95
96     /**
97      * Returns all passes in order to be processed.
98      *
99      * @return CompilerPassInterface[]
100      */
101     public function getPasses()
102     {
103         return array_merge(
104             array($this->mergePass),
105             $this->getBeforeOptimizationPasses(),
106             $this->getOptimizationPasses(),
107             $this->getBeforeRemovingPasses(),
108             $this->getRemovingPasses(),
109             $this->getAfterRemovingPasses()
110         );
111     }
112
113     /**
114      * Adds a pass.
115      *
116      * @param CompilerPassInterface $pass     A Compiler pass
117      * @param string                $type     The pass type
118      * @param int                   $priority Used to sort the passes
119      *
120      * @throws InvalidArgumentException when a pass type doesn't exist
121      */
122     public function addPass(CompilerPassInterface $pass, $type = self::TYPE_BEFORE_OPTIMIZATION/*, int $priority = 0*/)
123     {
124         if (func_num_args() >= 3) {
125             $priority = func_get_arg(2);
126         } else {
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);
131                 }
132             }
133
134             $priority = 0;
135         }
136
137         $property = $type.'Passes';
138         if (!isset($this->$property)) {
139             throw new InvalidArgumentException(sprintf('Invalid type "%s".', $type));
140         }
141
142         $passes = &$this->$property;
143
144         if (!isset($passes[$priority])) {
145             $passes[$priority] = array();
146         }
147         $passes[$priority][] = $pass;
148     }
149
150     /**
151      * Gets all passes for the AfterRemoving pass.
152      *
153      * @return CompilerPassInterface[]
154      */
155     public function getAfterRemovingPasses()
156     {
157         return $this->sortPasses($this->afterRemovingPasses);
158     }
159
160     /**
161      * Gets all passes for the BeforeOptimization pass.
162      *
163      * @return CompilerPassInterface[]
164      */
165     public function getBeforeOptimizationPasses()
166     {
167         return $this->sortPasses($this->beforeOptimizationPasses);
168     }
169
170     /**
171      * Gets all passes for the BeforeRemoving pass.
172      *
173      * @return CompilerPassInterface[]
174      */
175     public function getBeforeRemovingPasses()
176     {
177         return $this->sortPasses($this->beforeRemovingPasses);
178     }
179
180     /**
181      * Gets all passes for the Optimization pass.
182      *
183      * @return CompilerPassInterface[]
184      */
185     public function getOptimizationPasses()
186     {
187         return $this->sortPasses($this->optimizationPasses);
188     }
189
190     /**
191      * Gets all passes for the Removing pass.
192      *
193      * @return CompilerPassInterface[]
194      */
195     public function getRemovingPasses()
196     {
197         return $this->sortPasses($this->removingPasses);
198     }
199
200     /**
201      * Gets the Merge pass.
202      *
203      * @return CompilerPassInterface
204      */
205     public function getMergePass()
206     {
207         return $this->mergePass;
208     }
209
210     public function setMergePass(CompilerPassInterface $pass)
211     {
212         $this->mergePass = $pass;
213     }
214
215     /**
216      * Sets the AfterRemoving passes.
217      *
218      * @param CompilerPassInterface[] $passes
219      */
220     public function setAfterRemovingPasses(array $passes)
221     {
222         $this->afterRemovingPasses = array($passes);
223     }
224
225     /**
226      * Sets the BeforeOptimization passes.
227      *
228      * @param CompilerPassInterface[] $passes
229      */
230     public function setBeforeOptimizationPasses(array $passes)
231     {
232         $this->beforeOptimizationPasses = array($passes);
233     }
234
235     /**
236      * Sets the BeforeRemoving passes.
237      *
238      * @param CompilerPassInterface[] $passes
239      */
240     public function setBeforeRemovingPasses(array $passes)
241     {
242         $this->beforeRemovingPasses = array($passes);
243     }
244
245     /**
246      * Sets the Optimization passes.
247      *
248      * @param CompilerPassInterface[] $passes
249      */
250     public function setOptimizationPasses(array $passes)
251     {
252         $this->optimizationPasses = array($passes);
253     }
254
255     /**
256      * Sets the Removing passes.
257      *
258      * @param CompilerPassInterface[] $passes
259      */
260     public function setRemovingPasses(array $passes)
261     {
262         $this->removingPasses = array($passes);
263     }
264
265     /**
266      * Sort passes by priority.
267      *
268      * @param array $passes CompilerPassInterface instances with their priority as key
269      *
270      * @return CompilerPassInterface[]
271      */
272     private function sortPasses(array $passes)
273     {
274         if (0 === count($passes)) {
275             return array();
276         }
277
278         krsort($passes);
279
280         // Flatten the array
281         return call_user_func_array('array_merge', $passes);
282     }
283 }