1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dtc\QueueBundle\DependencyInjection\Compiler; |
4
|
|
|
|
5
|
|
|
use Dtc\QueueBundle\Model\Job; |
6
|
|
|
use Dtc\QueueBundle\Model\JobTiming; |
7
|
|
|
use Dtc\QueueBundle\Model\Run; |
8
|
|
|
use Dtc\QueueBundle\Exception\ClassNotFoundException; |
9
|
|
|
use Dtc\QueueBundle\Exception\ClassNotSubclassException; |
10
|
|
|
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
11
|
|
|
use Symfony\Component\DependencyInjection\Alias; |
12
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
13
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
14
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
15
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
16
|
|
|
|
17
|
|
|
class WorkerCompilerPass implements CompilerPassInterface |
18
|
|
|
{ |
19
|
|
|
use WorkerCompilerTrait; |
20
|
|
|
|
21
|
4 |
|
public function process(ContainerBuilder $container) |
22
|
|
|
{ |
23
|
4 |
|
if (false === $container->hasDefinition('dtc_queue.manager.worker')) { |
24
|
4 |
|
return; |
25
|
|
|
} |
26
|
|
|
|
27
|
4 |
|
$this->setupAliases($container); |
28
|
|
|
|
29
|
4 |
|
$definition = $container->getDefinition('dtc_queue.manager.worker'); |
30
|
|
|
|
31
|
4 |
|
$jobClass = $this->getJobClass($container); |
32
|
3 |
|
$jobArchiveClass = $this->getJobClassArchive($container); |
33
|
3 |
|
$container->setParameter('dtc_queue.class.job', $jobClass); |
34
|
3 |
|
$container->setParameter('dtc_queue.class.job_archive', $jobArchiveClass); |
35
|
|
|
|
36
|
3 |
|
$managerType = $this->getRunManagerType($container); |
37
|
3 |
|
$jobTimingManagerType = $this->getJobTimingManagerType($container); |
38
|
3 |
|
$container->setParameter('dtc_queue.class.job_timing', $this->getClass( |
39
|
3 |
|
$container, |
40
|
|
|
$jobTimingManagerType, |
41
|
3 |
|
'job_timing', |
42
|
3 |
|
'JobTiming', |
43
|
3 |
|
JobTiming::class |
44
|
|
|
)); |
45
|
3 |
|
$container->setParameter('dtc_queue.class.run', $this->getClass($container, $managerType, 'run', 'Run', Run::class)); |
46
|
3 |
|
$container->setParameter('dtc_queue.class.run_archive', $this->getClass($container, $managerType, 'run_archive', 'RunArchive', Run::class)); |
47
|
|
|
|
48
|
3 |
|
$this->setupTaggedServices($container, $definition); |
49
|
2 |
|
$eventDispatcher = $container->getDefinition('dtc_queue.event_dispatcher'); |
50
|
2 |
|
foreach ($container->findTaggedServiceIds('dtc_queue.event_subscriber') as $id => $attributes) { |
51
|
|
|
$eventSubscriber = $container->getDefinition($id); |
52
|
|
|
$eventDispatcher->addMethodCall('addSubscriber', [$eventSubscriber]); |
53
|
|
|
} |
54
|
2 |
|
$this->setupDoctrineManagers($container); |
55
|
2 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Add any extra method calls needed. |
59
|
|
|
* |
60
|
|
|
* @param ContainerBuilder $container |
61
|
|
|
* @param string $managerType |
62
|
|
|
*/ |
63
|
4 |
|
protected function addMethodCalls(ContainerBuilder $container, $managerType) |
64
|
|
|
{ |
65
|
4 |
|
if ('orm' === $managerType) { |
66
|
1 |
|
$doctrine = $container->getDefinition('doctrine'); |
67
|
1 |
|
$container->getDefinition('dtc_queue.doctrine_listener')->addMethodCall('setRegistry', [$doctrine]); |
68
|
1 |
|
$container->getDefinition('dtc_queue.manager.job.orm')->addMethodCall('setRegistry', [$doctrine]); |
69
|
1 |
|
$container->getDefinition('dtc_queue.manager.run.orm')->addMethodCall('setRegistry', [$doctrine]); |
70
|
1 |
|
$container->getDefinition('dtc_queue.manager.job_timing.orm')->addMethodCall('setRegistry', [$doctrine]); |
71
|
|
|
} |
72
|
4 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $type |
76
|
|
|
*/ |
77
|
4 |
|
protected function setupAlias(ContainerBuilder $container, $defaultManagerType, $type) |
78
|
|
|
{ |
79
|
4 |
|
$definitionName = 'dtc_queue.'.$type.'.'.$defaultManagerType; |
80
|
4 |
|
if (!$container->hasDefinition($definitionName) && !$container->hasAlias($definitionName)) { |
81
|
|
|
throw new InvalidConfigurationException("No $type manager found for dtc_queue.$type.$defaultManagerType"); |
82
|
|
|
} |
83
|
4 |
|
if ($container->hasDefinition($definitionName)) { |
84
|
4 |
|
$alias = new Alias('dtc_queue.'.$type.'.'.$defaultManagerType); |
85
|
4 |
|
$alias->setPublic(true); |
86
|
4 |
|
$container->setAlias('dtc_queue.'.$type, $alias); |
87
|
|
|
|
88
|
4 |
|
return; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$container->getAlias($definitionName)->setPublic(true); |
92
|
|
|
$container->setAlias('dtc_queue.'.$type, $container->getAlias($definitionName)); |
93
|
|
|
} |
94
|
|
|
|
95
|
4 |
|
protected function setupAliases(ContainerBuilder $container) |
96
|
|
|
{ |
97
|
4 |
|
$defaultManagerType = $container->getParameter('dtc_queue.manager.job'); |
98
|
4 |
|
$this->addMethodCalls($container, $defaultManagerType); |
99
|
4 |
|
$this->setupAlias($container, $defaultManagerType, 'manager.job'); |
100
|
4 |
|
$runManagerType = $container->getParameter($this->getRunManagerType($container)); |
101
|
4 |
|
$this->addMethodCalls($container, $runManagerType); |
102
|
4 |
|
$this->setupAlias($container, $runManagerType, 'manager.run'); |
103
|
4 |
|
$jobTimingManagerType = $container->getParameter($this->getJobTimingManagerType($container)); |
104
|
4 |
|
$this->addMethodCalls($container, $jobTimingManagerType); |
105
|
4 |
|
$this->setupAlias($container, $jobTimingManagerType, 'manager.job_timing'); |
106
|
4 |
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param ContainerBuilder $container |
110
|
|
|
* @param Definition $definition |
111
|
|
|
* @throws |
112
|
|
|
*/ |
113
|
3 |
|
protected function setupTaggedServices(ContainerBuilder $container, Definition $definition) |
114
|
|
|
{ |
115
|
3 |
|
$jobManagerRef = array(new Reference('dtc_queue.manager.job')); |
116
|
|
|
// Add each worker to workerManager, make sure each worker has instance to work |
117
|
3 |
|
foreach ($container->findTaggedServiceIds('dtc_queue.worker') as $id => $attributes) { |
118
|
2 |
|
$worker = $container->getDefinition($id); |
119
|
2 |
|
$class = $container->getDefinition($id)->getClass(); |
120
|
|
|
|
121
|
2 |
|
$refClass = new \ReflectionClass($class); |
122
|
2 |
|
$workerClass = 'Dtc\QueueBundle\Model\Worker'; |
123
|
2 |
|
if (!$refClass->isSubclassOf($workerClass)) { |
124
|
1 |
|
throw new \InvalidArgumentException(sprintf('Service "%s" must extend class "%s".', $id, $workerClass)); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
// Give each worker access to job manager |
128
|
1 |
|
$worker->addMethodCall('setJobManager', $jobManagerRef); |
129
|
1 |
|
$definition->addMethodCall('addWorker', array(new Reference($id))); |
130
|
|
|
} |
131
|
2 |
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param ContainerBuilder $container |
135
|
|
|
*/ |
136
|
2 |
|
protected function setupDoctrineManagers(ContainerBuilder $container) |
137
|
|
|
{ |
138
|
2 |
|
$documentManager = $container->getParameter('dtc_queue.odm.document_manager'); |
139
|
|
|
|
140
|
2 |
|
$odmManager = "doctrine_mongodb.odm.{$documentManager}_document_manager"; |
141
|
2 |
|
if ($container->has($odmManager)) { |
142
|
|
|
$container->setAlias('dtc_queue.document_manager', $odmManager); |
143
|
|
|
} |
144
|
|
|
|
145
|
2 |
|
$entityManager = $container->getParameter('dtc_queue.orm.entity_manager'); |
146
|
|
|
|
147
|
2 |
|
$ormManager = "doctrine.orm.{$entityManager}_entity_manager"; |
148
|
2 |
|
if ($container->has($ormManager)) { |
149
|
|
|
$container->setAlias('dtc_queue.entity_manager', $ormManager); |
150
|
|
|
} |
151
|
2 |
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param $managerType |
155
|
|
|
* |
156
|
|
|
* @return string|null |
157
|
|
|
*/ |
158
|
4 |
|
protected function getDirectory($managerType) |
159
|
|
|
{ |
160
|
4 |
|
switch ($managerType) { |
161
|
4 |
|
case 'odm': |
162
|
3 |
|
return 'Document'; |
163
|
2 |
|
case 'beanstalkd': |
164
|
1 |
|
return 'Beanstalkd'; |
165
|
2 |
|
case 'rabbit_mq': |
166
|
1 |
|
return 'RabbitMQ'; |
167
|
2 |
|
case 'orm': |
168
|
1 |
|
return 'Entity'; |
169
|
2 |
|
case 'redis': |
170
|
1 |
|
return 'Redis'; |
171
|
|
|
} |
172
|
|
|
|
173
|
1 |
|
return null; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Determines the job class based on the queue manager type. |
178
|
|
|
* |
179
|
|
|
* @param ContainerBuilder $container |
180
|
|
|
* |
181
|
|
|
* @return mixed|string |
182
|
|
|
* |
183
|
|
|
* @throws |
184
|
|
|
*/ |
185
|
4 |
|
protected function getJobClass(ContainerBuilder $container) |
186
|
|
|
{ |
187
|
4 |
|
$jobClass = $container->getParameter('dtc_queue.class.job'); |
188
|
4 |
|
if (!$jobClass) { |
189
|
4 |
|
if ($directory = $this->getDirectory($managerType = $container->getParameter('dtc_queue.manager.job'))) { |
190
|
3 |
|
$jobClass = 'Dtc\QueueBundle\\'.$directory.'\Job'; |
191
|
|
|
} else { |
192
|
1 |
|
throw new InvalidConfigurationException('Unknown manager.job type '.$managerType.' - please specify a Job class in the \'class\' configuration parameter'); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
196
|
3 |
|
$this->testClass($jobClass, Job::class); |
197
|
|
|
|
198
|
3 |
|
return $jobClass; |
199
|
|
|
} |
200
|
|
|
|
201
|
4 |
|
protected function getJobTimingManagerType(ContainerBuilder $container) |
202
|
|
|
{ |
203
|
4 |
|
$managerType = $this->getRunManagerType($container); |
204
|
4 |
|
if ($container->hasParameter('dtc_queue.manager.job_timing')) { |
205
|
4 |
|
$managerType = 'dtc_queue.manager.job_timing'; |
206
|
|
|
} |
207
|
|
|
|
208
|
4 |
|
return $managerType; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @param string $managerType |
213
|
|
|
* @param string $type |
214
|
|
|
* @param string $className |
215
|
|
|
*/ |
216
|
3 |
|
protected function getClass(ContainerBuilder $container, $managerType, $type, $className, $baseClass) |
217
|
|
|
{ |
218
|
3 |
|
$runClass = $container->hasParameter('dtc_queue.class.'.$type) ? $container->getParameter('dtc_queue.class.'.$type) : null; |
219
|
3 |
|
if (!$runClass) { |
220
|
3 |
|
switch ($container->getParameter($managerType)) { |
221
|
3 |
|
case 'odm': |
222
|
3 |
|
$runClass = 'Dtc\\QueueBundle\\Document\\'.$className; |
223
|
3 |
|
break; |
224
|
1 |
|
case 'orm': |
225
|
1 |
|
$runClass = 'Dtc\\QueueBundle\\Entity\\'.$className; |
226
|
1 |
|
break; |
227
|
|
|
default: |
228
|
|
|
$runClass = $baseClass; |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|
232
|
3 |
|
$this->testClass($runClass, $baseClass); |
233
|
|
|
|
234
|
3 |
|
return $runClass; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @throws ClassNotFoundException |
239
|
|
|
* @throws ClassNotSubclassException |
240
|
|
|
*/ |
241
|
3 |
|
protected function testClass($className, $parent) |
242
|
|
|
{ |
243
|
3 |
|
if (!class_exists($className)) { |
244
|
|
|
throw new ClassNotFoundException("Can't find class $className"); |
245
|
|
|
} |
246
|
|
|
|
247
|
3 |
|
$test = new $className(); |
248
|
3 |
|
if (!$test instanceof $className) { |
249
|
|
|
throw new ClassNotSubclassException("$className must be instance of (or derived from) $parent"); |
250
|
|
|
} |
251
|
3 |
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Determines the job class based on the queue manager type. |
255
|
|
|
* |
256
|
|
|
* @param ContainerBuilder $container |
257
|
|
|
* |
258
|
|
|
* @return mixed|string |
259
|
|
|
* |
260
|
|
|
* @throws ClassNotFoundException |
261
|
|
|
* @throws ClassNotSubclassException |
262
|
|
|
*/ |
263
|
3 |
|
protected function getJobClassArchive(ContainerBuilder $container) |
264
|
|
|
{ |
265
|
3 |
|
$jobArchiveClass = $container->getParameter('dtc_queue.class.job_archive'); |
266
|
3 |
|
if (!$jobArchiveClass) { |
267
|
3 |
|
switch ($container->getParameter('dtc_queue.manager.job')) { |
268
|
3 |
|
case 'odm': |
269
|
3 |
|
$jobArchiveClass = 'Dtc\\QueueBundle\\Document\\JobArchive'; |
270
|
3 |
|
break; |
271
|
1 |
|
case 'orm': |
272
|
1 |
|
$jobArchiveClass = 'Dtc\\QueueBundle\\Entity\\JobArchive'; |
273
|
1 |
|
break; |
274
|
|
|
} |
275
|
|
|
} |
276
|
3 |
|
if (null !== $jobArchiveClass) { |
277
|
3 |
|
$this->testClass($jobArchiveClass, Job::class); |
278
|
|
|
} |
279
|
|
|
|
280
|
3 |
|
return $jobArchiveClass; |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
|