1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Scheduler implementation module. |
5
|
|
|
* |
6
|
|
|
* @author Maksim Masiukevich <[email protected]> |
7
|
|
|
* @license MIT |
8
|
|
|
* @license https://opensource.org/licenses/MIT |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types = 1); |
12
|
|
|
|
13
|
|
|
namespace ServiceBus\Scheduler\Module; |
14
|
|
|
|
15
|
|
|
use ServiceBus\Common\Module\ServiceBusModule; |
16
|
|
|
use ServiceBus\MessagesRouter\ChainRouterConfigurator; |
17
|
|
|
use ServiceBus\MessagesRouter\Router; |
18
|
|
|
use ServiceBus\Scheduler\Emitter\RabbitMQEmitter; |
19
|
|
|
use ServiceBus\Scheduler\Emitter\SchedulerEmitter; |
20
|
|
|
use ServiceBus\Scheduler\SchedulerProvider; |
21
|
|
|
use ServiceBus\Scheduler\Store\SchedulerStore; |
22
|
|
|
use ServiceBus\Scheduler\Store\SqlSchedulerStore; |
23
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
24
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
25
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* |
29
|
|
|
*/ |
30
|
|
|
final class SchedulerModule implements ServiceBusModule |
31
|
|
|
{ |
32
|
|
|
private const TYPE = 'rabbitmq'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $adapterType; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private $storeImplementationServiceId; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
private $databaseAdapterServiceId; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $databaseAdapterServiceId |
51
|
|
|
* |
52
|
|
|
* @return self |
53
|
|
|
*/ |
54
|
|
|
public static function rabbitMqWithSqlStorage(string $databaseAdapterServiceId): self |
55
|
|
|
{ |
56
|
|
|
return new self( |
57
|
|
|
self::TYPE, |
58
|
|
|
SqlSchedulerStore::class, |
59
|
|
|
$databaseAdapterServiceId |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
public function boot(ContainerBuilder $containerBuilder): void |
67
|
|
|
{ |
68
|
|
|
$this->registerSchedulerStore($containerBuilder); |
69
|
|
|
$this->registerSchedulerProvider($containerBuilder); |
70
|
|
|
$this->registerEmitter($containerBuilder); |
71
|
|
|
$this->registerSchedulerMessagesRouterConfigurator($containerBuilder); |
72
|
|
|
|
73
|
|
|
$routerConfiguratorDefinition = $this->getRouterConfiguratorDefinition($containerBuilder); |
74
|
|
|
|
75
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
76
|
|
|
$routerConfiguratorDefinition->addMethodCall( |
77
|
|
|
'addConfigurator', |
78
|
|
|
[new Reference(SchedulerMessagesRouterConfigurator::class)] |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param ContainerBuilder $containerBuilder |
84
|
|
|
* |
85
|
|
|
* @throws \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException |
86
|
|
|
* |
87
|
|
|
* @return Definition |
88
|
|
|
* |
89
|
|
|
*/ |
90
|
|
|
private function getRouterConfiguratorDefinition(ContainerBuilder $containerBuilder): Definition |
91
|
|
|
{ |
92
|
|
|
if (false === $containerBuilder->hasDefinition(ChainRouterConfigurator::class)) |
93
|
|
|
{ |
94
|
|
|
$containerBuilder->addDefinitions( |
95
|
|
|
[ |
96
|
|
|
ChainRouterConfigurator::class => new Definition(ChainRouterConfigurator::class), |
97
|
|
|
] |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
102
|
|
|
$routerConfiguratorDefinition = $containerBuilder->getDefinition(ChainRouterConfigurator::class); |
103
|
|
|
|
104
|
|
|
if (false === $containerBuilder->hasDefinition(Router::class)) |
105
|
|
|
{ |
106
|
|
|
$containerBuilder->addDefinitions([Router::class => new Definition(Router::class)]); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** @var Definition $routerConfiguratorDefinition */ |
110
|
|
|
|
111
|
|
|
return $routerConfiguratorDefinition; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param ContainerBuilder $containerBuilder |
116
|
|
|
* |
117
|
|
|
* @return void |
118
|
|
|
*/ |
119
|
|
|
private function registerSchedulerMessagesRouterConfigurator(ContainerBuilder $containerBuilder): void |
120
|
|
|
{ |
121
|
|
|
$containerBuilder->addDefinitions([ |
122
|
|
|
SchedulerMessagesRouterConfigurator::class => (new Definition(SchedulerMessagesRouterConfigurator::class)) |
123
|
|
|
->setArguments([new Reference(SchedulerEmitter::class)]), |
124
|
|
|
]); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param ContainerBuilder $containerBuilder |
129
|
|
|
* |
130
|
|
|
* @throws \LogicException |
131
|
|
|
* |
132
|
|
|
* @return void |
133
|
|
|
* |
134
|
|
|
*/ |
135
|
|
|
private function registerEmitter(ContainerBuilder $containerBuilder): void |
136
|
|
|
{ |
137
|
|
|
if (self::TYPE === $this->adapterType) |
138
|
|
|
{ |
139
|
|
|
$containerBuilder->addDefinitions([ |
140
|
|
|
SchedulerEmitter::class => (new Definition(RabbitMQEmitter::class)) |
141
|
|
|
->setArguments([new Reference(SchedulerStore::class)]), |
142
|
|
|
]); |
143
|
|
|
|
144
|
|
|
return; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
throw new \LogicException('Wrong adapter type'); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param ContainerBuilder $containerBuilder |
152
|
|
|
* |
153
|
|
|
* @return void |
154
|
|
|
*/ |
155
|
|
|
private function registerSchedulerStore(ContainerBuilder $containerBuilder): void |
156
|
|
|
{ |
157
|
|
|
$containerBuilder->addDefinitions([ |
158
|
|
|
SchedulerStore::class => (new Definition($this->storeImplementationServiceId)) |
159
|
|
|
->setArguments([new Reference($this->databaseAdapterServiceId)]), |
160
|
|
|
]); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param ContainerBuilder $containerBuilder |
165
|
|
|
* |
166
|
|
|
* @return void |
167
|
|
|
*/ |
168
|
|
|
private function registerSchedulerProvider(ContainerBuilder $containerBuilder): void |
169
|
|
|
{ |
170
|
|
|
$containerBuilder->addDefinitions([ |
171
|
|
|
SchedulerProvider::class => (new Definition(SchedulerProvider::class)) |
172
|
|
|
->setArguments([new Reference(SchedulerStore::class)]), |
173
|
|
|
]); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param string $adapterType |
178
|
|
|
* @param string $storeImplementationServiceId |
179
|
|
|
* @param string $databaseAdapterServiceId |
180
|
|
|
*/ |
181
|
|
|
private function __construct( |
182
|
|
|
string $adapterType, |
183
|
|
|
string $storeImplementationServiceId, |
184
|
|
|
string $databaseAdapterServiceId |
185
|
|
|
) { |
186
|
|
|
$this->adapterType = $adapterType; |
187
|
|
|
$this->storeImplementationServiceId = $storeImplementationServiceId; |
188
|
|
|
$this->databaseAdapterServiceId = $databaseAdapterServiceId; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|