|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ola\RabbitMqAdminToolkitBundle\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Ola\RabbitMqAdminToolkitBundle\DependencyInjection\OlaRabbitMqAdminToolkitExtension; |
|
6
|
|
|
use Ola\RabbitMqAdminToolkitBundle\VhostConfiguration; |
|
7
|
|
|
use Ola\RabbitMqAdminToolkitBundle\VhostHandler; |
|
8
|
|
|
use Symfony\Component\Console\Command\Command; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
12
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
|
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
14
|
|
|
|
|
15
|
|
|
class VhostDefineCommand extends Command |
|
16
|
|
|
{ |
|
17
|
6 |
|
protected static $defaultName = 'rabbitmq:vhost:define'; |
|
18
|
|
|
|
|
19
|
6 |
|
private ContainerInterface $container; |
|
|
|
|
|
|
20
|
6 |
|
|
|
21
|
6 |
|
private array $vhostList; |
|
22
|
6 |
|
|
|
23
|
6 |
|
private VhostHandler $vhostHandler; |
|
24
|
|
|
|
|
25
|
6 |
|
private bool $silentFailure; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct( |
|
28
|
|
|
ContainerInterface $container, |
|
29
|
|
|
array $vhostList, |
|
30
|
6 |
|
VhostHandler $vhostHandler, |
|
31
|
|
|
bool $silentFailure |
|
32
|
6 |
|
) { |
|
33
|
|
|
parent::__construct(); |
|
34
|
6 |
|
|
|
35
|
|
|
$this->container = $container; |
|
36
|
6 |
|
$this->vhostList = $vhostList; |
|
37
|
6 |
|
$this->vhostHandler = $vhostHandler; |
|
38
|
|
|
$this->silentFailure = $silentFailure; |
|
39
|
6 |
|
} |
|
40
|
|
|
|
|
41
|
6 |
|
/** |
|
42
|
4 |
|
* {@inheritDoc} |
|
43
|
4 |
|
*/ |
|
44
|
|
|
protected function configure() |
|
45
|
4 |
|
{ |
|
46
|
|
|
$this |
|
47
|
4 |
|
->setDescription('Create or update a vhost') |
|
48
|
4 |
|
->addArgument('vhost', InputArgument::OPTIONAL, 'Which vhost should be configured ?') |
|
49
|
4 |
|
; |
|
50
|
5 |
|
} |
|
51
|
4 |
|
|
|
52
|
6 |
|
/** |
|
53
|
2 |
|
* {@inheritDoc} |
|
54
|
1 |
|
*/ |
|
55
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
|
56
|
|
|
{ |
|
57
|
5 |
|
$vhostList = $this->getVhostList($input); |
|
58
|
5 |
|
|
|
59
|
|
|
foreach ($vhostList as $vhost) { |
|
60
|
|
|
try { |
|
61
|
|
|
$this->comment($input, $output, sprintf( |
|
62
|
|
|
'Define rabbitmq <info>%s</info> vhost configuration', |
|
63
|
|
|
$vhost |
|
64
|
|
|
)); |
|
65
|
|
|
|
|
66
|
|
|
$vhostConfiguration = $this->getVhostConfiguration($vhost); |
|
67
|
6 |
|
$this->vhostHandler->define($vhostConfiguration); |
|
68
|
|
|
|
|
69
|
6 |
|
$this->success($input, $output, sprintf( |
|
70
|
6 |
|
'Rabbitmq "%s" vhost configuration successfully %s !', |
|
71
|
6 |
|
$vhost, |
|
72
|
4 |
|
!$this->vhostHandler->exists($vhostConfiguration) ? 'created' : 'updated' |
|
73
|
4 |
|
)); |
|
74
|
|
|
} catch (\Exception $e) { |
|
75
|
6 |
|
if (!$this->silentFailure) { |
|
76
|
|
|
throw $e; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return 0; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
6 |
|
* Return Vhosts to process |
|
86
|
|
|
* |
|
87
|
6 |
|
* @param InputInterface $input |
|
88
|
6 |
|
* |
|
89
|
|
|
* @return array |
|
90
|
6 |
|
*/ |
|
91
|
|
|
private function getVhostList(InputInterface $input): array |
|
92
|
6 |
|
{ |
|
93
|
2 |
|
$inputVhost = $input->getArgument('vhost'); |
|
94
|
2 |
|
|
|
95
|
|
|
return empty($inputVhost) ? $this->vhostList : [$inputVhost]; |
|
96
|
2 |
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
4 |
|
* @param string $vhost |
|
100
|
|
|
* |
|
101
|
|
|
* @return VhostConfiguration |
|
102
|
|
|
* |
|
103
|
|
|
* @throws \InvalidArgumentException |
|
104
|
|
|
*/ |
|
105
|
|
|
private function getVhostConfiguration(string $vhost): VhostConfiguration |
|
106
|
|
|
{ |
|
107
|
6 |
|
$serviceName = sprintf( |
|
108
|
|
|
OlaRabbitMqAdminToolkitExtension::VHOST_MANAGER_SERVICE_TEMPLATE, |
|
109
|
6 |
|
$vhost |
|
110
|
|
|
); |
|
111
|
6 |
|
|
|
112
|
6 |
|
if (!$this->container->has($serviceName)) { |
|
113
|
6 |
|
throw new \InvalidArgumentException(sprintf( |
|
114
|
|
|
'No configuration service found for vhost : "%s"', |
|
115
|
|
|
$vhost |
|
116
|
6 |
|
)); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $this->container->get($serviceName); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
4 |
|
* @param InputInterface $input |
|
124
|
|
|
* @param OutputInterface $output |
|
125
|
4 |
|
* @param string $message |
|
126
|
|
|
*/ |
|
127
|
4 |
|
private function comment(InputInterface $input, OutputInterface $output, string $message): void |
|
128
|
4 |
|
{ |
|
129
|
4 |
|
$io = $this->getIO($input, $output); |
|
130
|
|
|
|
|
131
|
|
|
if (null !== $io && method_exists($io, 'comment')) { |
|
132
|
4 |
|
$io->comment($message); |
|
133
|
|
|
} else { |
|
134
|
|
|
$output->writeln(sprintf('<comment>%s</comment>', $message)); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
6 |
|
* @param InputInterface $input |
|
140
|
|
|
* @param OutputInterface $output |
|
141
|
6 |
|
* @param string $message |
|
142
|
6 |
|
*/ |
|
143
|
|
|
private function success(InputInterface $input, OutputInterface $output, string $message): void |
|
144
|
|
|
{ |
|
145
|
|
|
$io = $this->getIO($input, $output); |
|
146
|
|
|
|
|
147
|
|
|
if (null !== $io) { |
|
148
|
|
|
$io->success($message); |
|
149
|
|
|
} else { |
|
150
|
|
|
$output->writeln(sprintf('<info>%s</info>', $message)); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @param InputInterface $input |
|
156
|
|
|
* @param OutputInterface $output |
|
157
|
|
|
* |
|
158
|
|
|
* @return null|\Symfony\Component\Console\Style\SymfonyStyle |
|
159
|
|
|
*/ |
|
160
|
|
|
private function getIO(InputInterface $input, OutputInterface $output): ?SymfonyStyle |
|
161
|
|
|
{ |
|
162
|
|
|
if (class_exists(SymfonyStyle::class)) { |
|
163
|
|
|
return new SymfonyStyle($input, $output); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
return null; |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|