1
|
|
|
<?php namespace Modules\Core\Console\Installers\Scripts; |
2
|
|
|
|
3
|
|
|
use Illuminate\Console\Command; |
4
|
|
|
use Illuminate\Contracts\Config\Repository as Config; |
5
|
|
|
use Modules\Core\Console\Installers\SetupScript; |
6
|
|
|
use Modules\Core\Console\Installers\Writers\EnvFileWriter; |
7
|
|
|
use PDOException; |
8
|
|
|
|
9
|
|
|
class ConfigureDatabase implements SetupScript |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var |
13
|
|
|
*/ |
14
|
|
|
protected $config; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var EnvFileWriter |
18
|
|
|
*/ |
19
|
|
|
protected $env; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param Config $config |
23
|
|
|
* @param EnvFileWriter $env |
24
|
|
|
*/ |
25
|
|
|
public function __construct(Config $config, EnvFileWriter $env) |
26
|
|
|
{ |
27
|
|
|
$this->config = $config; |
28
|
|
|
$this->env = $env; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var Command |
33
|
|
|
*/ |
34
|
|
|
protected $command; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Fire the install script |
38
|
|
|
* @param Command $command |
39
|
|
|
* @return mixed |
40
|
|
|
*/ |
41
|
|
|
public function fire(Command $command) |
42
|
|
|
{ |
43
|
|
|
$this->command = $command; |
44
|
|
|
|
45
|
|
|
$connected = false; |
46
|
|
|
|
47
|
|
|
while (! $connected) { |
48
|
|
|
$host = $this->askDatabaseHost(); |
49
|
|
|
|
50
|
|
|
$name = $this->askDatabaseName(); |
51
|
|
|
|
52
|
|
|
$user = $this->askDatabaseUsername(); |
53
|
|
|
|
54
|
|
|
$password = $this->askDatabasePassword(); |
55
|
|
|
|
56
|
|
|
$this->setLaravelConfiguration($name, $user, $password, $host); |
57
|
|
|
|
58
|
|
|
if ($this->databaseConnectionIsValid()) { |
59
|
|
|
$connected = true; |
60
|
|
|
} else { |
61
|
|
|
$command->error("Please ensure your database credentials are valid."); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$this->env->write($name, $user, $password, $host); |
|
|
|
|
66
|
|
|
|
67
|
|
|
$command->info('Database successfully configured'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
protected function askDatabaseHost() |
74
|
|
|
{ |
75
|
|
|
$host = $this->command->ask('Enter your database host', 'localhost'); |
76
|
|
|
|
77
|
|
|
return $host; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
View Code Duplication |
protected function askDatabaseName() |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
do { |
86
|
|
|
$name = $this->command->ask('Enter your database name'); |
87
|
|
|
if ($name == '') { |
88
|
|
|
$this->command->error('Database name is required'); |
89
|
|
|
} |
90
|
|
|
} while (!$name); |
91
|
|
|
|
92
|
|
|
return $name; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
View Code Duplication |
protected function askDatabaseUsername() |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
do { |
102
|
|
|
$user = $this->command->ask('Enter your database username', 'root'); |
103
|
|
|
if ($user == '') { |
104
|
|
|
$this->command->error('Database username is required'); |
105
|
|
|
} |
106
|
|
|
} while (!$user); |
107
|
|
|
|
108
|
|
|
return $user; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
protected function askDatabasePassword() |
116
|
|
|
{ |
117
|
|
|
$databasePassword = $this->command->ask('Enter your database password (leave <none> for no password)', '<none>'); |
118
|
|
|
|
119
|
|
|
return ($databasePassword === '<none>') ? '' : $databasePassword; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param $name |
124
|
|
|
* @param $user |
125
|
|
|
* @param $password |
126
|
|
|
*/ |
127
|
|
|
protected function setLaravelConfiguration($name, $user, $password, $host) |
128
|
|
|
{ |
129
|
|
|
$this->config['database.connections.mysql.host'] = $host; |
130
|
|
|
$this->config['database.connections.mysql.database'] = $name; |
131
|
|
|
$this->config['database.connections.mysql.username'] = $user; |
132
|
|
|
$this->config['database.connections.mysql.password'] = $password; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Is the database connection valid? |
137
|
|
|
* @return bool |
138
|
|
|
*/ |
139
|
|
|
protected function databaseConnectionIsValid() |
140
|
|
|
{ |
141
|
|
|
try { |
142
|
|
|
app('db')->reconnect(); |
143
|
|
|
|
144
|
|
|
return true; |
145
|
|
|
} catch (PDOException $e) { |
146
|
|
|
return false; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
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: