eMAGTechLabs /
RabbitMqBundle
| 1 | <?php |
||
| 2 | |||
| 3 | namespace OldSound\RabbitMqBundle\Command; |
||
| 4 | |||
| 5 | use OldSound\RabbitMqBundle\RabbitMq\Producer; |
||
| 6 | use Symfony\Component\Console\Command\Command; |
||
| 7 | use Symfony\Component\Console\Exception\InvalidArgumentException; |
||
| 8 | use Symfony\Component\Console\Input\InputOption; |
||
| 9 | use Symfony\Component\Console\Input\InputArgument; |
||
| 10 | use Symfony\Component\Console\Input\InputInterface; |
||
| 11 | use Symfony\Component\Console\Output\OutputInterface; |
||
| 12 | use Symfony\Component\DependencyInjection\ContainerAwareTrait; |
||
| 13 | |||
| 14 | class StdInProducerCommand extends Command |
||
| 15 | { |
||
| 16 | use ContainerAwareTrait; |
||
| 17 | |||
| 18 | const FORMAT_PHP = 'php'; |
||
| 19 | const FORMAT_RAW = 'raw'; |
||
| 20 | |||
| 21 | protected function configure() |
||
| 22 | { |
||
| 23 | parent::configure(); |
||
| 24 | |||
| 25 | $this |
||
| 26 | ->setName('rabbitmq:stdin-producer') |
||
| 27 | ->addArgument('name', InputArgument::REQUIRED, 'Producer Name') |
||
| 28 | ->setDescription('Executes a producer that reads data from STDIN') |
||
| 29 | ->addOption('route', 'r', InputOption::VALUE_OPTIONAL, 'Routing Key', '') |
||
| 30 | ->addOption('format', 'f', InputOption::VALUE_OPTIONAL, 'Payload Format', self::FORMAT_PHP) |
||
| 31 | ->addOption('debug', 'd', InputOption::VALUE_OPTIONAL, 'Enable Debugging', false) |
||
| 32 | ; |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Executes the current command. |
||
| 37 | * |
||
| 38 | * @param InputInterface $input An InputInterface instance |
||
| 39 | * @param OutputInterface $output An OutputInterface instance |
||
| 40 | * |
||
| 41 | * @return integer 0 if everything went fine, or an error code |
||
| 42 | */ |
||
| 43 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 44 | { |
||
| 45 | define('AMQP_DEBUG', (bool) $input->getOption('debug')); |
||
| 46 | |||
| 47 | $producerName = $input->getArgument('name'); |
||
| 48 | $alias = sprintf('old_sound_rabbit_mq.producer.%s', $producerName); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 49 | if (!$this->container->has($alias)) { |
||
| 50 | $producerNames = []; // TODO $this->container->getParameter('old_sound_rabbit_mq.allowed_producer_names'); |
||
| 51 | throw new InvalidArgumentException(sprintf('Producer %s is undefined. Allowed ones: %s', $producerName, join(', ', $producerNames))); |
||
| 52 | } |
||
| 53 | |||
| 54 | /** @var Producer $producer */ |
||
| 55 | $producer = $this->container->get($alias); |
||
| 56 | |||
| 57 | $data = ''; |
||
| 58 | while (!feof(STDIN)) { |
||
| 59 | $data .= fread(STDIN, 8192); |
||
| 60 | } |
||
| 61 | |||
| 62 | $route = $input->getOption('route'); |
||
| 63 | $format = $input->getOption('format'); |
||
| 64 | |||
| 65 | switch ($format) { |
||
| 66 | case self::FORMAT_RAW: |
||
| 67 | break; // data as is |
||
| 68 | case self::FORMAT_PHP: |
||
| 69 | $data = serialize($data); |
||
| 70 | break; |
||
| 71 | default: |
||
| 72 | throw new \InvalidArgumentException(sprintf('Invalid payload format "%s", expecting one of: %s.', |
||
| 73 | $format, implode(', ', array(self::FORMAT_PHP, self::FORMAT_RAW)))); |
||
| 74 | } |
||
| 75 | |||
| 76 | $producer->publish($data, $route); |
||
| 77 | |||
| 78 | return 0; |
||
| 79 | } |
||
| 80 | } |
||
| 81 |