PassConfig.php 7.5 KB

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