1
|
|
|
<?php namespace Magestead\Helper; |
2
|
|
|
|
3
|
|
|
use Symfony\Component\Console\Question\Question; |
4
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
5
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
6
|
|
|
use Symfony\Component\Console\Question\ChoiceQuestion; |
7
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Options |
11
|
|
|
* @package Magestead\Helper |
12
|
|
|
*/ |
13
|
|
|
class Options |
14
|
|
|
{ |
15
|
|
|
const BOX_PREFIX = 'richdynamix/magestead-'; |
16
|
|
|
|
17
|
|
|
protected $_app = 'magento2'; |
18
|
|
|
protected $_phpVer = '56'; |
19
|
|
|
protected $_os = 'centos65'; |
20
|
|
|
protected $_server; |
21
|
|
|
protected $_box; |
22
|
|
|
protected $_m2Username; |
23
|
|
|
protected $_m2Password; |
24
|
|
|
protected $_ipAddress; |
25
|
|
|
protected $_memorylimit; |
26
|
|
|
protected $_cpus; |
27
|
|
|
protected $_locale; |
28
|
|
|
protected $_currency; |
29
|
|
|
protected $_baseUrl; |
30
|
|
|
protected $_repoUrl = ''; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Options constructor. |
34
|
|
|
* @param $helper |
35
|
|
|
* @param InputInterface $input |
36
|
|
|
* @param OutputInterface $output |
37
|
|
|
* @param $project |
38
|
|
|
*/ |
39
|
|
|
public function __construct($helper, InputInterface $input, OutputInterface $output, $project) |
40
|
|
|
{ |
41
|
|
|
$this->setVagrantSettings($helper, $input, $output); |
42
|
|
|
|
43
|
|
|
$this->setServerConfig($helper, $input, $output); |
44
|
|
|
|
45
|
|
|
$this->setApplicationSettings($helper, $input, $output, $project); |
46
|
|
|
$this->setMagento2Settings($helper, $input, $output); |
47
|
|
|
$this->setVersionControlSettings($helper, $input, $output); |
48
|
|
|
|
49
|
|
|
$this->setVagrantBox(); |
50
|
|
|
|
51
|
|
|
} |
52
|
|
|
/** |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
|
|
public function getAllOptions() |
56
|
|
|
{ |
57
|
|
|
return [ |
58
|
|
|
'app' => $this->_app, |
59
|
|
|
'server' => $this->_server, |
60
|
|
|
'phpver' => $this->_phpVer, |
61
|
|
|
'os' => $this->_os, |
62
|
|
|
'box' => $this->_box, |
63
|
|
|
'm2user' => $this->_m2Username, |
64
|
|
|
'm2pass' => $this->_m2Password, |
65
|
|
|
'repo_url' => $this->_repoUrl, |
66
|
|
|
'ip_address' => $this->_ipAddress, |
67
|
|
|
'cpus' => $this->_cpus, |
68
|
|
|
'memory_limit' => $this->_memorylimit, |
69
|
|
|
'locale' => $this->_locale, |
70
|
|
|
'default_currency' => $this->_currency, |
71
|
|
|
'base_url' => $this->_baseUrl, |
72
|
|
|
]; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param $helper |
77
|
|
|
* @param InputInterface $input |
78
|
|
|
* @param OutputInterface $output |
79
|
|
|
*/ |
80
|
|
|
protected function setVagrantSettings($helper, InputInterface $input, OutputInterface $output) |
81
|
|
|
{ |
82
|
|
|
$output->writeln('<comment>Lets configure your project\'s VM</comment>'); |
83
|
|
|
|
84
|
|
|
$ipQuestion = new Question("Configure the IP for your VM (192.168.47.47): ", '192.168.47.47'); |
85
|
|
|
$this->_ipAddress = strtolower($helper->ask($input, $output, $ipQuestion)); |
86
|
|
|
|
87
|
|
|
$cpuQuestion = new Question("How many CPU's would you like to use? (1): ", '1'); |
88
|
|
|
$this->_cpus = strtolower($helper->ask($input, $output, $cpuQuestion)); |
89
|
|
|
|
90
|
|
|
$memoryQuestion = new Question("Define the VM memory limit (2048): ", '2048'); |
91
|
|
|
$this->_memorylimit = strtolower($helper->ask($input, $output, $memoryQuestion)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param $helper |
96
|
|
|
* @param InputInterface $input |
97
|
|
|
* @param OutputInterface $output |
98
|
|
|
* @param $project |
99
|
|
|
*/ |
100
|
|
|
protected function setApplicationSettings($helper, InputInterface $input, OutputInterface $output, $project) |
101
|
|
|
{ |
102
|
|
|
$output->writeln('<comment>Lets configure your project\'s application</comment>'); |
103
|
|
|
$appQuestion = new ChoiceQuestion( |
104
|
|
|
"Which application do you want to install?", |
105
|
|
|
['Magento', 'Magento2'], |
106
|
|
|
0 |
107
|
|
|
); |
108
|
|
|
|
109
|
|
|
$this->_app = strtolower($helper->ask($input, $output, $appQuestion)); |
110
|
|
|
|
111
|
|
|
$baseUrlQuestion = new Question("Enter your application's base_url ($project.dev): ", $project.'.dev'); |
112
|
|
|
$this->_baseUrl = strtolower($helper->ask($input, $output, $baseUrlQuestion)); |
113
|
|
|
|
114
|
|
|
$currenyQuestion = new Question("Enter your application's default currency (GBP): ", 'GBP'); |
115
|
|
|
$this->_currency = $helper->ask($input, $output, $currenyQuestion); |
116
|
|
|
|
117
|
|
|
$localeQuestion = new Question("Enter your application's default locale (en_GB): ", 'en_GB'); |
118
|
|
|
$this->_locale = $helper->ask($input, $output, $localeQuestion); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param $helper |
123
|
|
|
* @param InputInterface $input |
124
|
|
|
* @param OutputInterface $output |
125
|
|
|
* @return boolean|integer |
126
|
|
|
*/ |
127
|
|
|
protected function setMagento2Settings($helper, InputInterface $input, OutputInterface $output) |
128
|
|
|
{ |
129
|
|
|
if ($this->_app === 'magento2') { |
130
|
|
|
return $this->verifyAuth($helper, $input, $output); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return true; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param $helper |
138
|
|
|
* @param InputInterface $input |
139
|
|
|
* @param OutputInterface $output |
140
|
|
|
*/ |
141
|
|
|
protected function setVersionControlSettings($helper, InputInterface $input, OutputInterface $output) |
142
|
|
|
{ |
143
|
|
|
$versionControl = new ConfirmationQuestion("Would you like to add your project to GIT? (no/yes) ", false); |
144
|
|
|
$versioning = $helper->ask($input, $output, $versionControl); |
145
|
|
|
if ($versioning) { |
146
|
|
|
$repoQuestion = new Question("Enter your full GitHub/BitBucket repo URL: ", ''); |
147
|
|
|
$this->_repoUrl = strtolower($helper->ask($input, $output, $repoQuestion)); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param $helper |
153
|
|
|
* @param InputInterface $input |
154
|
|
|
* @param OutputInterface $output |
155
|
|
|
*/ |
156
|
|
|
protected function askForAuth($helper, InputInterface $input, OutputInterface $output) |
157
|
|
|
{ |
158
|
|
|
$username = new Question("Please enter your Magento username (public key): ", ''); |
159
|
|
|
$this->_m2Username = $helper->ask($input, $output, $username); |
160
|
|
|
|
161
|
|
|
$password = new Question("Please enter your Magento password (private key): ", ''); |
162
|
|
|
$this->_m2Password = $helper->ask($input, $output, $password); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param $helper |
167
|
|
|
* @param InputInterface $input |
168
|
|
|
* @param OutputInterface $output |
169
|
|
|
* @return boolean|integer |
170
|
|
|
*/ |
171
|
|
|
protected function verifyAuth($helper, InputInterface $input, OutputInterface $output) |
|
|
|
|
172
|
|
|
{ |
173
|
|
|
$authFile = $_SERVER['HOME'] . "/.composer/auth.json"; |
174
|
|
|
|
175
|
|
|
$authObj = []; |
176
|
|
|
if (file_exists($authFile)) { |
177
|
|
|
$authJson = file_get_contents($authFile); |
178
|
|
|
$authObj = (array)json_decode($authJson); |
179
|
|
|
|
180
|
|
|
if (isset($authObj['http-basic']) && isset($authObj['http-basic']->{'repo.magento.com'})) { |
181
|
|
|
return true; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
$this->askForAuth($helper, $input, $output); |
186
|
|
|
|
187
|
|
|
$authObj['http-basic']['repo.magento.com']['username'] = $this->_m2Username; |
188
|
|
|
$authObj['http-basic']['repo.magento.com']['password'] = $this->_m2Password; |
189
|
|
|
|
190
|
|
|
$authJson = json_encode($authObj); |
191
|
|
|
return file_put_contents($authFile, $authJson); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param $helper |
196
|
|
|
* @param InputInterface $input |
197
|
|
|
* @param OutputInterface $output |
198
|
|
|
*/ |
199
|
|
|
protected function setPhp($helper, InputInterface $input, OutputInterface $output) |
200
|
|
|
{ |
201
|
|
|
$output->writeln('<info>Keep in mind PHP7 is only available for Magento 2</info>'); |
202
|
|
|
$phpVerQuestion = new ChoiceQuestion( |
203
|
|
|
"Which version of PHP should be installed?", |
204
|
|
|
['56', '70'], |
205
|
|
|
0 |
206
|
|
|
); |
207
|
|
|
|
208
|
|
|
$this->_phpVer = $helper->ask($input, $output, $phpVerQuestion); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Set box name from concat user options |
213
|
|
|
*/ |
214
|
|
|
protected function setVagrantBox() |
215
|
|
|
{ |
216
|
|
|
$this->_box = self::BOX_PREFIX . $this->_os . "-$this->_server-php$this->_phpVer"; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @param $helper |
221
|
|
|
* @param InputInterface $input |
222
|
|
|
* @param OutputInterface $output |
223
|
|
|
*/ |
224
|
|
|
protected function setServerConfig($helper, InputInterface $input, OutputInterface $output) |
225
|
|
|
{ |
226
|
|
|
$output->writeln('<comment>Lets configure your server</comment>'); |
227
|
|
|
$this->setOperatingSystem($helper, $input, $output); |
228
|
|
|
$this->setWebServer($helper, $input, $output); |
229
|
|
|
$this->setPhp($helper, $input, $output); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @param $helper |
234
|
|
|
* @param InputInterface $input |
235
|
|
|
* @param OutputInterface $output |
236
|
|
|
*/ |
237
|
|
|
protected function setWebServer($helper, InputInterface $input, OutputInterface $output) |
238
|
|
|
{ |
239
|
|
|
$serverQuestion = new ChoiceQuestion( |
240
|
|
|
"Which webserver would you like?", |
241
|
|
|
['NGINX', 'Apache'], |
242
|
|
|
0 |
243
|
|
|
); |
244
|
|
|
|
245
|
|
|
$this->_server = strtolower($helper->ask($input, $output, $serverQuestion)); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @param $helper |
250
|
|
|
* @param InputInterface $input |
251
|
|
|
* @param OutputInterface $output |
252
|
|
|
*/ |
253
|
|
|
protected function setOperatingSystem($helper, InputInterface $input, OutputInterface $output) |
254
|
|
|
{ |
255
|
|
|
$osQuestion = new ChoiceQuestion( |
256
|
|
|
"Which OS would you like to install?", |
257
|
|
|
['CentOS 6.5', 'Ubuntu 14'], |
258
|
|
|
0 |
259
|
|
|
); |
260
|
|
|
|
261
|
|
|
$this->_os = str_replace(' ', '', str_replace('.', '', strtolower($helper->ask($input, $output, $osQuestion)))); |
262
|
|
|
} |
263
|
|
|
} |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: