PassConfig.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  18. */
  19. class PassConfig
  20. {
  21. public const TYPE_AFTER_REMOVING = 'afterRemoving';
  22. public const TYPE_BEFORE_OPTIMIZATION = 'beforeOptimization';
  23. public const TYPE_BEFORE_REMOVING = 'beforeRemoving';
  24. public const TYPE_OPTIMIZE = 'optimization';
  25. public const TYPE_REMOVE = 'removing';
  26. private $mergePass;
  27. private $afterRemovingPasses = [];
  28. private $beforeOptimizationPasses = [];
  29. private $beforeRemovingPasses = [];
  30. private $optimizationPasses;
  31. private $removingPasses;
  32. public function __construct()
  33. {
  34. $this->mergePass = new MergeExtensionConfigurationPass();
  35. $this->beforeOptimizationPasses = [
  36. 100 => [
  37. new ResolveClassPass(),
  38. new ResolveInstanceofConditionalsPass(),
  39. new RegisterEnvVarProcessorsPass(),
  40. ],
  41. -1000 => [new ExtensionCompilerPass()],
  42. ];
  43. $this->optimizationPasses = [[
  44. new ValidateEnvPlaceholdersPass(),
  45. new ResolveChildDefinitionsPass(),
  46. new RegisterServiceSubscribersPass(),
  47. new ResolveParameterPlaceHoldersPass(false, false),
  48. new ResolveFactoryClassPass(),
  49. new ResolveNamedArgumentsPass(),
  50. new AutowireRequiredMethodsPass(),
  51. new ResolveBindingsPass(),
  52. new ServiceLocatorTagPass(),
  53. new DecoratorServicePass(),
  54. new CheckDefinitionValidityPass(),
  55. new AutowirePass(false),
  56. new ResolveTaggedIteratorArgumentPass(),
  57. new ResolveServiceSubscribersPass(),
  58. new ResolveReferencesToAliasesPass(),
  59. new ResolveInvalidReferencesPass(),
  60. new AnalyzeServiceReferencesPass(true),
  61. new CheckCircularReferencesPass(),
  62. new CheckReferenceValidityPass(),
  63. new CheckArgumentsValidityPass(false),
  64. ]];
  65. $this->beforeRemovingPasses = [
  66. -100 => [
  67. new ResolvePrivatesPass(),
  68. ],
  69. ];
  70. $this->removingPasses = [[
  71. new RemovePrivateAliasesPass(),
  72. new ReplaceAliasByActualDefinitionPass(),
  73. new RemoveAbstractDefinitionsPass(),
  74. new RemoveUnusedDefinitionsPass(),
  75. new AnalyzeServiceReferencesPass(),
  76. new CheckExceptionOnInvalidReferenceBehaviorPass(),
  77. new InlineServiceDefinitionsPass(new AnalyzeServiceReferencesPass()),
  78. new AnalyzeServiceReferencesPass(),
  79. new DefinitionErrorExceptionPass(),
  80. ]];
  81. $this->afterRemovingPasses = [[
  82. new ResolveHotPathPass(),
  83. ]];
  84. }
  85. /**
  86. * Returns all passes in order to be processed.
  87. *
  88. * @return CompilerPassInterface[]
  89. */
  90. public function getPasses()
  91. {
  92. return array_merge(
  93. [$this->mergePass],
  94. $this->getBeforeOptimizationPasses(),
  95. $this->getOptimizationPasses(),
  96. $this->getBeforeRemovingPasses(),
  97. $this->getRemovingPasses(),
  98. $this->getAfterRemovingPasses()
  99. );
  100. }
  101. /**
  102. * Adds a pass.
  103. *
  104. * @param string $type The pass type
  105. * @param int $priority Used to sort the passes
  106. *
  107. * @throws InvalidArgumentException when a pass type doesn't exist
  108. */
  109. public function addPass(CompilerPassInterface $pass, $type = self::TYPE_BEFORE_OPTIMIZATION, int $priority = 0)
  110. {
  111. $property = $type.'Passes';
  112. if (!isset($this->$property)) {
  113. throw new InvalidArgumentException(sprintf('Invalid type "%s".', $type));
  114. }
  115. $passes = &$this->$property;
  116. if (!isset($passes[$priority])) {
  117. $passes[$priority] = [];
  118. }
  119. $passes[$priority][] = $pass;
  120. }
  121. /**
  122. * Gets all passes for the AfterRemoving pass.
  123. *
  124. * @return CompilerPassInterface[]
  125. */
  126. public function getAfterRemovingPasses()
  127. {
  128. return $this->sortPasses($this->afterRemovingPasses);
  129. }
  130. /**
  131. * Gets all passes for the BeforeOptimization pass.
  132. *
  133. * @return CompilerPassInterface[]
  134. */
  135. public function getBeforeOptimizationPasses()
  136. {
  137. return $this->sortPasses($this->beforeOptimizationPasses);
  138. }
  139. /**
  140. * Gets all passes for the BeforeRemoving pass.
  141. *
  142. * @return CompilerPassInterface[]
  143. */
  144. public function getBeforeRemovingPasses()
  145. {
  146. return $this->sortPasses($this->beforeRemovingPasses);
  147. }
  148. /**
  149. * Gets all passes for the Optimization pass.
  150. *
  151. * @return CompilerPassInterface[]
  152. */
  153. public function getOptimizationPasses()
  154. {
  155. return $this->sortPasses($this->optimizationPasses);
  156. }
  157. /**
  158. * Gets all passes for the Removing pass.
  159. *
  160. * @return CompilerPassInterface[]
  161. */
  162. public function getRemovingPasses()
  163. {
  164. return $this->sortPasses($this->removingPasses);
  165. }
  166. /**
  167. * Gets the Merge pass.
  168. *
  169. * @return CompilerPassInterface
  170. */
  171. public function getMergePass()
  172. {
  173. return $this->mergePass;
  174. }
  175. public function setMergePass(CompilerPassInterface $pass)
  176. {
  177. $this->mergePass = $pass;
  178. }
  179. /**
  180. * Sets the AfterRemoving passes.
  181. *
  182. * @param CompilerPassInterface[] $passes
  183. */
  184. public function setAfterRemovingPasses(array $passes)
  185. {
  186. $this->afterRemovingPasses = [$passes];
  187. }
  188. /**
  189. * Sets the BeforeOptimization passes.
  190. *
  191. * @param CompilerPassInterface[] $passes
  192. */
  193. public function setBeforeOptimizationPasses(array $passes)
  194. {
  195. $this->beforeOptimizationPasses = [$passes];
  196. }
  197. /**
  198. * Sets the BeforeRemoving passes.
  199. *
  200. * @param CompilerPassInterface[] $passes
  201. */
  202. public function setBeforeRemovingPasses(array $passes)
  203. {
  204. $this->beforeRemovingPasses = [$passes];
  205. }
  206. /**
  207. * Sets the Optimization passes.
  208. *
  209. * @param CompilerPassInterface[] $passes
  210. */
  211. public function setOptimizationPasses(array $passes)
  212. {
  213. $this->optimizationPasses = [$passes];
  214. }
  215. /**
  216. * Sets the Removing passes.
  217. *
  218. * @param CompilerPassInterface[] $passes
  219. */
  220. public function setRemovingPasses(array $passes)
  221. {
  222. $this->removingPasses = [$passes];
  223. }
  224. /**
  225. * Sort passes by priority.
  226. *
  227. * @param array $passes CompilerPassInterface instances with their priority as key
  228. *
  229. * @return CompilerPassInterface[]
  230. */
  231. private function sortPasses(array $passes): array
  232. {
  233. if (0 === \count($passes)) {
  234. return [];
  235. }
  236. krsort($passes);
  237. // Flatten the array
  238. return array_merge(...$passes);
  239. }
  240. }