|
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 |
|
$supervisorConfiguration = $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->write( |
|
77
|
1 |
|
sprintf('%s.conf', $name), |
|
78
|
1 |
|
$this->renderer->render($this->consumerConfiguration->generate($consumer, $route)->toArray()) |
|
79
|
|
|
); |
|
80
|
|
|
|
|
81
|
1 |
|
$supervisorConfiguration->addSection( |
|
82
|
1 |
|
$this->supervisorConfiguration->generateProgram( |
|
83
|
|
|
$name, |
|
84
|
1 |
|
$this->supervisorConfiguration->getConsumerProperties( |
|
85
|
|
|
$consumer, |
|
86
|
1 |
|
sprintf('%s/%s.conf', $this->config['path'], $name) |
|
87
|
|
|
) |
|
88
|
|
|
) |
|
89
|
|
|
); |
|
90
|
|
|
} else { |
|
91
|
2 |
|
$supervisorConfiguration->addSection( |
|
92
|
2 |
|
$this->supervisorConfiguration->generateProgram( |
|
93
|
|
|
$name, |
|
94
|
4 |
|
$this->supervisorConfiguration->getBundleProperties($consumer, $route) |
|
95
|
|
|
) |
|
96
|
|
|
); |
|
97
|
|
|
}; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
4 |
|
$this->write('supervisord.conf', $this->renderer->render( |
|
103
|
4 |
|
$supervisorConfiguration->toArray() |
|
104
|
|
|
)); |
|
105
|
4 |
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param string $path |
|
109
|
|
|
* @param string $content |
|
110
|
|
|
*/ |
|
111
|
4 |
|
private function write($path, $content) |
|
112
|
|
|
{ |
|
113
|
4 |
|
if ($this->filesystem->has($path)) { |
|
114
|
2 |
|
$this->filesystem->update($path, $content); |
|
115
|
|
|
} else { |
|
116
|
3 |
|
$this->filesystem->write($path, $content); |
|
117
|
|
|
} |
|
118
|
4 |
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|