123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- <?php
- namespace WorkflowBundle\Services;
- use OldSound\RabbitMqBundle\RabbitMq\ConsumerInterface;
- use PhpAmqpLib\Message\AMQPMessage;
- use Symfony\Bundle\FrameworkBundle\Console\Application;
- use Symfony\Component\DependencyInjection\ContainerInterface;
- use Symfony\Component\Console\Input\ArrayInput;
- use Symfony\Component\Console\Output\BufferedOutput;
- 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->body);
- if (isset($msgBody['name']) && isset($msgBody['args'])) {
- // command name and args
- $name = $msgBody['name'];
- $args = $msgBody['args'];
- $kernel = $this->serviceContainer->get('kernel');
- $application = new Application($kernel);
- $application->setAutoExit(false);
- $input = array('command' => $name,);
- foreach ($args as $arg) {
- $pieces = explode(':', $arg);
- if (isset($pieces[0]) && isset($pieces[1])) {
- $input[$pieces[0]] = $pieces[1];
- }
- }
-
- $input = new ArrayInput($input);
- $output = new BufferedOutput();
- $application->run($input, $output);
- // return the output
- $content = $output->fetch();
-
- echo $content;
- return true;
- }
- return false;
- }
- }
|