1
0

SingleCommandApplication.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. private $version = 'UNKNOWN';
  20. private $running = false;
  21. public function setVersion(string $version): self
  22. {
  23. $this->version = $version;
  24. return $this;
  25. }
  26. public function run(InputInterface $input = null, OutputInterface $output = null): int
  27. {
  28. if ($this->running) {
  29. return parent::run($input, $output);
  30. }
  31. // We use the command name as the application name
  32. $application = new Application($this->getName() ?: 'UNKNOWN', $this->version);
  33. // Fix the usage of the command displayed with "--help"
  34. $this->setName($_SERVER['argv'][0]);
  35. $application->add($this);
  36. $application->setDefaultCommand($this->getName(), true);
  37. $this->running = true;
  38. try {
  39. $ret = $application->run($input, $output);
  40. } finally {
  41. $this->running = false;
  42. }
  43. return $ret ?? 1;
  44. }
  45. }