1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Jellyfish\QueueRabbitMq; |
6
|
|
|
|
7
|
|
|
use Jellyfish\Queue\DestinationInterface; |
8
|
|
|
use Jellyfish\Queue\QueueConstants; |
9
|
|
|
use Jellyfish\Queue\QueueServiceProvider; |
10
|
|
|
use PhpAmqpLib\Connection\AMQPLazyConnection; |
11
|
|
|
use Pimple\Container; |
12
|
|
|
use Pimple\ServiceProviderInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
16
|
|
|
*/ |
17
|
|
|
class QueueRabbitMqServiceProvider implements ServiceProviderInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @param \Pimple\Container $container |
21
|
|
|
* |
22
|
|
|
* @return void |
23
|
|
|
*/ |
24
|
|
|
public function register(Container $container): void |
25
|
|
|
{ |
26
|
|
|
$this->registerConnection($container) |
27
|
|
|
->registerAmqpMessageFactory($container) |
28
|
|
|
->registerQueueClient($container); |
29
|
|
|
} |
30
|
|
|
/** |
31
|
|
|
* @param \Pimple\Container $container |
32
|
|
|
* |
33
|
|
|
* @return \Jellyfish\QueueRabbitMq\QueueRabbitMqServiceProvider |
34
|
|
|
*/ |
35
|
|
|
protected function registerConnection(Container $container): QueueRabbitMqServiceProvider |
36
|
|
|
{ |
37
|
|
|
$self = $this; |
38
|
|
|
|
39
|
|
|
$container->offsetSet( |
40
|
|
|
QueueRabbitMqConstants::CONTAINER_KEY_CONNECTION, |
41
|
|
|
static function (Container $container) use ($self) { |
42
|
|
|
$lazyConnection = $self->createAmqpLazyConnection($container); |
43
|
|
|
|
44
|
|
|
return new Connection($lazyConnection); |
45
|
|
|
} |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param \Pimple\Container $container |
53
|
|
|
* |
54
|
|
|
* @return \Jellyfish\QueueRabbitMq\QueueRabbitMqServiceProvider |
55
|
|
|
*/ |
56
|
|
|
protected function registerAmqpMessageFactory(Container $container): QueueRabbitMqServiceProvider |
57
|
|
|
{ |
58
|
|
|
$container->offsetSet(QueueRabbitMqConstants::CONTAINER_KEY_AMQP_MESSAGE_FACTORY, static function () { |
59
|
|
|
return new AmqpMessageFactory(); |
60
|
|
|
}); |
61
|
|
|
|
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param \Pimple\Container $container |
67
|
|
|
* |
68
|
|
|
* @return \Jellyfish\QueueRabbitMq\QueueRabbitMqServiceProvider |
69
|
|
|
*/ |
70
|
|
|
protected function registerQueueClient(Container $container): QueueRabbitMqServiceProvider |
71
|
|
|
{ |
72
|
|
|
$self = $this; |
73
|
|
|
|
74
|
|
|
$container->offsetSet( |
75
|
|
|
QueueConstants::CONTAINER_KEY_QUEUE_CLIENT, |
76
|
|
|
static function (Container $container) use ($self) { |
77
|
|
|
return new QueueClient( |
78
|
|
|
$self->createConsumers($container), |
79
|
|
|
$self->createProducers($container) |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param \Pimple\Container $container |
89
|
|
|
* |
90
|
|
|
* @return \PhpAmqpLib\Connection\AMQPLazyConnection |
91
|
|
|
*/ |
92
|
|
|
protected function createAmqpLazyConnection(Container $container): AMQPLazyConnection |
93
|
|
|
{ |
94
|
|
|
$config = $container->offsetGet('config'); |
95
|
|
|
|
96
|
|
|
$rabbitMqHost = $config->get( |
97
|
|
|
QueueRabbitMqConstants::RABBIT_MQ_HOST, |
98
|
|
|
QueueRabbitMqConstants::DEFAULT_RABBIT_MQ_HOST |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
$rabbitMqPort = $config->get( |
102
|
|
|
QueueRabbitMqConstants::RABBIT_MQ_PORT, |
103
|
|
|
QueueRabbitMqConstants::DEFAULT_RABBIT_MQ_PORT |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
$rabbitMqUser = $config->get( |
107
|
|
|
QueueRabbitMqConstants::RABBIT_MQ_USER, |
108
|
|
|
QueueRabbitMqConstants::DEFAULT_RABBIT_MQ_USER |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
$rabbitMqPassword = $config->get( |
112
|
|
|
QueueRabbitMqConstants::RABBIT_MQ_PASSWORD, |
113
|
|
|
QueueRabbitMqConstants::DEFAULT_RABBIT_MQ_PASSWORD |
114
|
|
|
); |
115
|
|
|
|
116
|
|
|
$rabbitMqVhost = $config->get( |
117
|
|
|
QueueRabbitMqConstants::RABBIT_MQ_VHOST, |
118
|
|
|
QueueRabbitMqConstants::DEFAULT_RABBIT_MQ_VHOST |
119
|
|
|
); |
120
|
|
|
|
121
|
|
|
return new AMQPLazyConnection( |
122
|
|
|
$rabbitMqHost, |
123
|
|
|
$rabbitMqPort, |
124
|
|
|
$rabbitMqUser, |
125
|
|
|
$rabbitMqPassword, |
126
|
|
|
$rabbitMqVhost |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param \Pimple\Container $container |
132
|
|
|
* |
133
|
|
|
* @return \Jellyfish\Queue\ConsumerInterface[] |
134
|
|
|
*/ |
135
|
|
|
protected function createConsumers(Container $container): array |
136
|
|
|
{ |
137
|
|
|
$connection = $container->offsetGet(QueueRabbitMqConstants::CONTAINER_KEY_CONNECTION); |
138
|
|
|
$messageMapper = $container->offsetGet(QueueConstants::CONTAINER_KEY_MESSAGE_MAPPER); |
139
|
|
|
|
140
|
|
|
return [ |
141
|
|
|
DestinationInterface::TYPE_QUEUE => new QueueConsumer($connection, $messageMapper), |
142
|
|
|
DestinationInterface::TYPE_FANOUT => new FanoutConsumer($connection, $messageMapper) |
143
|
|
|
]; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param \Pimple\Container $container |
148
|
|
|
* |
149
|
|
|
* @return \Jellyfish\Queue\ProducerInterface[] |
150
|
|
|
*/ |
151
|
|
|
protected function createProducers(Container $container): array |
152
|
|
|
{ |
153
|
|
|
$connection = $container->offsetGet(QueueRabbitMqConstants::CONTAINER_KEY_CONNECTION); |
154
|
|
|
$messageMapper = $container->offsetGet(QueueConstants::CONTAINER_KEY_MESSAGE_MAPPER); |
155
|
|
|
$amqpMessageFactory = $container->offsetGet(QueueRabbitMqConstants::CONTAINER_KEY_AMQP_MESSAGE_FACTORY); |
156
|
|
|
|
157
|
|
|
return [ |
158
|
|
|
DestinationInterface::TYPE_QUEUE => new QueueProducer($connection, $messageMapper, $amqpMessageFactory), |
159
|
|
|
DestinationInterface::TYPE_FANOUT => new FanoutProducer($connection, $messageMapper, $amqpMessageFactory), |
160
|
|
|
]; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|