1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- <?php
- namespace WorkflowBundle\Services;
- use OldSound\RabbitMqBundle\RabbitMq\ConsumerInterface;
- use PhpAmqpLib\Message\AMQPMessage;
- use Symfony\Component\DependencyInjection\ContainerInterface;
- use Symfony\Component\Process\Process;
- class CommandConsumer implements ConsumerInterface
- {
- /**
- * @var ContainerInterface
- */
- protected $serviceContainer;
- /**
- * @param ContainerInterface $serviceContainer
- */
- public function __construct(ContainerInterface $serviceContainer)
- {
- $this->serviceContainer = $serviceContainer;
- }
- /**
- * $msg will be an instance of `PhpAmqpLib\Message\AMQPMessage`
- * with the $msg->body being the data sent over RabbitMQ.
- *
- * @param AMQPMessage $msg
- */
- public function execute(AMQPMessage $msg)
- {
- $msgBody = unserialize($msg->getBody());
- if (isset($msgBody['name']) && isset($msgBody['args'])) {
- // command name and args
- $name = $msgBody['name'];
- $args = $msgBody['args'];
- $input = array();
- foreach ($args as $arg) {
- $arg = str_replace('\\', '\\\\', $arg);
- $pos = strpos($arg, ':');
- $pos2 = strpos($arg, '--');
- if ($pos2 === false) {
- // command argument, no option
- $input[] = substr($arg, $pos + 1);
- } else {
- // reemplazo el primer : por = para las options
- $input[] = preg_replace('/:/', '=', $arg, 1);
- }
- }
-
- $consolePath = $this->serviceContainer->get('kernel')->getRootDir() . '/../bin/console';
- $command = $name . ' ' . implode(' ', $input);
- $process = new Process("{$consolePath} {$command}");
- $process->run();
- $content = $process->getOutput();
- echo $content;
-
- return true;
- }
- return false;
- }
- }
|