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