/** * @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; }