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