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