Application.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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\Console;
  11. use Symfony\Component\Console\Application as BaseApplication;
  12. use Symfony\Component\Console\Command\Command;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Input\InputOption;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. use Symfony\Component\DependencyInjection\ContainerAwareInterface;
  17. use Symfony\Component\HttpKernel\Bundle\Bundle;
  18. use Symfony\Component\HttpKernel\Kernel;
  19. use Symfony\Component\HttpKernel\KernelInterface;
  20. /**
  21. * @author Fabien Potencier <fabien@symfony.com>
  22. */
  23. class Application extends BaseApplication
  24. {
  25. private $kernel;
  26. private $commandsRegistered = false;
  27. public function __construct(KernelInterface $kernel)
  28. {
  29. $this->kernel = $kernel;
  30. parent::__construct('Symfony', Kernel::VERSION.' - '.$kernel->getName().'/'.$kernel->getEnvironment().($kernel->isDebug() ? '/debug' : ''));
  31. $inputDefinition = $this->getDefinition();
  32. $inputDefinition->addOption(new InputOption('--shell', '-s', InputOption::VALUE_NONE, 'Launch the shell.'));
  33. $inputDefinition->addOption(new InputOption('--process-isolation', null, InputOption::VALUE_NONE, 'Launch commands from shell as a separate process.'));
  34. $inputDefinition->addOption(new InputOption('--env', '-e', InputOption::VALUE_REQUIRED, 'The Environment name.', $kernel->getEnvironment()));
  35. $inputDefinition->addOption(new InputOption('--no-debug', null, InputOption::VALUE_NONE, 'Switches off debug mode.'));
  36. }
  37. /**
  38. * Gets the Kernel associated with this Console.
  39. *
  40. * @return KernelInterface A KernelInterface instance
  41. */
  42. public function getKernel()
  43. {
  44. return $this->kernel;
  45. }
  46. /**
  47. * Runs the current application.
  48. *
  49. * @return int 0 if everything went fine, or an error code
  50. */
  51. public function doRun(InputInterface $input, OutputInterface $output)
  52. {
  53. $this->kernel->boot();
  54. $container = $this->kernel->getContainer();
  55. foreach ($this->all() as $command) {
  56. if ($command instanceof ContainerAwareInterface) {
  57. $command->setContainer($container);
  58. }
  59. }
  60. $this->setDispatcher($container->get('event_dispatcher'));
  61. if (true === $input->hasParameterOption(array('--shell', '-s'))) {
  62. @trigger_error('The "--shell" option is deprecated since Symfony 2.8 and will be removed in 3.0.', E_USER_DEPRECATED);
  63. $shell = new Shell($this);
  64. $shell->setProcessIsolation($input->hasParameterOption(array('--process-isolation')));
  65. $shell->run();
  66. return 0;
  67. }
  68. return parent::doRun($input, $output);
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function find($name)
  74. {
  75. $this->registerCommands();
  76. return parent::find($name);
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function get($name)
  82. {
  83. $this->registerCommands();
  84. return parent::get($name);
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function all($namespace = null)
  90. {
  91. $this->registerCommands();
  92. return parent::all($namespace);
  93. }
  94. /**
  95. * {@inheritdoc}
  96. */
  97. public function add(Command $command)
  98. {
  99. $this->registerCommands();
  100. return parent::add($command);
  101. }
  102. protected function registerCommands()
  103. {
  104. if ($this->commandsRegistered) {
  105. return;
  106. }
  107. $this->commandsRegistered = true;
  108. $this->kernel->boot();
  109. $container = $this->kernel->getContainer();
  110. foreach ($this->kernel->getBundles() as $bundle) {
  111. if ($bundle instanceof Bundle) {
  112. $bundle->registerCommands($this);
  113. }
  114. }
  115. if ($container->hasParameter('console.command.ids')) {
  116. foreach ($container->getParameter('console.command.ids') as $id) {
  117. $this->add($container->get($id));
  118. }
  119. }
  120. }
  121. }