1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MyOnlineStore\Bundle\RabbitMqManagerBundle\Generator; |
4
|
|
|
|
5
|
|
|
use Indigo\Ini\Renderer; |
6
|
|
|
use League\Flysystem\FilesystemInterface; |
7
|
|
|
use MyOnlineStore\Bundle\RabbitMqManagerBundle\Configuration\Consumer\ConsumerConfiguration; |
8
|
|
|
use MyOnlineStore\Bundle\RabbitMqManagerBundle\Configuration\Supervisor\SupervisorConfiguration; |
9
|
|
|
|
10
|
|
|
final class RabbitMqConfigGenerator implements RabbitMqConfigGeneratorInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var SupervisorConfiguration |
14
|
|
|
*/ |
15
|
|
|
private $supervisorConfiguration; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var ConsumerConfiguration |
19
|
|
|
*/ |
20
|
|
|
private $consumerConfiguration; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Renderer |
24
|
|
|
*/ |
25
|
|
|
private $renderer; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var FilesystemInterface |
29
|
|
|
*/ |
30
|
|
|
private $filesystem; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
private $config; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param SupervisorConfiguration $supervisorConfiguration |
39
|
|
|
* @param ConsumerConfiguration $consumerConfiguration |
40
|
|
|
* @param Renderer $renderer |
41
|
|
|
* @param FilesystemInterface $filesystem |
42
|
|
|
* @param array $config |
43
|
|
|
*/ |
44
|
4 |
|
public function __construct( |
45
|
|
|
SupervisorConfiguration $supervisorConfiguration, |
46
|
|
|
ConsumerConfiguration $consumerConfiguration, |
47
|
|
|
Renderer $renderer, |
48
|
|
|
FilesystemInterface $filesystem, |
49
|
|
|
array $config |
50
|
|
|
) { |
51
|
4 |
|
$this->supervisorConfiguration = $supervisorConfiguration; |
52
|
4 |
|
$this->consumerConfiguration = $consumerConfiguration; |
53
|
4 |
|
$this->renderer = $renderer; |
54
|
4 |
|
$this->filesystem = $filesystem; |
55
|
4 |
|
$this->config = $config; |
56
|
4 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @inheritdoc |
60
|
|
|
*/ |
61
|
4 |
|
public function generate() |
62
|
|
|
{ |
63
|
4 |
|
$supervisorSection = $this->supervisorConfiguration->generate(); |
64
|
|
|
|
65
|
4 |
|
foreach (['consumers', 'rpc_servers'] as $type) { |
66
|
4 |
|
foreach ($this->config[$type] as $consumerName => $consumer) { |
67
|
3 |
|
if (!isset($consumer['worker']['queue']['routing'])) { |
68
|
|
|
// this can be moved to DI\Configuration |
69
|
|
|
$consumer['worker']['queue']['routing'] = [null]; |
70
|
|
|
} |
71
|
|
|
|
72
|
3 |
|
foreach ($consumer['worker']['queue']['routing'] as $index => $route) { |
73
|
3 |
|
$name = sprintf('%s_%s_%d', substr($type, 0, 1), $consumerName, $index); |
74
|
|
|
|
75
|
3 |
|
if ('cli-consumer' === $consumer['processor']) { |
76
|
1 |
|
$this->filesystem->put( |
77
|
1 |
|
sprintf('%s.conf', $name), |
78
|
|
|
|
79
|
1 |
|
$this->renderer->render( |
80
|
1 |
|
$this->consumerConfiguration->generate($consumer, $route)->toArray() |
81
|
1 |
|
) |
82
|
1 |
|
); |
83
|
|
|
|
84
|
1 |
|
$supervisorSection->addSection( |
85
|
1 |
|
$this->supervisorConfiguration->generateProgram( |
86
|
1 |
|
$name, |
87
|
1 |
|
$this->supervisorConfiguration->getConsumerProperties( |
88
|
1 |
|
$consumer, |
89
|
1 |
|
sprintf('%s/%s.conf', $this->config['path'], $name) |
90
|
1 |
|
) |
91
|
1 |
|
) |
92
|
1 |
|
); |
93
|
1 |
|
} else { |
94
|
2 |
|
$supervisorSection->addSection( |
95
|
2 |
|
$this->supervisorConfiguration->generateProgram( |
96
|
2 |
|
$name, |
97
|
2 |
|
$this->supervisorConfiguration->getBundleProperties($consumer, $route) |
98
|
2 |
|
) |
99
|
2 |
|
); |
100
|
|
|
}; |
101
|
3 |
|
} |
102
|
4 |
|
} |
103
|
4 |
|
} |
104
|
|
|
|
105
|
4 |
|
$this->filesystem->put('supervisord.conf', $this->renderer->render( |
106
|
4 |
|
$supervisorSection->toArray() |
107
|
4 |
|
)); |
108
|
4 |
|
} |
109
|
|
|
} |
110
|
|
|
|