|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* PDO connection class |
|
4
|
|
|
* |
|
5
|
|
|
* @file PdoConnection.php |
|
6
|
|
|
* |
|
7
|
|
|
* PHP version 8.0+ |
|
8
|
|
|
* |
|
9
|
|
|
* @author Alexander Yancharuk <alex at itvault dot info> |
|
10
|
|
|
* @copyright © 2012-2021 Alexander Yancharuk |
|
11
|
|
|
* @date 2013-12-31 15:44 |
|
12
|
|
|
* @license The BSD 3-Clause License |
|
13
|
|
|
* <https://tldrlegal.com/license/bsd-3-clause-license-(revised)> |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace Veles\DataBase\Connections; |
|
17
|
|
|
|
|
18
|
|
|
use Exception; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class PdoConnection |
|
22
|
|
|
* |
|
23
|
|
|
* Class-container for PDO connection and it's options |
|
24
|
|
|
* |
|
25
|
|
|
* @author Alexander Yancharuk <alex at itvault dot info> |
|
26
|
|
|
*/ |
|
27
|
|
|
class PdoConnection extends DbConnection |
|
28
|
|
|
{ |
|
29
|
|
|
/** @var string */ |
|
30
|
|
|
protected $dsn; |
|
31
|
|
|
/** @var array */ |
|
32
|
|
|
protected $options; |
|
33
|
|
|
/** @var array */ |
|
34
|
|
|
protected $callbacks = []; |
|
35
|
|
|
/** @var string */ |
|
36
|
|
|
protected $driver = '\PDO'; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Create connection |
|
40
|
|
|
* |
|
41
|
|
|
* @return \PDO |
|
42
|
|
|
* @throws Exception |
|
43
|
|
|
*/ |
|
44
|
2 |
|
public function create() |
|
45
|
|
|
{ |
|
46
|
2 |
|
$this->resource = new $this->driver( |
|
47
|
2 |
|
$this->getDsn(), $this->getUserName(), |
|
48
|
2 |
|
$this->getPassword(), $this->getOptions() |
|
49
|
2 |
|
); |
|
50
|
|
|
|
|
51
|
2 |
|
if ([] === $this->callbacks) { |
|
52
|
1 |
|
return $this->resource; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
1 |
|
foreach ($this->callbacks as $call) { |
|
56
|
1 |
|
call_user_func_array( |
|
57
|
1 |
|
[$this->resource, $call['method']], $call['arguments'] |
|
58
|
1 |
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
1 |
|
return $this->resource; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Set connection DSN (Data Source Name) |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $dsn |
|
68
|
|
|
* |
|
69
|
|
|
* @return $this |
|
70
|
|
|
*/ |
|
71
|
4 |
|
public function setDsn($dsn) |
|
72
|
|
|
{ |
|
73
|
4 |
|
$this->dsn = $dsn; |
|
74
|
4 |
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Get connection DSN (Data Source Name) |
|
79
|
|
|
* |
|
80
|
|
|
* @throws Exception |
|
81
|
|
|
* |
|
82
|
|
|
* @return string |
|
83
|
|
|
*/ |
|
84
|
4 |
|
public function getDsn() |
|
85
|
|
|
{ |
|
86
|
4 |
|
if (null === $this->dsn) { |
|
87
|
1 |
|
throw new Exception('Connection DSN not set.'); |
|
88
|
|
|
} |
|
89
|
3 |
|
return $this->dsn; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Set connection options |
|
94
|
|
|
* |
|
95
|
|
|
* @param array $options |
|
96
|
|
|
* |
|
97
|
|
|
* @return $this |
|
98
|
|
|
*/ |
|
99
|
2 |
|
public function setOptions(array $options) |
|
100
|
|
|
{ |
|
101
|
2 |
|
$this->options = $options; |
|
102
|
2 |
|
return $this; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Get connection options |
|
107
|
|
|
* |
|
108
|
|
|
* @throws Exception |
|
109
|
|
|
* |
|
110
|
|
|
* @return array |
|
111
|
|
|
*/ |
|
112
|
3 |
|
public function getOptions() |
|
113
|
|
|
{ |
|
114
|
3 |
|
return $this->options; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Get connection callbacks |
|
119
|
|
|
* |
|
120
|
|
|
* @return array |
|
121
|
|
|
*/ |
|
122
|
1 |
|
public function getCallbacks() |
|
123
|
|
|
{ |
|
124
|
1 |
|
return $this->callbacks; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Save calls for future invocation |
|
129
|
|
|
* |
|
130
|
|
|
* @param string $method Method name that should be called |
|
131
|
|
|
* @param array $arguments Method arguments |
|
132
|
|
|
* |
|
133
|
|
|
* @return $this |
|
134
|
|
|
*/ |
|
135
|
2 |
|
public function setCallback($method, array $arguments) |
|
136
|
|
|
{ |
|
137
|
2 |
|
$this->callbacks[] = [ |
|
138
|
2 |
|
'method' => $method, |
|
139
|
2 |
|
'arguments' => $arguments |
|
140
|
2 |
|
]; |
|
141
|
2 |
|
return $this; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|