ContainerAwareCommand.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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\Console\Command\Command;
  12. use Symfony\Component\DependencyInjection\ContainerAwareInterface;
  13. use Symfony\Component\DependencyInjection\ContainerInterface;
  14. /**
  15. * Command.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. */
  19. abstract class ContainerAwareCommand extends Command implements ContainerAwareInterface
  20. {
  21. /**
  22. * @var ContainerInterface|null
  23. */
  24. private $container;
  25. /**
  26. * @return ContainerInterface
  27. *
  28. * @throws \LogicException
  29. */
  30. protected function getContainer()
  31. {
  32. if (null === $this->container) {
  33. $application = $this->getApplication();
  34. if (null === $application) {
  35. throw new \LogicException('The container cannot be retrieved as the application instance is not yet set.');
  36. }
  37. $this->container = $application->getKernel()->getContainer();
  38. }
  39. return $this->container;
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function setContainer(ContainerInterface $container = null)
  45. {
  46. $this->container = $container;
  47. }
  48. }