1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Database\Connectors; |
4
|
|
|
|
5
|
|
|
use PDO; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Connector |
9
|
|
|
* @package Nip\Database\Connectors |
10
|
|
|
*/ |
11
|
|
|
class Connector |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The default PDO connection options. |
16
|
|
|
* |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $options = [ |
20
|
|
|
PDO::ATTR_CASE => PDO::CASE_NATURAL, |
21
|
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
22
|
|
|
PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL, |
23
|
|
|
PDO::ATTR_STRINGIFY_FETCHES => false, |
24
|
|
|
PDO::ATTR_EMULATE_PREPARES => false, |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Create a new PDO connection. |
29
|
|
|
* |
30
|
|
|
* @param string $dsn |
31
|
|
|
* @param array $config |
32
|
|
|
* @param array $options |
33
|
|
|
* @return \PDO |
34
|
|
|
* |
35
|
|
|
* @throws \Exception |
36
|
|
|
*/ |
37
|
|
|
public function createConnection($dsn, array $config, array $options) |
38
|
|
|
{ |
39
|
|
|
[$username, $password] = [ |
|
|
|
|
40
|
|
|
$config['username'] ?? null, |
41
|
|
|
$config['password'] ?? null, |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
try { |
45
|
|
|
return $this->createPdoConnection( |
46
|
|
|
$dsn, $username, $password, $options |
47
|
|
|
); |
48
|
|
|
} catch (Exception $e) { |
|
|
|
|
49
|
|
|
return $this->tryAgainIfCausedByLostConnection( |
50
|
|
|
$e, $dsn, $username, $password, $options |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Create a new PDO connection instance. |
57
|
|
|
* |
58
|
|
|
* @param string $dsn |
59
|
|
|
* @param string $username |
60
|
|
|
* @param string $password |
61
|
|
|
* @param array $options |
62
|
|
|
* @return \PDO |
63
|
|
|
*/ |
64
|
|
|
protected function createPdoConnection($dsn, $username, $password, $options) |
65
|
|
|
{ |
66
|
|
|
if (class_exists(PDOConnection::class) && !$this->isPersistentConnection($options)) { |
67
|
|
|
return new PDOConnection($dsn, $username, $password, $options); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return new PDO($dsn, $username, $password, $options); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Determine if the connection is persistent. |
75
|
|
|
* |
76
|
|
|
* @param array $options |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
|
|
protected function isPersistentConnection($options) |
80
|
|
|
{ |
81
|
|
|
return isset($options[PDO::ATTR_PERSISTENT]) && |
82
|
|
|
$options[PDO::ATTR_PERSISTENT]; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Handle an exception that occurred during connect execution. |
87
|
|
|
* |
88
|
|
|
* @param \Throwable $e |
89
|
|
|
* @param string $dsn |
90
|
|
|
* @param string $username |
91
|
|
|
* @param string $password |
92
|
|
|
* @param array $options |
93
|
|
|
* @return \PDO |
94
|
|
|
* |
95
|
|
|
* @throws \Exception |
96
|
|
|
*/ |
97
|
|
|
protected function tryAgainIfCausedByLostConnection(Throwable $e, $dsn, $username, $password, $options) |
98
|
|
|
{ |
99
|
|
|
if ($this->causedByLostConnection($e)) { |
|
|
|
|
100
|
|
|
return $this->createPdoConnection($dsn, $username, $password, $options); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
throw $e; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get the PDO options based on the configuration. |
108
|
|
|
* |
109
|
|
|
* @param array $config |
110
|
|
|
* @return array |
111
|
|
|
*/ |
112
|
|
|
public function getOptions(array $config) |
113
|
|
|
{ |
114
|
|
|
$options = $config['options'] ?? []; |
115
|
|
|
|
116
|
|
|
return array_diff_key($this->options, $options) + $options; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get the default PDO connection options. |
121
|
|
|
* |
122
|
|
|
* @return array |
123
|
|
|
*/ |
124
|
|
|
public function getDefaultOptions() |
125
|
|
|
{ |
126
|
|
|
return $this->options; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Set the default PDO connection options. |
131
|
|
|
* |
132
|
|
|
* @param array $options |
133
|
|
|
* @return void |
134
|
|
|
*/ |
135
|
|
|
public function setDefaultOptions(array $options) |
136
|
|
|
{ |
137
|
|
|
$this->options = $options; |
138
|
|
|
} |
139
|
|
|
} |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.