Exemple #1
0
 /**
  * @param ContainerInterface $container
  * @return Queue
  * @throws Exception\InvalidArgumentException
  */
 public function __invoke(ContainerInterface $container) : Queue
 {
     $options = $this->options($container->get('config'), $this->queueName);
     if (null === $this->channel) {
         $connection = $container->get($options['connection']);
         $this->channel = $connection->newChannel();
     }
     $queue = $this->channel->newQueue();
     if ('' !== $options['name']) {
         $queue->setName($options['name']);
     }
     $queue->setFlags($this->getFlags($options));
     $queue->setArguments($options['arguments']);
     if ($this->autoSetupFabric || $options['auto_setup_fabric']) {
         // auto setup fabric depended exchange
         if (isset($options['arguments']['x-dead-letter-exchange'])) {
             // auto setup fabric dead letter exchange
             $exchangeName = $options['arguments']['x-dead-letter-exchange'];
             ExchangeFactory::$exchangeName($container, $this->channel, true);
         }
         $exchanges = $options['exchanges'];
         if ($exchanges instanceof \Traversable) {
             $exchanges = iterator_to_array($exchanges);
         }
         if (!is_array($exchanges) || empty($exchanges)) {
             throw new Exception\InvalidArgumentException('Expected an array or traversable of exchanges');
         }
         foreach ($exchanges as $exchange => $exchangeOptions) {
             ExchangeFactory::$exchange($container, $this->channel, true);
         }
         $queue->declareQueue();
         foreach ($exchanges as $exchange => $exchangeOptions) {
             if (empty($exchangeOptions)) {
                 $this->bindQueue($queue, $exchange, [], []);
             } else {
                 foreach ($exchangeOptions as $exchangeOption) {
                     $routingKeys = $exchangeOption['routing_keys'] ?? [];
                     $bindArguments = $exchangeOption['bind_arguments'] ?? [];
                     $this->bindQueue($queue, $exchange, $routingKeys, $bindArguments);
                 }
             }
         }
     }
     return $queue;
 }
Exemple #2
0
 /**
  * @param ContainerInterface $container
  * @return Exchange
  * @throws Exception\InvalidArgumentException
  */
 public function __invoke(ContainerInterface $container) : Exchange
 {
     $config = $container->get('config');
     $options = $this->options($config, $this->exchangeName);
     if (null === $this->channel) {
         $connection = $container->get($options['connection']);
         $this->channel = $connection->newChannel();
     }
     $exchange = $this->channel->newExchange();
     $exchange->setArguments($options['arguments']);
     $exchange->setName($options['name']);
     $exchange->setFlags($this->getFlags($options));
     $exchange->setType($options['type']);
     if ($this->autoSetupFabric || $options['auto_setup_fabric']) {
         if (isset($options['arguments']['alternate-exchange'])) {
             // auto setup fabric alternate exchange
             $exchangeName = $options['arguments']['alternate-exchange'];
             ExchangeFactory::$exchangeName($container, $this->channel, true);
         }
         $exchange->declareExchange();
         // rabbitmq extension: exchange to exchange bindings
         foreach ($options['exchange_bindings'] as $exchangeName => $bindOptions) {
             ExchangeFactory::$exchangeName($container, $this->channel, true);
             if (empty($bindOptions)) {
                 $this->bindExchange($exchange, $exchangeName, [], []);
             } else {
                 foreach ($bindOptions as $bindOption) {
                     $routingKeys = $bindOption['routing_keys'] ?? [];
                     $bindArguments = $bindOption['arguments'] ?? [];
                     $this->bindExchange($exchange, $exchangeName, $routingKeys, $bindArguments);
                 }
             }
         }
     }
     return $exchange;
 }