1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace APP\Controllers; |
6
|
|
|
|
7
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
8
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
9
|
|
|
|
10
|
|
|
class Setup |
11
|
|
|
{ |
12
|
|
|
public function __construct($container) |
13
|
|
|
{ |
14
|
|
|
$this->container = $container; |
|
|
|
|
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
public function index(Request $request, Response $response) |
18
|
|
|
{ |
19
|
|
|
$driver = $this->container['config']->get('database.driver'); |
20
|
|
|
$host = $this->container['config']->get('database.drivers.mysql.host'); |
21
|
|
|
$database = $this->container['config']->get('database.drivers.mysql.database'); |
22
|
|
|
$username = $this->container['config']->get('database.drivers.mysql.username'); |
23
|
|
|
$password = $this->container['config']->get('database.drivers.mysql.password'); |
24
|
|
|
|
25
|
|
|
if (testDatabaseConnectivity($driver, $host, $database, $username, $password)) { |
26
|
|
|
return $response->withHeader('Location', resolveRoute('home')); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
$actionRoute = resolveRoute('setup'); |
30
|
|
|
$messages = $this->container['flash']->getMessages(); |
31
|
|
|
$error = false; |
32
|
|
|
|
33
|
|
|
if (isset($messages['error'])) { |
34
|
|
|
$error = true; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
ob_start(); |
38
|
|
|
include VIEWS.'setup.php'; |
39
|
|
|
$view = ob_get_contents(); |
40
|
|
|
ob_end_clean(); |
41
|
|
|
|
42
|
|
|
$response->write($view); |
43
|
|
|
|
44
|
|
|
return $response; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function update(Request $request, Response $response) |
48
|
|
|
{ |
49
|
|
|
$driver = $this->container['config']->get('database.driver'); |
50
|
|
|
|
51
|
|
|
$inputs = $request->getParsedBody(); |
52
|
|
|
|
53
|
|
|
$host = $inputs['host']; |
54
|
|
|
$database = $inputs['database']; |
55
|
|
|
$username = $inputs['username']; |
56
|
|
|
$password = $inputs['password']; |
57
|
|
|
|
58
|
|
|
// to avoid a strange behaviour the PDO's class make which is not throwing an exepction when the host is empty. |
59
|
|
|
// TO-DO: figure out what cause the problem really ! |
60
|
|
|
if (!$host) { |
61
|
|
|
$host = '0.0.0.0'; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if (!testDatabaseConnectivity($driver, $host, $database, $username, $password)) { |
65
|
|
|
$this->container['flash']->addMessage('error', true); |
66
|
|
|
$this->container['flash']->addMessage('old', $inputs); |
67
|
|
|
|
68
|
|
|
return $response->withHeader('Location', resolveRoute('setup')); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$this->container['config']->set('database.drivers.mysql.host', $host, true); |
72
|
|
|
$this->container['config']->set('database.drivers.mysql.database', $database, true); |
73
|
|
|
$this->container['config']->set('database.drivers.mysql.username', $username, true); |
74
|
|
|
$this->container['config']->set('database.drivers.mysql.password', $password, true); |
75
|
|
|
|
76
|
|
|
$this->container['config']->sync('database'); |
77
|
|
|
|
78
|
|
|
return $response->withHeader('Location', resolveRoute('home')); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: