vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Compiler/Compiler.php line 140

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\ContainerBuilder;
  12. use Symfony\Component\DependencyInjection\Exception\EnvParameterException;
  13. /**
  14.  * This class is used to remove circular dependencies between individual passes.
  15.  *
  16.  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  17.  */
  18. class Compiler
  19. {
  20.     private $passConfig;
  21.     private $log = [];
  22.     private $loggingFormatter;
  23.     private $serviceReferenceGraph;
  24.     public function __construct()
  25.     {
  26.         $this->passConfig = new PassConfig();
  27.         $this->serviceReferenceGraph = new ServiceReferenceGraph();
  28.     }
  29.     /**
  30.      * Returns the PassConfig.
  31.      *
  32.      * @return PassConfig The PassConfig instance
  33.      */
  34.     public function getPassConfig()
  35.     {
  36.         return $this->passConfig;
  37.     }
  38.     /**
  39.      * Returns the ServiceReferenceGraph.
  40.      *
  41.      * @return ServiceReferenceGraph The ServiceReferenceGraph instance
  42.      */
  43.     public function getServiceReferenceGraph()
  44.     {
  45.         return $this->serviceReferenceGraph;
  46.     }
  47.     /**
  48.      * Returns the logging formatter which can be used by compilation passes.
  49.      *
  50.      * @return LoggingFormatter
  51.      *
  52.      * @deprecated since version 3.3, to be removed in 4.0. Use the ContainerBuilder::log() method instead.
  53.      */
  54.     public function getLoggingFormatter()
  55.     {
  56.         if (null === $this->loggingFormatter) {
  57.             @trigger_error(sprintf('The %s() method is deprecated since Symfony 3.3 and will be removed in 4.0. Use the ContainerBuilder::log() method instead.'__METHOD__), E_USER_DEPRECATED);
  58.             $this->loggingFormatter = new LoggingFormatter();
  59.         }
  60.         return $this->loggingFormatter;
  61.     }
  62.     /**
  63.      * Adds a pass to the PassConfig.
  64.      *
  65.      * @param CompilerPassInterface $pass A compiler pass
  66.      * @param string                $type The type of the pass
  67.      */
  68.     public function addPass(CompilerPassInterface $pass$type PassConfig::TYPE_BEFORE_OPTIMIZATION/*, int $priority = 0*/)
  69.     {
  70.         if (\func_num_args() >= 3) {
  71.             $priority func_get_arg(2);
  72.         } else {
  73.             if (__CLASS__ !== \get_class($this)) {
  74.                 $r = new \ReflectionMethod($this__FUNCTION__);
  75.                 if (__CLASS__ !== $r->getDeclaringClass()->getName()) {
  76.                     @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);
  77.                 }
  78.             }
  79.             $priority 0;
  80.         }
  81.         $this->passConfig->addPass($pass$type$priority);
  82.     }
  83.     /**
  84.      * Adds a log message.
  85.      *
  86.      * @param string $string The log message
  87.      *
  88.      * @deprecated since version 3.3, to be removed in 4.0. Use the ContainerBuilder::log() method instead.
  89.      */
  90.     public function addLogMessage($string)
  91.     {
  92.         @trigger_error(sprintf('The %s() method is deprecated since Symfony 3.3 and will be removed in 4.0. Use the ContainerBuilder::log() method instead.'__METHOD__), E_USER_DEPRECATED);
  93.         $this->log[] = $string;
  94.     }
  95.     /**
  96.      * @final
  97.      */
  98.     public function log(CompilerPassInterface $pass$message)
  99.     {
  100.         if (false !== strpos($message"\n")) {
  101.             $message str_replace("\n""\n".\get_class($pass).': 'trim($message));
  102.         }
  103.         $this->log[] = \get_class($pass).': '.$message;
  104.     }
  105.     /**
  106.      * Returns the log.
  107.      *
  108.      * @return array Log array
  109.      */
  110.     public function getLog()
  111.     {
  112.         return $this->log;
  113.     }
  114.     /**
  115.      * Run the Compiler and process all Passes.
  116.      */
  117.     public function compile(ContainerBuilder $container)
  118.     {
  119.         try {
  120.             foreach ($this->passConfig->getPasses() as $pass) {
  121.                 $pass->process($container);
  122.             }
  123.         } catch (\Exception $e) {
  124.             $usedEnvs = [];
  125.             $prev $e;
  126.             do {
  127.                 $msg $prev->getMessage();
  128.                 if ($msg !== $resolvedMsg $container->resolveEnvPlaceholders($msgnull$usedEnvs)) {
  129.                     $r = new \ReflectionProperty($prev'message');
  130.                     $r->setAccessible(true);
  131.                     $r->setValue($prev$resolvedMsg);
  132.                 }
  133.             } while ($prev $prev->getPrevious());
  134.             if ($usedEnvs) {
  135.                 $e = new EnvParameterException($usedEnvs$e);
  136.             }
  137.             throw $e;
  138.         } finally {
  139.             $this->getServiceReferenceGraph()->clear();
  140.         }
  141.     }
  142. }