1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Modules\Core\Console\Installers\Scripts; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Contracts\Config\Repository as Config; |
7
|
|
|
use Modules\Core\Console\Installers\SetupScript; |
8
|
|
|
use Modules\Core\Console\Installers\Writers\EnvFileWriter; |
9
|
|
|
use PDOException; |
10
|
|
|
|
11
|
|
|
class ConfigureDatabase implements SetupScript |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var |
15
|
|
|
*/ |
16
|
|
|
protected $config; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var EnvFileWriter |
20
|
|
|
*/ |
21
|
|
|
protected $env; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param Config $config |
25
|
|
|
* @param EnvFileWriter $env |
26
|
|
|
*/ |
27
|
|
|
public function __construct(Config $config, EnvFileWriter $env) |
28
|
|
|
{ |
29
|
|
|
$this->config = $config; |
30
|
|
|
$this->env = $env; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var Command |
35
|
|
|
*/ |
36
|
|
|
protected $command; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Fire the install script |
40
|
|
|
* @param Command $command |
41
|
|
|
* @return mixed |
42
|
|
|
*/ |
43
|
|
|
public function fire(Command $command) |
44
|
|
|
{ |
45
|
|
|
$this->command = $command; |
46
|
|
|
|
47
|
|
|
$connected = false; |
48
|
|
|
|
49
|
|
|
while (! $connected) { |
50
|
|
|
$driver = $this->askDatabaseDriver(); |
51
|
|
|
$host = $this->askDatabaseHost(); |
52
|
|
|
$port = $this->askDatabasePort($driver); |
53
|
|
|
$name = $this->askDatabaseName(); |
54
|
|
|
$user = $this->askDatabaseUsername(); |
55
|
|
|
$password = $this->askDatabasePassword(); |
56
|
|
|
|
57
|
|
|
$this->setLaravelConfiguration($driver, $host, $port, $name, $user, $password); |
58
|
|
|
|
59
|
|
|
if ($this->databaseConnectionIsValid()) { |
60
|
|
|
$connected = true; |
61
|
|
|
} else { |
62
|
|
|
$command->error("Please ensure your database credentials are valid."); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$this->env->write($name, $user, $password, $host); |
|
|
|
|
67
|
|
|
|
68
|
|
|
$command->info('Database successfully configured'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
protected function askDatabaseDriver() |
75
|
|
|
{ |
76
|
|
|
$driver = $this->command->ask('Enter your database driver (e.g. mysql, pgsql)', 'mysql'); |
77
|
|
|
return $driver; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
protected function askDatabaseHost() |
85
|
|
|
{ |
86
|
|
|
$host = $this->command->ask('Enter your database host', 'localhost'); |
87
|
|
|
|
88
|
|
|
return $host; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
protected function askDatabasePort($driver) |
95
|
|
|
{ |
96
|
|
|
$port = $this->command->ask('Enter your database port', $this->config['database.connections.'.$driver.'.port']); |
97
|
|
|
return $port; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
View Code Duplication |
protected function askDatabaseName() |
104
|
|
|
{ |
105
|
|
|
do { |
106
|
|
|
$name = $this->command->ask('Enter your database name', 'homestead'); |
107
|
|
|
if ($name == '') { |
108
|
|
|
$this->command->error('Database name is required'); |
109
|
|
|
} |
110
|
|
|
} while (!$name); |
111
|
|
|
|
112
|
|
|
return $name; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
|
View Code Duplication |
protected function askDatabaseUsername() |
120
|
|
|
{ |
121
|
|
|
do { |
122
|
|
|
$user = $this->command->ask('Enter your database username', 'homestead'); |
123
|
|
|
if ($user == '') { |
124
|
|
|
$this->command->error('Database username is required'); |
125
|
|
|
} |
126
|
|
|
} while (!$user); |
127
|
|
|
|
128
|
|
|
return $user; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
|
protected function askDatabasePassword() |
136
|
|
|
{ |
137
|
|
|
$databasePassword = $this->command->ask('Enter your database password (leave <none> for no password)', 'secret'); |
138
|
|
|
|
139
|
|
|
return ($databasePassword === '<none>') ? '' : $databasePassword; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param $driver |
144
|
|
|
* @param $name |
145
|
|
|
* @param $port |
146
|
|
|
* @param $user |
147
|
|
|
* @param $password |
148
|
|
|
*/ |
149
|
|
|
protected function setLaravelConfiguration($driver, $host, $port, $name, $user, $password) |
150
|
|
|
{ |
151
|
|
|
$this->config['database.default'] = $driver; |
152
|
|
|
$this->config['database.connections.'.$driver.'.host'] = $host; |
153
|
|
|
$this->config['database.connections.'.$driver.'.port'] = $port; |
154
|
|
|
$this->config['database.connections.'.$driver.'.database'] = $name; |
155
|
|
|
$this->config['database.connections.'.$driver.'.username'] = $user; |
156
|
|
|
$this->config['database.connections.'.$driver.'.password'] = $password; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Is the database connection valid? |
161
|
|
|
* @return bool |
162
|
|
|
*/ |
163
|
|
|
protected function databaseConnectionIsValid() |
164
|
|
|
{ |
165
|
|
|
try { |
166
|
|
|
app('db')->reconnect(); |
167
|
|
|
|
168
|
|
|
return true; |
169
|
|
|
} catch (PDOException $e) { |
170
|
|
|
return false; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: