1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Stonks\DataLayer; |
4
|
|
|
|
5
|
|
|
use PDO; |
6
|
|
|
use PDOException; |
7
|
|
|
use stdClass; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Connect |
11
|
|
|
* |
12
|
|
|
* @package Stonks\DataLayer |
13
|
|
|
*/ |
14
|
|
|
class Connect |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var PDO|stdClass|null |
19
|
|
|
*/ |
20
|
|
|
private static $instance; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var PDOException|stdClass|null |
24
|
|
|
*/ |
25
|
|
|
private static $error; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param string|null $database |
29
|
|
|
* @return PDO|null |
30
|
|
|
*/ |
31
|
|
|
public static function getInstance(?string $database = null): ?PDO |
32
|
|
|
{ |
33
|
|
|
if (empty(self::$instance)) { |
34
|
|
|
if (array_key_exists('driver', DATA_LAYER_CONFIG)) { |
35
|
|
|
self::getOnlyOneConnection(); |
36
|
|
|
} else { |
37
|
|
|
self::getMultipleConnections(); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if ($database && isset(self::$instance->$database)) { |
42
|
|
|
return self::$instance->$database; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return (self::$instance instanceof PDO ? self::$instance : null); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string|null $database |
50
|
|
|
* @return PDOException|null |
51
|
|
|
*/ |
52
|
|
|
public static function getError(?string $database = null): ?PDOException |
53
|
|
|
{ |
54
|
|
|
if ($database && isset(self::$error->$database)) { |
55
|
|
|
return self::$error->$database; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return (self::$error instanceof PDOException ? self::$error : null); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return void |
63
|
|
|
*/ |
64
|
|
|
private static function getOnlyOneConnection(): void |
65
|
|
|
{ |
66
|
|
|
try { |
67
|
|
|
self::$instance = new PDO( |
68
|
|
|
DATA_LAYER_CONFIG['driver'] . ':host=' . DATA_LAYER_CONFIG['host'] . ';dbname=' . DATA_LAYER_CONFIG['dbname'] . ';port=' . DATA_LAYER_CONFIG['port'], |
69
|
|
|
DATA_LAYER_CONFIG['username'], |
70
|
|
|
DATA_LAYER_CONFIG['passwd'], |
71
|
|
|
DATA_LAYER_CONFIG['options'] |
72
|
|
|
); |
73
|
|
|
} catch (PDOException $exception) { |
74
|
|
|
self::$error = $exception; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return void |
80
|
|
|
*/ |
81
|
|
|
private static function getMultipleConnections(): void |
82
|
|
|
{ |
83
|
|
|
self::$instance = new stdClass(); |
84
|
|
|
self::$error = new stdClass(); |
85
|
|
|
|
86
|
|
|
foreach (DATA_LAYER_CONFIG as $key => $config) { |
87
|
|
|
$dbname = (is_string($key) ? $key : $config['dbname']); |
88
|
|
|
|
89
|
|
|
try { |
90
|
|
|
self::$instance->$dbname = new PDO( |
91
|
|
|
"{$config['driver']}:host={$config['host']};dbname={$config['dbname']};port={$config['port']}", |
92
|
|
|
$config['username'], |
93
|
|
|
$config['passwd'], |
94
|
|
|
$config['options'] |
95
|
|
|
); |
96
|
|
|
} catch (PDOException $exception) { |
97
|
|
|
self::$error->$dbname = $exception; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string|null $database |
104
|
|
|
* @return PDO |
105
|
|
|
*/ |
106
|
|
|
public static function testConnection(?string $database = null): PDO |
107
|
|
|
{ |
108
|
|
|
$connect = self::getInstance($database); |
109
|
|
|
|
110
|
|
|
if (is_null($connect) || !$connect instanceof PDO) { |
111
|
|
|
throw (self::getError($database) ?? new PDOException("Unknown key or database '{$database}'")); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $connect; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Connect constructor. |
119
|
|
|
*/ |
120
|
|
|
final private function __construct() |
121
|
|
|
{ |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Connect clone. |
126
|
|
|
*/ |
127
|
|
|
final private function __clone() |
128
|
|
|
{ |
129
|
|
|
} |
130
|
|
|
} |