CommandConsumer.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace WorkflowBundle\Services;
  3. use OldSound\RabbitMqBundle\RabbitMq\ConsumerInterface;
  4. use PhpAmqpLib\Message\AMQPMessage;
  5. use Symfony\Component\DependencyInjection\ContainerInterface;
  6. use Symfony\Component\Process\Process;
  7. class CommandConsumer implements ConsumerInterface
  8. {
  9. /**
  10. * @var ContainerInterface
  11. */
  12. protected $serviceContainer;
  13. /**
  14. * @param ContainerInterface $serviceContainer
  15. */
  16. public function __construct(ContainerInterface $serviceContainer)
  17. {
  18. $this->serviceContainer = $serviceContainer;
  19. }
  20. /**
  21. * $msg will be an instance of `PhpAmqpLib\Message\AMQPMessage`
  22. * with the $msg->body being the data sent over RabbitMQ.
  23. *
  24. * @param AMQPMessage $msg
  25. */
  26. public function execute(AMQPMessage $msg)
  27. {
  28. $msgBody = unserialize($msg->getBody());
  29. if (isset($msgBody['name']) && isset($msgBody['args'])) {
  30. // command name and args
  31. $name = $msgBody['name'];
  32. $args = $msgBody['args'];
  33. $input = array();
  34. foreach ($args as $arg) {
  35. $arg = str_replace('\\', '\\\\', $arg);
  36. $pos = strpos($arg, ':');
  37. $pos2 = strpos($arg, '--');
  38. if ($pos2 === false) {
  39. // command argument, no option
  40. $input[] = substr($arg, $pos + 1);
  41. } else {
  42. // reemplazo el primer : por = para las options
  43. $input[] = preg_replace('/:/', '=', $arg, 1);
  44. }
  45. }
  46. $consolePath = $this->serviceContainer->get('kernel')->getRootDir() . '/../bin/console';
  47. $command = $name . ' ' . implode(' ', $input);
  48. $process = new Process("{$consolePath} {$command}");
  49. $process->run();
  50. $content = $process->getOutput();
  51. echo $content;
  52. return true;
  53. }
  54. return false;
  55. }
  56. }