Compiler.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace Symfony\Component\DependencyInjection\Compiler;
  3. use Symfony\Component\DependencyInjection\ContainerBuilder;
  4. use Symfony\Component\DependencyInjection\Compiler\PassConfig;
  5. /**
  6. * This class is used to remove circular dependencies between individual passes.
  7. *
  8. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  9. */
  10. class Compiler
  11. {
  12. protected $passConfig;
  13. protected $currentPass;
  14. protected $currentStartTime;
  15. protected $log;
  16. protected $serviceReferenceGraph;
  17. public function __construct()
  18. {
  19. $this->passConfig = new PassConfig();
  20. $this->serviceReferenceGraph = new ServiceReferenceGraph();
  21. $this->log = array();
  22. }
  23. public function getPassConfig()
  24. {
  25. return $this->passConfig;
  26. }
  27. public function getServiceReferenceGraph()
  28. {
  29. return $this->serviceReferenceGraph;
  30. }
  31. public function addPass(CompilerPassInterface $pass, $type = PassConfig::TYPE_BEFORE_OPTIMIZATION)
  32. {
  33. $this->passConfig->addPass($pass, $type);
  34. }
  35. public function addLogMessage($string)
  36. {
  37. $this->log[] = $string;
  38. }
  39. public function getLog()
  40. {
  41. return $this->log;
  42. }
  43. public function compile(ContainerBuilder $container)
  44. {
  45. foreach ($this->passConfig->getPasses() as $pass) {
  46. $this->startPass($pass);
  47. $pass->process($container);
  48. $this->endPass($pass);
  49. }
  50. }
  51. protected function startPass(CompilerPassInterface $pass)
  52. {
  53. if ($pass instanceof CompilerAwareInterface) {
  54. $pass->setCompiler($this);
  55. }
  56. $this->currentPass = $pass;
  57. $this->currentStartTime = microtime(true);
  58. }
  59. protected function endPass(CompilerPassInterface $pass)
  60. {
  61. $this->currentPass = null;
  62. $this->addLogMessage(sprintf('%s finished in %.3fs', get_class($pass), microtime(true) - $this->currentStartTime));
  63. }
  64. }