SingleCommandApplication.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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\Console;
  11. use Symfony\Component\Console\Command\Command;
  12. use Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Output\OutputInterface;
  14. /**
  15. * @author Grégoire Pineau <lyrixx@lyrixx.info>
  16. */
  17. class SingleCommandApplication extends Command
  18. {
  19. /**
  20. * @var string
  21. */
  22. private $version = 'UNKNOWN';
  23. /**
  24. * @var bool
  25. */
  26. private $autoExit = true;
  27. /**
  28. * @var bool
  29. */
  30. private $running = false;
  31. /**
  32. * @return $this
  33. * @param string $version
  34. */
  35. public function setVersion($version)
  36. {
  37. $this->version = $version;
  38. return $this;
  39. }
  40. /**
  41. * @final
  42. *
  43. * @return $this
  44. * @param bool $autoExit
  45. */
  46. public function setAutoExit($autoExit)
  47. {
  48. $this->autoExit = $autoExit;
  49. return $this;
  50. }
  51. /**
  52. * @param \Symfony\Component\Console\Input\InputInterface|null $input
  53. * @param \Symfony\Component\Console\Output\OutputInterface|null $output
  54. */
  55. public function run($input = null, $output = null)
  56. {
  57. if ($this->running) {
  58. return parent::run($input, $output);
  59. }
  60. // We use the command name as the application name
  61. $application = new Application($this->getName() ?: 'UNKNOWN', $this->version);
  62. $application->setAutoExit($this->autoExit);
  63. // Fix the usage of the command displayed with "--help"
  64. $this->setName($_SERVER['argv'][0]);
  65. $application->add($this);
  66. $application->setDefaultCommand($this->getName(), true);
  67. $this->running = true;
  68. try {
  69. $ret = $application->run($input, $output);
  70. } finally {
  71. $this->running = false;
  72. }
  73. return $ret ?? 1;
  74. }
  75. }