1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Jellyfish\QueueRabbitMq; |
6
|
|
|
|
7
|
|
|
use PhpAmqpLib\Connection\AbstractConnection; |
8
|
|
|
use PhpAmqpLib\Connection\AMQPLazyConnection; |
9
|
|
|
use Pimple\Container; |
10
|
|
|
use Pimple\ServiceProviderInterface; |
11
|
|
|
|
12
|
|
|
class QueueRabbitMqServiceProvider implements ServiceProviderInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @param \Pimple\Container $pimple |
16
|
|
|
* |
17
|
|
|
* @return void |
18
|
|
|
*/ |
19
|
|
|
public function register(Container $pimple): void |
20
|
|
|
{ |
21
|
|
|
$this->registerQueueClient($pimple); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param \Pimple\Container $container |
26
|
|
|
* |
27
|
|
|
* @return \Jellyfish\QueueRabbitMq\QueueRabbitMqServiceProvider |
28
|
|
|
*/ |
29
|
|
|
protected function registerQueueClient(Container $container): QueueRabbitMqServiceProvider |
30
|
|
|
{ |
31
|
|
|
$self = $this; |
32
|
|
|
|
33
|
|
|
$container->offsetSet('queue_client', static function (Container $container) use ($self) { |
34
|
|
|
return new QueueClient( |
35
|
|
|
$self->createConnection($container), |
36
|
|
|
$self->createAmqpMessageFactory(), |
37
|
|
|
$container->offsetGet('message_mapper') |
38
|
|
|
); |
39
|
|
|
}); |
40
|
|
|
|
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param \Pimple\Container $container |
46
|
|
|
* |
47
|
|
|
* @return \PhpAmqpLib\Connection\AbstractConnection |
48
|
|
|
*/ |
49
|
|
|
protected function createConnection(Container $container): AbstractConnection |
50
|
|
|
{ |
51
|
|
|
$config = $container->offsetGet('config'); |
52
|
|
|
|
53
|
|
|
$rabbitMqHost = $config->get( |
54
|
|
|
QueueRabbitMqConstants::RABBIT_MQ_HOST, |
55
|
|
|
QueueRabbitMqConstants::DEFAULT_RABBIT_MQ_HOST |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
$rabbitMqPort = $config->get( |
59
|
|
|
QueueRabbitMqConstants::RABBIT_MQ_PORT, |
60
|
|
|
QueueRabbitMqConstants::DEFAULT_RABBIT_MQ_PORT |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
$rabbitMqUser = $config->get( |
64
|
|
|
QueueRabbitMqConstants::RABBIT_MQ_USER, |
65
|
|
|
QueueRabbitMqConstants::DEFAULT_RABBIT_MQ_USER |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
$rabbitMqPassword = $config->get( |
69
|
|
|
QueueRabbitMqConstants::RABBIT_MQ_PASSWORD, |
70
|
|
|
QueueRabbitMqConstants::DEFAULT_RABBIT_MQ_PASSWORD |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
$rabbitMqVhost = $config->get( |
74
|
|
|
QueueRabbitMqConstants::RABBIT_MQ_VHOST, |
75
|
|
|
QueueRabbitMqConstants::DEFAULT_RABBIT_MQ_VHOST |
76
|
|
|
); |
77
|
|
|
|
78
|
|
|
return new AMQPLazyConnection( |
79
|
|
|
$rabbitMqHost, |
80
|
|
|
$rabbitMqPort, |
81
|
|
|
$rabbitMqUser, |
82
|
|
|
$rabbitMqPassword, |
83
|
|
|
$rabbitMqVhost |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return \Jellyfish\QueueRabbitMq\AmqpMessageFactoryInterface |
89
|
|
|
*/ |
90
|
|
|
protected function createAmqpMessageFactory(): AmqpMessageFactoryInterface |
91
|
|
|
{ |
92
|
|
|
return new AmqpMessageFactory(); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|