|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jellyfish\Event; |
|
4
|
|
|
|
|
5
|
|
|
use Jellyfish\Event\Command\EventQueueConsumeCommand; |
|
6
|
|
|
use Jellyfish\Event\Command\EventQueueWorkerStartCommand; |
|
7
|
|
|
use Pimple\Container; |
|
8
|
|
|
use Pimple\ServiceProviderInterface; |
|
9
|
|
|
|
|
10
|
|
|
class EventServiceProvider implements ServiceProviderInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @param \Pimple\Container $pimple |
|
14
|
|
|
* |
|
15
|
|
|
* @return void |
|
16
|
|
|
*/ |
|
17
|
|
|
public function register(Container $pimple): void |
|
18
|
|
|
{ |
|
19
|
|
|
$this->createEventFactory($pimple); |
|
20
|
|
|
$this->createEventQueueNameGenerator($pimple); |
|
21
|
|
|
$this->createEventMapper($pimple); |
|
22
|
|
|
$this->createEventQueueProducer($pimple); |
|
23
|
|
|
$this->createEventQueueConsumer($pimple); |
|
24
|
|
|
$this->createEventListenerProvider($pimple); |
|
25
|
|
|
$this->createEventDispatcher($pimple); |
|
26
|
|
|
$this->createEventQueueWorker($pimple); |
|
27
|
|
|
$this->createCommands($pimple); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param \Pimple\Container $container |
|
32
|
|
|
* |
|
33
|
|
|
* @return \Pimple\ServiceProviderInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
protected function createEventFactory(Container $container): ServiceProviderInterface |
|
36
|
|
|
{ |
|
37
|
|
|
$container->offsetSet('event_factory', function () { |
|
38
|
|
|
return new EventFactory(); |
|
39
|
|
|
}); |
|
40
|
|
|
|
|
41
|
|
|
return $this; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param \Pimple\Container $container |
|
46
|
|
|
* |
|
47
|
|
|
* @return \Pimple\ServiceProviderInterface |
|
48
|
|
|
*/ |
|
49
|
|
|
protected function createEventQueueNameGenerator(Container $container): ServiceProviderInterface |
|
50
|
|
|
{ |
|
51
|
|
|
$container->offsetSet('event_queue_name_generator', function () { |
|
52
|
|
|
return new EventQueueNameGenerator(); |
|
53
|
|
|
}); |
|
54
|
|
|
|
|
55
|
|
|
return $this; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param \Pimple\Container $container |
|
60
|
|
|
* |
|
61
|
|
|
* @return \Pimple\ServiceProviderInterface |
|
62
|
|
|
*/ |
|
63
|
|
|
protected function createEventMapper(Container $container): ServiceProviderInterface |
|
64
|
|
|
{ |
|
65
|
|
|
$container->offsetSet('event_mapper', function (Container $container) { |
|
66
|
|
|
return new EventMapper( |
|
67
|
|
|
$container->offsetGet('event_factory'), |
|
68
|
|
|
$container->offsetGet('message_factory'), |
|
69
|
|
|
$container->offsetGet('serializer') |
|
70
|
|
|
); |
|
71
|
|
|
}); |
|
72
|
|
|
|
|
73
|
|
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param \Pimple\Container $container |
|
78
|
|
|
* |
|
79
|
|
|
* @return \Pimple\ServiceProviderInterface |
|
80
|
|
|
*/ |
|
81
|
|
|
protected function createEventQueueProducer(Container $container): ServiceProviderInterface |
|
82
|
|
|
{ |
|
83
|
|
|
$container->offsetSet('event_queue_producer', function (Container $container) { |
|
84
|
|
|
return new EventQueueProducer( |
|
85
|
|
|
$container->offsetGet('event_mapper'), |
|
86
|
|
|
$container->offsetGet('event_queue_name_generator'), |
|
87
|
|
|
$container->offsetGet('queue_client') |
|
88
|
|
|
); |
|
89
|
|
|
}); |
|
90
|
|
|
|
|
91
|
|
|
return $this; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param \Pimple\Container $container |
|
96
|
|
|
* |
|
97
|
|
|
* @return \Pimple\ServiceProviderInterface |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function createEventQueueConsumer(Container $container): ServiceProviderInterface |
|
100
|
|
|
{ |
|
101
|
|
|
$container->offsetSet('event_queue_consumer', function (Container $container) { |
|
102
|
|
|
return new EventQueueConsumer( |
|
103
|
|
|
$container->offsetGet('process_factory'), |
|
104
|
|
|
$container->offsetGet('event_mapper'), |
|
105
|
|
|
$container->offsetGet('event_queue_name_generator'), |
|
106
|
|
|
$container->offsetGet('queue_client'), |
|
107
|
|
|
$container->offsetGet('root_dir') |
|
108
|
|
|
); |
|
109
|
|
|
}); |
|
110
|
|
|
|
|
111
|
|
|
return $this; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
protected function createEventListenerProvider(Container $container): ServiceProviderInterface |
|
115
|
|
|
{ |
|
116
|
|
|
$container->offsetSet('event_listener_provider', function () { |
|
117
|
|
|
return new EventListenerProvider(); |
|
118
|
|
|
}); |
|
119
|
|
|
|
|
120
|
|
|
return $this; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @param \Pimple\Container $container |
|
125
|
|
|
* |
|
126
|
|
|
* @return \Pimple\ServiceProviderInterface |
|
127
|
|
|
*/ |
|
128
|
|
|
protected function createEventDispatcher(Container $container): ServiceProviderInterface |
|
129
|
|
|
{ |
|
130
|
|
|
$container->offsetSet('event_dispatcher', function (Container $container) { |
|
131
|
|
|
return new EventDispatcher( |
|
132
|
|
|
$container->offsetGet('event_listener_provider'), |
|
133
|
|
|
$container->offsetGet('event_queue_producer') |
|
134
|
|
|
); |
|
135
|
|
|
}); |
|
136
|
|
|
|
|
137
|
|
|
return $this; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param \Pimple\Container $container |
|
142
|
|
|
* |
|
143
|
|
|
* @return \Pimple\ServiceProviderInterface |
|
144
|
|
|
*/ |
|
145
|
|
|
protected function createEventQueueWorker(Container $container): ServiceProviderInterface |
|
146
|
|
|
{ |
|
147
|
|
|
$container->offsetSet('event_queue_worker', function (Container $container) { |
|
148
|
|
|
return new EventQueueWorker( |
|
149
|
|
|
$container->offsetGet('event_listener_provider'), |
|
150
|
|
|
$container->offsetGet('event_queue_consumer') |
|
151
|
|
|
); |
|
152
|
|
|
}); |
|
153
|
|
|
|
|
154
|
|
|
return $this; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @param \Pimple\Container $container |
|
159
|
|
|
* |
|
160
|
|
|
* @return \Pimple\ServiceProviderInterface |
|
161
|
|
|
*/ |
|
162
|
|
|
protected function createCommands(Container $container): ServiceProviderInterface |
|
163
|
|
|
{ |
|
164
|
|
|
$container->extend('commands', function (array $commands, Container $container) { |
|
165
|
|
|
$commands[] = new EventQueueConsumeCommand( |
|
166
|
|
|
$container->offsetGet('event_listener_provider'), |
|
167
|
|
|
$container->offsetGet('event_queue_consumer'), |
|
168
|
|
|
$container->offsetGet('lock_factory'), |
|
169
|
|
|
$container->offsetGet('logger') |
|
170
|
|
|
); |
|
171
|
|
|
|
|
172
|
|
|
$commands[] = new EventQueueWorkerStartCommand( |
|
173
|
|
|
$container->offsetGet('event_queue_worker') |
|
174
|
|
|
); |
|
175
|
|
|
|
|
176
|
|
|
return $commands; |
|
177
|
|
|
}); |
|
178
|
|
|
|
|
179
|
|
|
return $this; |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|