BuildDebugContainerTrait.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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\Bundle\FrameworkBundle\Command;
  11. use Symfony\Component\Config\ConfigCache;
  12. use Symfony\Component\Config\FileLocator;
  13. use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
  14. use Symfony\Component\DependencyInjection\ContainerBuilder;
  15. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  16. /**
  17. * @internal
  18. *
  19. * @author Robin Chalas <robin.chalas@gmail.com>
  20. * @author Nicolas Grekas <p@tchwork.com>
  21. */
  22. trait BuildDebugContainerTrait
  23. {
  24. protected $containerBuilder;
  25. /**
  26. * Loads the ContainerBuilder from the cache.
  27. *
  28. * @throws \LogicException
  29. */
  30. protected function getContainerBuilder(): ContainerBuilder
  31. {
  32. if ($this->containerBuilder) {
  33. return $this->containerBuilder;
  34. }
  35. $kernel = $this->getApplication()->getKernel();
  36. if (!$kernel->isDebug() || !(new ConfigCache($kernel->getContainer()->getParameter('debug.container.dump'), true))->isFresh()) {
  37. $buildContainer = \Closure::bind(function () { return $this->buildContainer(); }, $kernel, \get_class($kernel));
  38. $container = $buildContainer();
  39. $container->getCompilerPassConfig()->setRemovingPasses([]);
  40. $container->getCompilerPassConfig()->setAfterRemovingPasses([]);
  41. $container->compile();
  42. } else {
  43. (new XmlFileLoader($container = new ContainerBuilder(), new FileLocator()))->load($kernel->getContainer()->getParameter('debug.container.dump'));
  44. $locatorPass = new ServiceLocatorTagPass();
  45. $locatorPass->process($container);
  46. }
  47. return $this->containerBuilder = $container;
  48. }
  49. }