Compiler.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 $serviceReferenceGraph;
  23. public function __construct()
  24. {
  25. $this->passConfig = new PassConfig();
  26. $this->serviceReferenceGraph = new ServiceReferenceGraph();
  27. }
  28. /**
  29. * Returns the PassConfig.
  30. *
  31. * @return PassConfig The PassConfig instance
  32. */
  33. public function getPassConfig()
  34. {
  35. return $this->passConfig;
  36. }
  37. /**
  38. * Returns the ServiceReferenceGraph.
  39. *
  40. * @return ServiceReferenceGraph The ServiceReferenceGraph instance
  41. */
  42. public function getServiceReferenceGraph()
  43. {
  44. return $this->serviceReferenceGraph;
  45. }
  46. /**
  47. * Adds a pass to the PassConfig.
  48. */
  49. public function addPass(CompilerPassInterface $pass, string $type = PassConfig::TYPE_BEFORE_OPTIMIZATION, int $priority = 0)
  50. {
  51. $this->passConfig->addPass($pass, $type, $priority);
  52. }
  53. /**
  54. * @final
  55. */
  56. public function log(CompilerPassInterface $pass, string $message)
  57. {
  58. if (false !== strpos($message, "\n")) {
  59. $message = str_replace("\n", "\n".\get_class($pass).': ', trim($message));
  60. }
  61. $this->log[] = \get_class($pass).': '.$message;
  62. }
  63. /**
  64. * Returns the log.
  65. *
  66. * @return array Log array
  67. */
  68. public function getLog()
  69. {
  70. return $this->log;
  71. }
  72. /**
  73. * Run the Compiler and process all Passes.
  74. */
  75. public function compile(ContainerBuilder $container)
  76. {
  77. try {
  78. foreach ($this->passConfig->getPasses() as $pass) {
  79. $pass->process($container);
  80. }
  81. } catch (\Exception $e) {
  82. $usedEnvs = [];
  83. $prev = $e;
  84. do {
  85. $msg = $prev->getMessage();
  86. if ($msg !== $resolvedMsg = $container->resolveEnvPlaceholders($msg, null, $usedEnvs)) {
  87. $r = new \ReflectionProperty($prev, 'message');
  88. $r->setAccessible(true);
  89. $r->setValue($prev, $resolvedMsg);
  90. }
  91. } while ($prev = $prev->getPrevious());
  92. if ($usedEnvs) {
  93. $e = new EnvParameterException($usedEnvs, $e);
  94. }
  95. throw $e;
  96. } finally {
  97. $this->getServiceReferenceGraph()->clear();
  98. }
  99. }
  100. }