vendor/symfony/dependency-injection/Compiler/PassConfig.php line 38

Open in your IDE?
  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 array $afterRemovingPasses;
  28.     private array $beforeOptimizationPasses;
  29.     private array $beforeRemovingPasses = [];
  30.     private array $optimizationPasses;
  31.     private array $removingPasses;
  32.     public function __construct()
  33.     {
  34.         $this->mergePass = new MergeExtensionConfigurationPass();
  35.         $this->beforeOptimizationPasses = [
  36.             100 => [
  37.                 new ResolveClassPass(),
  38.                 new RegisterAutoconfigureAttributesPass(),
  39.                 new AttributeAutoconfigurationPass(),
  40.                 new ResolveInstanceofConditionalsPass(),
  41.                 new RegisterEnvVarProcessorsPass(),
  42.             ],
  43.             -1000 => [new ExtensionCompilerPass()],
  44.         ];
  45.         $this->optimizationPasses = [[
  46.             new AutoAliasServicePass(),
  47.             new ValidateEnvPlaceholdersPass(),
  48.             new ResolveDecoratorStackPass(),
  49.             new ResolveChildDefinitionsPass(),
  50.             new RegisterServiceSubscribersPass(),
  51.             new ResolveParameterPlaceHoldersPass(false, false),
  52.             new ResolveFactoryClassPass(),
  53.             new ResolveNamedArgumentsPass(),
  54.             new AutowireRequiredMethodsPass(),
  55.             new AutowireRequiredPropertiesPass(),
  56.             new ResolveBindingsPass(),
  57.             new ServiceLocatorTagPass(),
  58.             new DecoratorServicePass(),
  59.             new CheckDefinitionValidityPass(),
  60.             new AutowirePass(false),
  61.             new ServiceLocatorTagPass(),
  62.             new ResolveTaggedIteratorArgumentPass(),
  63.             new ResolveServiceSubscribersPass(),
  64.             new ResolveReferencesToAliasesPass(),
  65.             new ResolveInvalidReferencesPass(),
  66.             new AnalyzeServiceReferencesPass(true),
  67.             new CheckCircularReferencesPass(),
  68.             new CheckReferenceValidityPass(),
  69.             new CheckArgumentsValidityPass(false),
  70.         ]];
  71.         $this->removingPasses = [[
  72.             new RemovePrivateAliasesPass(),
  73.             new ReplaceAliasByActualDefinitionPass(),
  74.             new RemoveAbstractDefinitionsPass(),
  75.             new RemoveUnusedDefinitionsPass(),
  76.             new AnalyzeServiceReferencesPass(),
  77.             new CheckExceptionOnInvalidReferenceBehaviorPass(),
  78.             new InlineServiceDefinitionsPass(new AnalyzeServiceReferencesPass()),
  79.             new AnalyzeServiceReferencesPass(),
  80.             new DefinitionErrorExceptionPass(),
  81.         ]];
  82.         $this->afterRemovingPasses = [[
  83.             new ResolveHotPathPass(),
  84.             new ResolveNoPreloadPass(),
  85.             new AliasDeprecatedPublicServicesPass(),
  86.         ]];
  87.     }
  88.     /**
  89.      * Returns all passes in order to be processed.
  90.      *
  91.      * @return CompilerPassInterface[]
  92.      */
  93.     public function getPasses(): array
  94.     {
  95.         return array_merge(
  96.             [$this->mergePass],
  97.             $this->getBeforeOptimizationPasses(),
  98.             $this->getOptimizationPasses(),
  99.             $this->getBeforeRemovingPasses(),
  100.             $this->getRemovingPasses(),
  101.             $this->getAfterRemovingPasses()
  102.         );
  103.     }
  104.     /**
  105.      * Adds a pass.
  106.      *
  107.      * @throws InvalidArgumentException when a pass type doesn't exist
  108.      */
  109.     public function addPass(CompilerPassInterface $pass, string $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(): array
  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(): array
  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(): array
  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(): array
  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(): array
  163.     {
  164.         return $this->sortPasses($this->removingPasses);
  165.     }
  166.     /**
  167.      * Gets the Merge pass.
  168.      */
  169.     public function getMergePass(): CompilerPassInterface
  170.     {
  171.         return $this->mergePass;
  172.     }
  173.     public function setMergePass(CompilerPassInterface $pass)
  174.     {
  175.         $this->mergePass = $pass;
  176.     }
  177.     /**
  178.      * Sets the AfterRemoving passes.
  179.      *
  180.      * @param CompilerPassInterface[] $passes
  181.      */
  182.     public function setAfterRemovingPasses(array $passes)
  183.     {
  184.         $this->afterRemovingPasses = [$passes];
  185.     }
  186.     /**
  187.      * Sets the BeforeOptimization passes.
  188.      *
  189.      * @param CompilerPassInterface[] $passes
  190.      */
  191.     public function setBeforeOptimizationPasses(array $passes)
  192.     {
  193.         $this->beforeOptimizationPasses = [$passes];
  194.     }
  195.     /**
  196.      * Sets the BeforeRemoving passes.
  197.      *
  198.      * @param CompilerPassInterface[] $passes
  199.      */
  200.     public function setBeforeRemovingPasses(array $passes)
  201.     {
  202.         $this->beforeRemovingPasses = [$passes];
  203.     }
  204.     /**
  205.      * Sets the Optimization passes.
  206.      *
  207.      * @param CompilerPassInterface[] $passes
  208.      */
  209.     public function setOptimizationPasses(array $passes)
  210.     {
  211.         $this->optimizationPasses = [$passes];
  212.     }
  213.     /**
  214.      * Sets the Removing passes.
  215.      *
  216.      * @param CompilerPassInterface[] $passes
  217.      */
  218.     public function setRemovingPasses(array $passes)
  219.     {
  220.         $this->removingPasses = [$passes];
  221.     }
  222.     /**
  223.      * Sort passes by priority.
  224.      *
  225.      * @param array $passes CompilerPassInterface instances with their priority as key
  226.      *
  227.      * @return CompilerPassInterface[]
  228.      */
  229.     private function sortPasses(array $passes): array
  230.     {
  231.         if (0 === \count($passes)) {
  232.             return [];
  233.         }
  234.         krsort($passes);
  235.         // Flatten the array
  236.         return array_merge(...$passes);
  237.     }
  238. }