Compiler.php 2.6 KB

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