1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* Copyright (C) 2024 Rafael San José <[email protected]> |
4
|
|
|
* |
5
|
|
|
* This program is free software; you can redistribute it and/or modify |
6
|
|
|
* it under the terms of the GNU General Public License as published by |
7
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
8
|
|
|
* any later version. |
9
|
|
|
* |
10
|
|
|
* This program is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU General Public License |
16
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Alxarafe\Base; |
20
|
|
|
|
21
|
|
|
use Alxarafe\Tools\Debug; |
22
|
|
|
use DebugBar\DataCollector\PDO\PDOCollector; |
23
|
|
|
use DebugBar\DebugBarException; |
24
|
|
|
use Illuminate\Database\Capsule\Manager as CapsuleManager; |
25
|
|
|
use PDO; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Create a PDO database connection |
29
|
|
|
* |
30
|
|
|
* @package Alxarafe\Base |
31
|
|
|
*/ |
32
|
|
|
class Database extends CapsuleManager |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* Construct the database access |
36
|
|
|
* |
37
|
|
|
* @param $db |
38
|
|
|
* |
39
|
|
|
* @throws DebugBarException |
40
|
|
|
*/ |
41
|
|
|
public function __construct($db) |
42
|
|
|
{ |
43
|
|
|
parent::__construct(); |
44
|
|
|
|
45
|
|
|
$this->addConnection([ |
46
|
|
|
'driver' => $db->type, |
47
|
|
|
'host' => $db->host, |
48
|
|
|
'database' => $db->name, |
49
|
|
|
'username' => $db->user, |
50
|
|
|
'password' => $db->pass, |
51
|
|
|
'charset' => $db->charset, |
52
|
|
|
'collation' => $db->collation, |
53
|
|
|
'prefix' => $db->prefix, |
54
|
|
|
]); |
55
|
|
|
|
56
|
|
|
$this->setAsGlobal(); |
57
|
|
|
$this->bootEloquent(); |
58
|
|
|
|
59
|
|
|
$debugBar = Debug::getDebugBar(); |
60
|
|
|
if ($debugBar->hasCollector('pdo')) { |
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$pdo = $this->getConnection()->getPdo(); |
65
|
|
|
$debugBar?->addCollector(new PDOCollector($pdo)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Checks if the connection to the database is possible with the parameters |
70
|
|
|
* defined in the configuration file. |
71
|
|
|
* |
72
|
|
|
* @param $data |
73
|
|
|
* @param bool $create |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
|
|
public static function checkDatabaseConnection($data, $create = false): bool |
77
|
|
|
{ |
78
|
|
|
if (!static::checkIfDatabaseExists($data)) { |
79
|
|
|
if (!$create) { |
80
|
|
|
return false; |
81
|
|
|
} |
82
|
|
|
if (!static::createDatabaseIfNotExists($data)) { |
83
|
|
|
return false; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public static function checkIfDatabaseExists(\stdClass $data): bool |
90
|
|
|
{ |
91
|
|
|
if (!static::checkConnection($data)) { |
92
|
|
|
return false; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$dsn = "$data->type:host=$data->host;dbname=$data->name;charset=$data->charset"; |
96
|
|
|
try { |
97
|
|
|
new PDO($dsn, $data->user, $data->pass); |
98
|
|
|
return true; |
99
|
|
|
} catch (\PDOException $e) { |
100
|
|
|
error_log($e->getMessage()); |
101
|
|
|
return false; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Checks if there is a connection to the database engine. |
107
|
|
|
* |
108
|
|
|
* @param $data |
109
|
|
|
* @return bool |
110
|
|
|
*/ |
111
|
|
|
public static function checkConnection(\stdClass $data): bool |
112
|
|
|
{ |
113
|
|
|
$dsn = "$data->type:host=$data->host"; |
114
|
|
|
try { |
115
|
|
|
new PDO($dsn, $data->user, $data->pass); |
116
|
|
|
return true; |
117
|
|
|
} catch (\PDOException $e) { |
118
|
|
|
error_log($e->getMessage()); |
119
|
|
|
return false; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public static function createDatabaseIfNotExists(\stdClass $data): bool |
124
|
|
|
{ |
125
|
|
|
if (static::checkIfDatabaseExists($data)) { |
126
|
|
|
return true; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$dsn = "$data->type:host=$data->host"; |
130
|
|
|
try { |
131
|
|
|
$pdo = new PDO($dsn, $data->user, $data->pass); |
132
|
|
|
$pdo->exec('CREATE DATABASE ' . $data->name); |
133
|
|
|
return true; |
134
|
|
|
} catch (\PDOException $e) { |
135
|
|
|
error_log($e->getMessage()); |
136
|
|
|
dump($e->getMessage()); |
137
|
|
|
return false; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|