1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Xervice\RabbitMQ; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Xervice\Core\Factory\AbstractFactory; |
8
|
|
|
use Xervice\RabbitMQ\Core\Bootstrapper; |
9
|
|
|
use Xervice\RabbitMQ\Core\ConnectionProvider; |
10
|
|
|
use Xervice\RabbitMQ\Core\ConnectionProviderInterface; |
11
|
|
|
use Xervice\RabbitMQ\Core\ExchangeProvider; |
12
|
|
|
use Xervice\RabbitMQ\Core\ExchangeProviderInterface; |
13
|
|
|
use Xervice\RabbitMQ\Core\QueueProvider; |
14
|
|
|
use Xervice\RabbitMQ\Core\QueueProviderInterface; |
15
|
|
|
use Xervice\RabbitMQ\Exchange\ExchangeBuilder; |
16
|
|
|
use Xervice\RabbitMQ\Message\MessageProvider; |
17
|
|
|
use Xervice\RabbitMQ\Message\MessageProviderInterface; |
18
|
|
|
use Xervice\RabbitMQ\Queue\QueueBuilder; |
19
|
|
|
use Xervice\RabbitMQ\Worker\Consumer\Consumer; |
20
|
|
|
use Xervice\RabbitMQ\Worker\Consumer\ConsumerInterface; |
21
|
|
|
use Xervice\RabbitMQ\Worker\Listener\ListenerInterface; |
22
|
|
|
use Xervice\RabbitMQ\Worker\Worker; |
23
|
|
|
use Xervice\RabbitMQ\Worker\WorkerInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @method \Xervice\RabbitMQ\RabbitMQConfig getConfig() |
27
|
|
|
*/ |
28
|
|
|
class RabbitMQFactory extends AbstractFactory |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var ConnectionProviderInterface |
32
|
|
|
*/ |
33
|
|
|
private $connection; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var MessageProviderInterface |
37
|
|
|
*/ |
38
|
|
|
private $messageProvider; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return \Xervice\RabbitMQ\Core\BootstrapperInterface |
42
|
|
|
*/ |
43
|
|
|
public function createBootstrapper() |
44
|
|
|
{ |
45
|
|
|
return new Bootstrapper( |
46
|
|
|
$this->createExchangeBuilder(), |
47
|
|
|
$this->createQueueBuilder() |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return \Xervice\RabbitMQ\Worker\WorkerInterface |
53
|
|
|
*/ |
54
|
|
|
public function createWorker(): WorkerInterface |
55
|
|
|
{ |
56
|
|
|
return new Worker( |
57
|
|
|
$this->getListenerCollection(), |
58
|
|
|
$this->createConsumer() |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return \Xervice\RabbitMQ\Worker\Consumer\ConsumerInterface |
64
|
|
|
*/ |
65
|
|
|
public function createConsumer(): ConsumerInterface |
66
|
|
|
{ |
67
|
|
|
return new Consumer( |
68
|
|
|
$this->getConnectionProvider()->getChannel(), |
69
|
|
|
$this->getConfig()->getConsumerConfig() |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return \Xervice\RabbitMQ\Queue\QueueBuilder |
75
|
|
|
*/ |
76
|
|
|
public function createQueueBuilder() |
77
|
|
|
{ |
78
|
|
|
return new QueueBuilder( |
79
|
|
|
$this->createQueueProvider(), |
80
|
|
|
$this->getQueueCollection() |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return \Xervice\RabbitMQ\Exchange\ExchangeBuilder |
86
|
|
|
*/ |
87
|
|
|
public function createExchangeBuilder() |
88
|
|
|
{ |
89
|
|
|
return new ExchangeBuilder( |
90
|
|
|
$this->createExchangeProvider(), |
91
|
|
|
$this->getExchangeCollection() |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return \Xervice\RabbitMQ\Core\ExchangeProviderInterface |
97
|
|
|
*/ |
98
|
|
|
public function createExchangeProvider() : ExchangeProviderInterface |
99
|
|
|
{ |
100
|
|
|
return new ExchangeProvider( |
101
|
|
|
$this->getConnectionProvider()->getChannel() |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return \Xervice\RabbitMQ\Core\QueueProviderInterface |
107
|
|
|
*/ |
108
|
|
|
public function createQueueProvider() : QueueProviderInterface |
109
|
|
|
{ |
110
|
|
|
return new QueueProvider( |
111
|
|
|
$this->getConnectionProvider()->getChannel() |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return \Xervice\RabbitMQ\Message\MessageProviderInterface |
117
|
|
|
*/ |
118
|
|
|
public function createMessageProvider() : MessageProviderInterface |
119
|
|
|
{ |
120
|
|
|
return new MessageProvider( |
121
|
|
|
$this->getConnectionProvider()->getChannel() |
122
|
|
|
); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return \Xervice\RabbitMQ\Core\ConnectionProviderInterface |
127
|
|
|
*/ |
128
|
|
|
public function createConnectionProvider() : ConnectionProviderInterface |
129
|
|
|
{ |
130
|
|
|
return new ConnectionProvider( |
131
|
|
|
$this->getConfig()->getConnectionConfig() |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return \Xervice\RabbitMQ\Message\MessageProviderInterface |
137
|
|
|
*/ |
138
|
|
|
public function getMessageProvider() : MessageProviderInterface |
139
|
|
|
{ |
140
|
|
|
if ($this->messageProvider === null) { |
141
|
|
|
$this->messageProvider = $this->createMessageProvider(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $this->messageProvider; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return \Xervice\RabbitMQ\Core\ConnectionProviderInterface |
149
|
|
|
*/ |
150
|
|
|
public function getConnectionProvider() : ConnectionProviderInterface |
151
|
|
|
{ |
152
|
|
|
if ($this->connection === null) { |
153
|
|
|
$this->connection = $this->createConnectionProvider(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $this->connection; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @return \Xervice\RabbitMQ\Exchange\ExchangeCollection |
161
|
|
|
*/ |
162
|
|
|
public function getExchangeCollection() |
163
|
|
|
{ |
164
|
|
|
return $this->getDependency(RabbitMQDependencyProvider::RABBITMQ_EXCHANGES); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @return \Xervice\RabbitMQ\Queue\QueueCollection |
169
|
|
|
*/ |
170
|
|
|
public function getQueueCollection() |
171
|
|
|
{ |
172
|
|
|
return $this->getDependency(RabbitMQDependencyProvider::RABBITMQ_QUEUES); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @return \Xervice\RabbitMQ\Worker\Listener\ListenerCollection |
177
|
|
|
*/ |
178
|
|
|
public function getListenerCollection() |
179
|
|
|
{ |
180
|
|
|
return $this->getDependency(RabbitMQDependencyProvider::RABBITMQ_LISTENER); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
} |