AMQPRemoteCommand.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. namespace WorkflowBundle\Command;
  3. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  4. use Symfony\Component\Console\Input\InputInterface;
  5. use Symfony\Component\Console\Input\InputOption;
  6. use Symfony\Component\Console\Output\OutputInterface;
  7. class AMQPRemoteCommand extends ContainerAwareCommand
  8. {
  9. protected function configure()
  10. {
  11. $this
  12. ->setName('amqp:remote')
  13. ->setDescription('Run a command on an AMQP Consumer')
  14. ->setHelp('Run a command on an AMQP Consumer')
  15. ->addArgument('name', InputOption::VALUE_REQUIRED, 'Command to execute')
  16. ->addOption('args', null, InputOption::VALUE_OPTIONAL|InputOption::VALUE_IS_ARRAY, 'Optional Commands arguments. e.g. --args=key:value --args=key1:value1')
  17. ->addOption('route', null, InputOption::VALUE_OPTIONAL, 'AMQP Route Key', '')
  18. ;
  19. }
  20. /**
  21. * @param InputInterface $input
  22. * @param OutputInterface $output
  23. */
  24. protected function execute(InputInterface $input, OutputInterface $output)
  25. {
  26. $name = $input->getArgument('name');
  27. $args = $input->getOption('args');
  28. $date = date("Y-m-d H:i:s");
  29. if ($name) {
  30. $producer = $this->getContainer()->get('old_sound_rabbit_mq.command_producer_producer');
  31. $route = $input->getOption('route');
  32. $producer->publish(serialize(compact('name', 'args')), $route);
  33. $output->writeln("<info>Command executed:</info> {$date} # {$name} # ".implode(" ",$args));
  34. } else {
  35. $output->writeln("<error>Enter a valid command:</error> {$date} # {$name} # ".implode(" ",$args));
  36. }
  37. }
  38. }