123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- <?php
- namespace App\Command;
- use Osinet\DemoBundle\Command\DemoCommand;
- use Osinet\DemoBundle\Lucky;
- use Symfony\Component\Console\Command\Command;
- use Symfony\Component\Console\Input\ArrayInput;
- use Symfony\Component\Console\Input\InputInterface;
- use Symfony\Component\Console\Input\InputOption;
- use Symfony\Component\Console\Output\NullOutput;
- use Symfony\Component\Console\Output\OutputInterface;
- class AppCommand extends Command {
- const O_DELEGATE = 'delegate';
- /**
- * @var \Osinet\DemoBundle\Lucky
- */
- protected $lucky;
- public function __construct(Lucky $lucky, ?string $name = NULL) {
- $this->lucky = $lucky;
- parent::__construct($name);
- }
- /**
- * {@inheritdoc}
- *
- * Nothing in parent, so don't call it.
- */
- protected function configure() {
- $this->setName('app:lucky')
- ->setDescription('Draws a lucky number')
- ->setHelp('Draws a pseudo-random number, or returns the one you pass')
- ->addOption(static::O_DELEGATE, 'd', InputOption::VALUE_NONE, 'Delegate to demo bundle command');
- }
- protected function execute(InputInterface $input, OutputInterface $output) {
- // Delegate to bundle commande.
- if ($input->getOption(static::O_DELEGATE)) {
- $bundleCommand = $this->getApplication()->find(DemoCommand::NAME);
- $arguments = [];
- $commandInput = new ArrayInput($arguments);
- $commandOutput = new NullOutput();
- // Pass no options/arguments, mask commmand output.
- $value = $bundleCommand->run($commandInput, $commandOutput);
- }
- // Perform value selection directly.
- else {
- $value = $this->lucky->number();
- }
- $output->writeln($value);
- }
- }
|