forked from ezsystems/launchpad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProjectWizard.php
306 lines (259 loc) · 8.98 KB
/
ProjectWizard.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php
/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace eZ\Launchpad\Core;
use eZ\Launchpad\Configuration\Project as ProjectConfiguration;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Style\SymfonyStyle;
class ProjectWizard
{
/**
* @var SymfonyStyle
*/
protected $io;
/**
* @var ProjectConfiguration
*/
protected $projectConfiguration;
const INIT_STD = 'standard';
const INIT_STD_COMPOER_AUTH = 'standard-with-composer-auth';
const INIT_EXPERT = 'expert';
/**
* @var array
*/
protected static $modes = [
self::INIT_STD,
self::INIT_STD_COMPOER_AUTH,
self::INIT_EXPERT,
];
/**
* @var string
*/
protected $mode;
/**
* ProjectWizard constructor.
*
* @param SymfonyStyle $io
* @param ProjectConfiguration $configuration
*/
public function __construct(SymfonyStyle $io, ProjectConfiguration $configuration)
{
$this->io = $io;
$this->projectConfiguration = $configuration;
}
/**
* @param DockerCompose $compose
*
* @return array
*/
public function __invoke(DockerCompose $compose)
{
$this->mode = $this->getInitializationMode();
$configuration = [
$this->getNetworkName(),
$this->getNetworkTCPPort(),
$this->getComposerHttpBasicCredentials(),
$this->getSelectedServices(
$compose->getServices(),
['varnish', 'solr', 'mailcatcher', 'adminer', 'memcache', 'memcachedadmin']
),
$this->getProvisioningFolderName(),
$this->getComposeFileName(),
];
return $configuration;
}
/**
* @return string
*/
public function getInitializationMode()
{
$standard = self::INIT_STD;
$withComposer = self::INIT_STD_COMPOER_AUTH;
$expert = self::INIT_EXPERT;
$question = <<<END
eZ Launchpad will install a new architecture for you.
Three modes are available:
- <fg=cyan>{$standard}</>: All the services, no composer auth
- <fg=cyan>{$withComposer}</>: Standard with ability to provide Composer Auth, useful for eZ Platform Enterprise
- <fg=cyan>{$expert}</>: All the questions will be asked and you can select the services you want only
Please select your <fg=yellow;options=bold>Init</>ialization mode
END;
return $this->io->choice($question, self::$modes, self::INIT_STD);
}
/**
* @return array
*/
protected function getComposerHttpBasicCredentials()
{
if (!$this->requireComposerAuth()) {
return [];
}
$credentials = [];
$endString = '<fg=yellow;options=bold>Composer HTTP-BASIC</> for this project?';
$questionString = 'Do you want to set '.$endString;
while ($this->io->confirm($questionString, false)) {
list($host, $login, $password) = $this->getOneComposerHttpBasic();
$credentials[] = [$host, $login, $password];
$questionString = 'Do you want to add another '.$endString;
}
return $credentials;
}
/**
* @return array
*/
protected function getOneComposerHttpBasic()
{
$pattern = '^[a-zA-Z0-9\-\.]*$';
$validatorHost = function ($value) use ($pattern) {
return preg_match("/{$pattern}/", $value);
};
$message = 'What is the <fg=yellow;options=bold>host</> on which you want to add credentials?';
$errorMessage = "The host MUST respect {$pattern}.";
$default = 'updates.ez.no';
$host = $this->io->askQuestion($this->getQuestion($message, $default, $validatorHost, $errorMessage));
$login = $this->io->askQuestion($this->getQuestion('Login?'));
$password = $this->io->askQuestion($this->getQuestion('Password?'));
return [$host, $login, $password];
}
/**
* @return string
*/
protected function getProvisioningFolderName()
{
$default = $this->projectConfiguration->get('provisioning.folder_name');
if (empty($default)) {
$default = 'provisioning';
}
if ($this->isStandardMode()) {
return $default;
}
$pattern = '^[a-zA-Z0-9]*$';
$validator = function ($value) use ($pattern) {
return preg_match("/{$pattern}/", $value);
};
$message = 'What is your preferred name for the <fg=yellow;options=bold>provisioning folder</>?';
$errorMessage = "The name of the folder MUST respect {$pattern}.";
return $this->io->askQuestion($this->getQuestion($message, $default, $validator, $errorMessage));
}
/**
* @return string
*/
public function getComposeFileName()
{
$default = $this->projectConfiguration->get('docker.compose_file');
if (empty($default)) {
$default = 'docker-compose.yml';
}
if ($this->isStandardMode()) {
return $default;
}
$pattern = '^[a-zA-Z0-9\-]*\.yml$';
$validator = function ($value) use ($pattern) {
return preg_match("/{$pattern}/", $value);
};
$message = 'What is your preferred filename for the <fg=yellow;options=bold>Docker Compose file</>?';
$errorMessage = "The name of the filename MUST respect {$pattern}.";
return $this->io->askQuestion($this->getQuestion($message, $default, $validator, $errorMessage));
}
/**
* @param array $services
* @param array $questionnable
*
* @return array
*/
protected function getSelectedServices($services, $questionnable)
{
$selectedServices = [];
foreach ($services as $name => $service) {
if (in_array($name, $questionnable)) {
if ($this->isStandardMode() ||
$this->io->confirm("Do you want the service <fg=yellow;options=bold>{$name}</>")) {
$selectedServices[] = $name;
}
} else {
$selectedServices[] = $name;
}
}
return $selectedServices;
}
/**
* @return string
*/
protected function getNetworkName()
{
$default = str_replace(['-', '_', '.'], '', strtolower(basename(getcwd())));
$pattern = '^[a-zA-Z0-9]*$';
$validator = function ($value) use ($pattern) {
return preg_match("/{$pattern}/", $value);
};
if ($validator($default) && $this->isStandardMode()) {
return $default;
}
$message = 'Please select a name for the containers <fg=yellow;options=bold>Docker Network</>';
$errorMessage = "The name of the network MUST respect {$pattern}.";
return $this->io->askQuestion($this->getQuestion($message, $default, $validator, $errorMessage));
}
/**
* @return int
*/
protected function getNetworkTCPPort()
{
$default = 42;
$validator = function ($value) {
if (($value > 0) && ($value <= 65)) {
$socket = @fsockopen('127.0.0.1', intval("{$value}080"), $errno, $errstr, 5);
if ($socket) {
fclose($socket);
return false;
}
return true;
}
return false;
};
if ($validator($default) && $this->isStandardMode()) {
return $default;
}
$message = 'What is the <fg=yellow;options=bold>TCP Port Prefix</> you want?';
$errorMessage = 'The TCP Port Prefix is not correct (already used or not between 1 and 65.';
return (int) $this->io->askQuestion($this->getQuestion($message, $default, $validator, $errorMessage));
}
/**
* @param string $message
* @param null|string $default
* @param null|callable $validator
* @param string $exceptionMessage
*
* @return Question
*/
protected function getQuestion($message, $default = null, $validator = null, $exceptionMessage = 'Entry not valid')
{
$question = new Question($message, $default);
if (is_callable($validator)) {
$question->setValidator(
function ($value) use ($validator, $exceptionMessage) {
if (!$validator($value)) {
throw new \RuntimeException($exceptionMessage);
}
return $value;
}
);
}
return $question;
}
/**
* @return bool
*/
protected function isStandardMode()
{
return self::INIT_STD == $this->mode;
}
/**
* @return bool
*/
protected function requireComposerAuth()
{
return self::INIT_STD !== $this->mode;
}
}