PassConfig.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 AutoAliasServicePass(),
  45. new ValidateEnvPlaceholdersPass(),
  46. new ResolveDecoratorStackPass(),
  47. new ResolveChildDefinitionsPass(),
  48. new RegisterServiceSubscribersPass(),
  49. new ResolveParameterPlaceHoldersPass(false, false),
  50. new ResolveFactoryClassPass(),
  51. new ResolveNamedArgumentsPass(),
  52. new AutowireRequiredMethodsPass(),
  53. new AutowireRequiredPropertiesPass(),
  54. new ResolveBindingsPass(),
  55. new ServiceLocatorTagPass(),
  56. new DecoratorServicePass(),
  57. new CheckDefinitionValidityPass(),
  58. new AutowirePass(false),
  59. new ResolveTaggedIteratorArgumentPass(),
  60. new ResolveServiceSubscribersPass(),
  61. new ResolveReferencesToAliasesPass(),
  62. new ResolveInvalidReferencesPass(),
  63. new AnalyzeServiceReferencesPass(true),
  64. new CheckCircularReferencesPass(),
  65. new CheckReferenceValidityPass(),
  66. new CheckArgumentsValidityPass(false),
  67. ]];
  68. $this->removingPasses = [[
  69. new RemovePrivateAliasesPass(),
  70. new ReplaceAliasByActualDefinitionPass(),
  71. new RemoveAbstractDefinitionsPass(),
  72. new RemoveUnusedDefinitionsPass(),
  73. new AnalyzeServiceReferencesPass(),
  74. new CheckExceptionOnInvalidReferenceBehaviorPass(),
  75. new InlineServiceDefinitionsPass(new AnalyzeServiceReferencesPass()),
  76. new AnalyzeServiceReferencesPass(),
  77. new DefinitionErrorExceptionPass(),
  78. ]];
  79. $this->afterRemovingPasses = [[
  80. new ResolveHotPathPass(),
  81. new ResolveNoPreloadPass(),
  82. new AliasDeprecatedPublicServicesPass(),
  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. * @throws InvalidArgumentException when a pass type doesn't exist
  105. */
  106. public function addPass(CompilerPassInterface $pass, string $type = self::TYPE_BEFORE_OPTIMIZATION, int $priority = 0)
  107. {
  108. $property = $type.'Passes';
  109. if (!isset($this->$property)) {
  110. throw new InvalidArgumentException(sprintf('Invalid type "%s".', $type));
  111. }
  112. $passes = &$this->$property;
  113. if (!isset($passes[$priority])) {
  114. $passes[$priority] = [];
  115. }
  116. $passes[$priority][] = $pass;
  117. }
  118. /**
  119. * Gets all passes for the AfterRemoving pass.
  120. *
  121. * @return CompilerPassInterface[]
  122. */
  123. public function getAfterRemovingPasses()
  124. {
  125. return $this->sortPasses($this->afterRemovingPasses);
  126. }
  127. /**
  128. * Gets all passes for the BeforeOptimization pass.
  129. *
  130. * @return CompilerPassInterface[]
  131. */
  132. public function getBeforeOptimizationPasses()
  133. {
  134. return $this->sortPasses($this->beforeOptimizationPasses);
  135. }
  136. /**
  137. * Gets all passes for the BeforeRemoving pass.
  138. *
  139. * @return CompilerPassInterface[]
  140. */
  141. public function getBeforeRemovingPasses()
  142. {
  143. return $this->sortPasses($this->beforeRemovingPasses);
  144. }
  145. /**
  146. * Gets all passes for the Optimization pass.
  147. *
  148. * @return CompilerPassInterface[]
  149. */
  150. public function getOptimizationPasses()
  151. {
  152. return $this->sortPasses($this->optimizationPasses);
  153. }
  154. /**
  155. * Gets all passes for the Removing pass.
  156. *
  157. * @return CompilerPassInterface[]
  158. */
  159. public function getRemovingPasses()
  160. {
  161. return $this->sortPasses($this->removingPasses);
  162. }
  163. /**
  164. * Gets the Merge pass.
  165. *
  166. * @return CompilerPassInterface
  167. */
  168. public function getMergePass()
  169. {
  170. return $this->mergePass;
  171. }
  172. public function setMergePass(CompilerPassInterface $pass)
  173. {
  174. $this->mergePass = $pass;
  175. }
  176. /**
  177. * Sets the AfterRemoving passes.
  178. *
  179. * @param CompilerPassInterface[] $passes
  180. */
  181. public function setAfterRemovingPasses(array $passes)
  182. {
  183. $this->afterRemovingPasses = [$passes];
  184. }
  185. /**
  186. * Sets the BeforeOptimization passes.
  187. *
  188. * @param CompilerPassInterface[] $passes
  189. */
  190. public function setBeforeOptimizationPasses(array $passes)
  191. {
  192. $this->beforeOptimizationPasses = [$passes];
  193. }
  194. /**
  195. * Sets the BeforeRemoving passes.
  196. *
  197. * @param CompilerPassInterface[] $passes
  198. */
  199. public function setBeforeRemovingPasses(array $passes)
  200. {
  201. $this->beforeRemovingPasses = [$passes];
  202. }
  203. /**
  204. * Sets the Optimization passes.
  205. *
  206. * @param CompilerPassInterface[] $passes
  207. */
  208. public function setOptimizationPasses(array $passes)
  209. {
  210. $this->optimizationPasses = [$passes];
  211. }
  212. /**
  213. * Sets the Removing passes.
  214. *
  215. * @param CompilerPassInterface[] $passes
  216. */
  217. public function setRemovingPasses(array $passes)
  218. {
  219. $this->removingPasses = [$passes];
  220. }
  221. /**
  222. * Sort passes by priority.
  223. *
  224. * @param array $passes CompilerPassInterface instances with their priority as key
  225. *
  226. * @return CompilerPassInterface[]
  227. */
  228. private function sortPasses(array $passes): array
  229. {
  230. if (0 === \count($passes)) {
  231. return [];
  232. }
  233. krsort($passes);
  234. // Flatten the array
  235. return array_merge(...$passes);
  236. }
  237. }