Compiler.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. * @param string $type The type of the pass
  50. * @param int $priority Used to sort the passes
  51. */
  52. public function addPass(CompilerPassInterface $pass, $type = PassConfig::TYPE_BEFORE_OPTIMIZATION, int $priority = 0)
  53. {
  54. $this->passConfig->addPass($pass, $type, $priority);
  55. }
  56. /**
  57. * @final
  58. */
  59. public function log(CompilerPassInterface $pass, string $message)
  60. {
  61. if (false !== strpos($message, "\n")) {
  62. $message = str_replace("\n", "\n".\get_class($pass).': ', trim($message));
  63. }
  64. $this->log[] = \get_class($pass).': '.$message;
  65. }
  66. /**
  67. * Returns the log.
  68. *
  69. * @return array Log array
  70. */
  71. public function getLog()
  72. {
  73. return $this->log;
  74. }
  75. /**
  76. * Run the Compiler and process all Passes.
  77. */
  78. public function compile(ContainerBuilder $container)
  79. {
  80. try {
  81. foreach ($this->passConfig->getPasses() as $pass) {
  82. $pass->process($container);
  83. }
  84. } catch (\Exception $e) {
  85. $usedEnvs = [];
  86. $prev = $e;
  87. do {
  88. $msg = $prev->getMessage();
  89. if ($msg !== $resolvedMsg = $container->resolveEnvPlaceholders($msg, null, $usedEnvs)) {
  90. $r = new \ReflectionProperty($prev, 'message');
  91. $r->setAccessible(true);
  92. $r->setValue($prev, $resolvedMsg);
  93. }
  94. } while ($prev = $prev->getPrevious());
  95. if ($usedEnvs) {
  96. $e = new EnvParameterException($usedEnvs, $e);
  97. }
  98. throw $e;
  99. } finally {
  100. $this->getServiceReferenceGraph()->clear();
  101. }
  102. }
  103. }