Compiler.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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\Compiler\PassConfig;
  13. /**
  14. * This class is used to remove circular dependencies between individual passes.
  15. *
  16. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  17. *
  18. * @api
  19. */
  20. class Compiler
  21. {
  22. private $passConfig;
  23. private $log;
  24. private $loggingFormatter;
  25. private $serviceReferenceGraph;
  26. /**
  27. * Constructor.
  28. */
  29. public function __construct()
  30. {
  31. $this->passConfig = new PassConfig();
  32. $this->serviceReferenceGraph = new ServiceReferenceGraph();
  33. $this->loggingFormatter = new LoggingFormatter();
  34. $this->log = array();
  35. }
  36. /**
  37. * Returns the PassConfig.
  38. *
  39. * @return PassConfig The PassConfig instance
  40. *
  41. * @api
  42. */
  43. public function getPassConfig()
  44. {
  45. return $this->passConfig;
  46. }
  47. /**
  48. * Returns the ServiceReferenceGraph.
  49. *
  50. * @return ServiceReferenceGraph The ServiceReferenceGraph instance
  51. *
  52. * @api
  53. */
  54. public function getServiceReferenceGraph()
  55. {
  56. return $this->serviceReferenceGraph;
  57. }
  58. /**
  59. * Returns the logging formatter which can be used by compilation passes.
  60. *
  61. * @return LoggingFormatter
  62. */
  63. public function getLoggingFormatter()
  64. {
  65. return $this->loggingFormatter;
  66. }
  67. /**
  68. * Adds a pass to the PassConfig.
  69. *
  70. * @param CompilerPassInterface $pass A compiler pass
  71. * @param string $type The type of the pass
  72. *
  73. * @api
  74. */
  75. public function addPass(CompilerPassInterface $pass, $type = PassConfig::TYPE_BEFORE_OPTIMIZATION)
  76. {
  77. $this->passConfig->addPass($pass, $type);
  78. }
  79. /**
  80. * Adds a log message.
  81. *
  82. * @param string $string The log message
  83. */
  84. public function addLogMessage($string)
  85. {
  86. $this->log[] = $string;
  87. }
  88. /**
  89. * Returns the log.
  90. *
  91. * @return array Log array
  92. */
  93. public function getLog()
  94. {
  95. return $this->log;
  96. }
  97. /**
  98. * Run the Compiler and process all Passes.
  99. *
  100. * @param ContainerBuilder $container
  101. *
  102. * @api
  103. */
  104. public function compile(ContainerBuilder $container)
  105. {
  106. foreach ($this->passConfig->getPasses() as $pass) {
  107. $pass->process($container);
  108. }
  109. }
  110. }