1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Console\Commands; |
4
|
|
|
|
5
|
|
|
use DotEnvWriter\DotEnvWriter; |
6
|
|
|
use Illuminate\Console\Command; |
7
|
|
|
use Illuminate\Contracts\Console\Kernel; |
8
|
|
|
|
9
|
|
|
class Install extends Command |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The name and signature of the console command. |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $signature = 'myio:install'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The console command description. |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $description = 'Wizard to create the .env file'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* We will write the settings to this |
25
|
|
|
* file. |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $envFile = '.env'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Same environment file. |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $sampleEnv = '.env.example'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* These are the questions for creating |
38
|
|
|
* the environment file. |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
protected $steps = []; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* This array will contain the keys and values |
45
|
|
|
* we will write to the environment file. |
46
|
|
|
* @var array |
47
|
|
|
*/ |
48
|
|
|
protected $env = []; |
49
|
|
|
|
50
|
|
|
/** |
51
|
12 |
|
* Create a new command instance. |
52
|
|
|
*/ |
53
|
12 |
|
public function __construct() |
54
|
|
|
{ |
55
|
12 |
|
parent::__construct(); |
56
|
12 |
|
|
57
|
12 |
|
$this->env[] = [ |
58
|
12 |
|
'type' => 'value', |
59
|
|
|
'key' => 'APP_KEY', |
60
|
|
|
'value' => $this->createAppKey(), |
61
|
12 |
|
]; |
62
|
|
|
|
63
|
12 |
|
$this->steps = [ |
64
|
12 |
|
[ |
65
|
12 |
|
'question' => 'What environment will you run '.config('app.name').' in', |
66
|
6 |
|
'default' => 'local', |
67
|
12 |
|
'key' => 'APP_ENV', |
68
|
|
|
'answers' => ['local', 'production'], |
69
|
6 |
|
], |
70
|
6 |
|
[ |
71
|
6 |
|
'question' => 'Enable debugging? ', |
72
|
6 |
|
'default' => 'true', |
73
|
6 |
|
'key' => 'APP_DEBUG', |
74
|
|
|
'answers' => ['true', 'false'], |
75
|
12 |
|
], |
76
|
12 |
|
[ |
77
|
12 |
|
'question' => 'Logging level', |
78
|
6 |
|
'default' => config('app.log_level'), |
79
|
6 |
|
'key' => 'APP_LOG_LEVEL', |
80
|
|
|
'answers' => ['debug', 'info', 'notice', 'warning', 'error', 'critical', 'alert', 'emergency'], |
81
|
12 |
|
], |
82
|
12 |
|
[ |
83
|
12 |
|
'question' => 'Application url', |
84
|
6 |
|
'default' => config('app.url'), |
85
|
12 |
|
'key' => 'APP_URL', |
86
|
6 |
|
'answers' => [], |
87
|
|
|
'nextline' => 'separator', |
88
|
6 |
|
], |
89
|
6 |
|
[ |
90
|
6 |
|
'question' => 'Database connection', |
91
|
6 |
|
'default' => 'mysql', |
92
|
6 |
|
'key' => 'DB_CONNECTION', |
93
|
|
|
'answers' => ['mysql'], |
94
|
6 |
|
], |
95
|
6 |
|
[ |
96
|
6 |
|
'question' => 'Database host', |
97
|
6 |
|
'default' => '127.0.0.1', |
98
|
6 |
|
'key' => 'DB_HOST', |
99
|
|
|
'answers' => ['127.0.0.1', 'localhost'], |
100
|
6 |
|
], |
101
|
6 |
|
[ |
102
|
6 |
|
'question' => 'Database port', |
103
|
6 |
|
'default' => '3306', |
104
|
6 |
|
'key' => 'DB_PORT', |
105
|
|
|
'answers' => ['3306'], |
106
|
6 |
|
], |
107
|
6 |
|
[ |
108
|
6 |
|
'question' => 'Database name', |
109
|
6 |
|
'default' => 'myio', |
110
|
6 |
|
'key' => 'DB_DATABASE', |
111
|
|
|
'answers' => [], |
112
|
6 |
|
], |
113
|
6 |
|
[ |
114
|
6 |
|
'question' => 'Database user', |
115
|
6 |
|
'default' => 'root', |
116
|
6 |
|
'key' => 'DB_USERNAME', |
117
|
|
|
'answers' => [], |
118
|
6 |
|
], |
119
|
6 |
|
[ |
120
|
6 |
|
'question' => 'Database password', |
121
|
6 |
|
'default' => 'root', |
122
|
6 |
|
'key' => 'DB_PASSWORD', |
123
|
|
|
'answers' => [], |
124
|
12 |
|
'nextline' => 'separator', |
125
|
|
|
], |
126
|
|
|
[ |
127
|
|
|
'question' => 'Recaptcha public key', |
128
|
|
|
'default' => 'null', |
129
|
|
|
'key' => 'RECAPTCHA_PUBLIC_KEY', |
130
|
|
|
'answers' => [], |
131
|
|
|
], |
132
|
12 |
|
[ |
133
|
|
|
'question' => 'Recaptcha private key', |
134
|
12 |
|
'default' => 'null', |
135
|
|
|
'key' => 'RECAPTCHA_PRIVATE_KEY', |
136
|
|
|
'answers' => [], |
137
|
|
|
'nextline' => 'separator', |
138
|
|
|
], |
139
|
|
|
]; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Generate a unique APP key, This is |
144
|
|
|
* code i stole and modified from the |
145
|
|
|
* core. |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
|
|
private function createAppKey() |
149
|
|
|
{ |
150
|
|
|
return 'base64:'.base64_encode(random_bytes(config('app.cipher') == 'AES-128-CBC' ? 16 : 32)); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Execute the console command. |
155
|
|
|
* @return mixed |
156
|
|
|
*/ |
157
|
|
|
public function handle() |
158
|
|
|
{ |
159
|
|
|
$this->info('Welcome to '.config('app.name').'. We will get you started quickly by asking you a few questions.'); |
160
|
|
|
|
161
|
|
|
if (file_exists($this->envFile) == true) { |
|
|
|
|
162
|
|
|
if (($answer = $this->anticipate($this->envFile.' already exists, do you wish to continue?', ['yes', 'no'], 'no'))) { |
163
|
|
|
if ($answer == 'no') { |
164
|
|
|
$this->info("\r\nBye bye ... "); |
165
|
|
|
|
166
|
|
|
return; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
foreach ($this->steps as $step) { |
172
|
|
|
$repeat = true; |
173
|
|
|
while ($repeat) { |
174
|
|
|
$answer = $this->anticipate($step['question'], $step['answers'], $step['default']); |
175
|
|
|
|
176
|
|
|
if (count($step['answers']) > 0 && in_array($answer, array_values($step['answers'])) == false) { |
|
|
|
|
177
|
|
|
$repeat = true; |
178
|
|
|
$this->info('Your possible choices are: ['.implode(', ', $step['answers']).']'); |
179
|
|
|
} else { |
180
|
|
|
$repeat = false; |
181
|
|
|
$this->env[] = [ |
182
|
|
|
'type' => 'value', |
183
|
|
|
'key' => $step['key'], |
184
|
|
|
'value' => $answer, |
185
|
|
|
]; |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
if (isset($step['nextline']) == true && $step['nextline'] == 'separator') { |
|
|
|
|
190
|
|
|
$this->env[] = ['type' => 'separator']; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
$sampleEnv = file_get_contents($this->sampleEnv); |
195
|
|
|
|
196
|
|
|
if ($sampleEnv) { |
197
|
|
|
$lines = explode("\n", $sampleEnv); |
198
|
|
|
foreach ($lines as $line) { |
199
|
|
|
$line = trim($line); |
200
|
|
|
if (empty($line)) { |
201
|
|
|
$this->env[] = ['type' => 'separator']; |
202
|
|
|
} |
203
|
|
|
$line = explode('=', $line); |
204
|
|
|
if (isset($line[0]) == true && isset($line[1]) == true) { |
|
|
|
|
205
|
|
|
$this->env[] = [ |
206
|
|
|
'type' => 'value', |
207
|
|
|
'key' => $line[0], |
208
|
|
|
'value' => $line[1], |
209
|
|
|
]; |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
unset($sampleEnv); |
214
|
|
|
|
215
|
|
|
$written = []; |
216
|
|
|
|
217
|
|
|
if (count($this->env) > 1) { |
218
|
|
|
$writer = new DotEnvWriter($this->envFile); |
219
|
|
|
foreach ($this->env as $item) { |
220
|
|
|
if ($item['type'] == 'value') { |
221
|
|
|
if (isset($written[$item['key']])) { |
222
|
|
|
continue; |
223
|
|
|
} |
224
|
|
|
$writer->set($item['key'], $item['value']); |
225
|
|
|
$written[$item['key']] = $item['value']; |
226
|
|
|
} else { |
227
|
|
|
if ($item['type'] == 'separator') { |
228
|
|
|
$writer->line(); |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
$writer->save(); |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
app(Kernel::class)->call('migrate'); |
236
|
|
|
app(Kernel::class)->call('db:seed', ['--class' => 'UserRolesSeeder']); |
237
|
|
|
|
238
|
|
|
$this->info("\r\nHave fun ... "); |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.