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