AppCommand.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace App\Command;
  3. use Osinet\DemoBundle\Command\DemoCommand;
  4. use Osinet\DemoBundle\Lucky;
  5. use Symfony\Component\Console\Command\Command;
  6. use Symfony\Component\Console\Input\ArrayInput;
  7. use Symfony\Component\Console\Input\InputInterface;
  8. use Symfony\Component\Console\Input\InputOption;
  9. use Symfony\Component\Console\Output\NullOutput;
  10. use Symfony\Component\Console\Output\OutputInterface;
  11. class AppCommand extends Command {
  12. const O_DELEGATE = 'delegate';
  13. /**
  14. * @var \Osinet\DemoBundle\Lucky
  15. */
  16. protected $lucky;
  17. public function __construct(Lucky $lucky, ?string $name = NULL) {
  18. $this->lucky = $lucky;
  19. parent::__construct($name);
  20. }
  21. /**
  22. * {@inheritdoc}
  23. *
  24. * Nothing in parent, so don't call it.
  25. */
  26. protected function configure() {
  27. $this->setName('app:lucky')
  28. ->setDescription('Draws a lucky number')
  29. ->setHelp('Draws a pseudo-random number, or returns the one you pass')
  30. ->addOption(static::O_DELEGATE, 'd', InputOption::VALUE_NONE, 'Delegate to demo bundle command');
  31. }
  32. protected function execute(InputInterface $input, OutputInterface $output) {
  33. // Delegate to bundle commande.
  34. if ($input->getOption(static::O_DELEGATE)) {
  35. $bundleCommand = $this->getApplication()->find(DemoCommand::NAME);
  36. $arguments = [];
  37. $commandInput = new ArrayInput($arguments);
  38. $commandOutput = new NullOutput();
  39. // Pass no options/arguments, mask commmand output.
  40. $value = $bundleCommand->run($commandInput, $commandOutput);
  41. }
  42. // Perform value selection directly.
  43. else {
  44. $value = $this->lucky->number();
  45. }
  46. $output->writeln($value);
  47. }
  48. }