|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Enqueue\Client\Driver; |
| 4 | + |
| 5 | +use Enqueue\SnsQs\SnsQsContext; |
| 6 | +use Enqueue\SnsQs\SnsQsTopic; |
| 7 | +use Interop\Queue\Destination; |
| 8 | +use Psr\Log\LoggerInterface; |
| 9 | +use Psr\Log\NullLogger; |
| 10 | + |
| 11 | +/** |
| 12 | + * @method SnsQsContext getContext() |
| 13 | + * @method SnsQsTopic createRouterTopic() |
| 14 | + */ |
| 15 | +class SnsQsDriver extends GenericDriver |
| 16 | +{ |
| 17 | + public function __construct(SnsQsContext $context, ...$args) |
| 18 | + { |
| 19 | + parent::__construct($context, ...$args); |
| 20 | + } |
| 21 | + |
| 22 | + public function setupBroker(LoggerInterface $logger = null): void |
| 23 | + { |
| 24 | + $logger = $logger ?: new NullLogger(); |
| 25 | + $log = function ($text, ...$args) use ($logger) { |
| 26 | + $logger->debug(sprintf('[SqsQsDriver] '.$text, ...$args)); |
| 27 | + }; |
| 28 | + |
| 29 | + // setup router |
| 30 | + $routerTopic = $this->createRouterTopic(); |
| 31 | + $log('Declare router topic: %s', $routerTopic->getTopicName()); |
| 32 | + $this->getContext()->declareTopic($routerTopic); |
| 33 | + |
| 34 | + $routerQueue = $this->createQueue($this->getConfig()->getRouterQueue()); |
| 35 | + $log('Declare router queue: %s', $routerQueue->getQueueName()); |
| 36 | + $this->getContext()->declareQueue($routerQueue); |
| 37 | + |
| 38 | + $log('Bind router queue to topic: %s -> %s', $routerQueue->getQueueName(), $routerTopic->getTopicName()); |
| 39 | + $this->getContext()->bind($routerTopic, $routerQueue); |
| 40 | + |
| 41 | + // setup queues |
| 42 | + $declaredQueues = []; |
| 43 | + $declaredTopics = []; |
| 44 | + foreach ($this->getRouteCollection()->all() as $route) { |
| 45 | + $queue = $this->createRouteQueue($route); |
| 46 | + if (false === array_key_exists($queue->getQueueName(), $declaredQueues)) { |
| 47 | + $log('Declare processor queue: %s', $queue->getQueueName()); |
| 48 | + $this->getContext()->declareQueue($queue); |
| 49 | + |
| 50 | + $declaredQueues[$queue->getQueueName()] = true; |
| 51 | + } |
| 52 | + |
| 53 | + if ($route->isCommand()) { |
| 54 | + continue; |
| 55 | + } |
| 56 | + |
| 57 | + $topic = $this->doCreateTopic($this->createTransportQueueName($route->getSource(), true)); |
| 58 | + if (false === array_key_exists($topic->getTopicName(), $declaredTopics)) { |
| 59 | + $log('Declare processor topic: %s', $topic->getTopicName()); |
| 60 | + $this->getContext()->declareTopic($topic); |
| 61 | + |
| 62 | + $declaredTopics[$topic->getTopicName()] = true; |
| 63 | + } |
| 64 | + |
| 65 | + $log('Bind processor queue to topic: %s -> %s', $queue->getQueueName(), $topic->getTopicName()); |
| 66 | + $this->getContext()->bind($topic, $queue); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + protected function createRouterTopic(): Destination |
| 71 | + { |
| 72 | + return $this->doCreateTopic( |
| 73 | + $this->createTransportRouterTopicName($this->getConfig()->getRouterTopic(), true) |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + protected function createTransportRouterTopicName(string $name, bool $prefix): string |
| 78 | + { |
| 79 | + $name = parent::createTransportRouterTopicName($name, $prefix); |
| 80 | + |
| 81 | + return str_replace('.', '_dot_', $name); |
| 82 | + } |
| 83 | + |
| 84 | + protected function createTransportQueueName(string $name, bool $prefix): string |
| 85 | + { |
| 86 | + $name = parent::createTransportQueueName($name, $prefix); |
| 87 | + |
| 88 | + return str_replace('.', '_dot_', $name); |
| 89 | + } |
| 90 | +} |
0 commit comments