1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DrMVC\Database\Drivers; |
4
|
|
|
|
5
|
|
|
use DrMVC\Config\ConfigInterface; |
6
|
|
|
use DrMVC\Database\Drivers\Interfaces\DriverInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Abstract class, in which implemented basic logic similar for all database drivers |
10
|
|
|
* @package DrMVC\Database\Drivers |
11
|
|
|
*/ |
12
|
|
|
abstract class Driver implements DriverInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var mixed |
16
|
|
|
*/ |
17
|
|
|
protected $_instance; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var ConfigInterface |
21
|
|
|
*/ |
22
|
|
|
private $_config; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Name of collection |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $_collection; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Driver constructor. |
32
|
|
|
* |
33
|
|
|
* @param ConfigInterface $config object with current configuration |
34
|
|
|
* @param string $collection name of collection |
35
|
|
|
*/ |
36
|
|
|
public function __construct(ConfigInterface $config, string $collection = null) |
37
|
|
|
{ |
38
|
|
|
$this->setConfig($config); |
39
|
|
|
|
40
|
|
|
// Set collection name if name is provided |
41
|
|
|
if (null !== $collection) { |
42
|
|
|
$this->setCollection($this->getParam('prefix') . $collection); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Initiate connection with database |
48
|
|
|
* |
49
|
|
|
* @return DriverInterface |
50
|
|
|
*/ |
51
|
|
|
abstract public function connect(): DriverInterface; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Close database connection |
55
|
|
|
* |
56
|
|
|
* @return DriverInterface |
57
|
|
|
*/ |
58
|
|
|
public function disconnect(): DriverInterface |
59
|
|
|
{ |
60
|
|
|
$this->setInstance(null); |
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Generate DSN by parameters in config |
66
|
|
|
* |
67
|
|
|
* @param $config |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
abstract protected function genDsn($config): string; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Generate DSN by data in config |
74
|
|
|
* |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
public function getDsn(): string |
78
|
|
|
{ |
79
|
|
|
// Get all parameters |
80
|
|
|
$config = $this->getConfig()->get(); |
81
|
|
|
|
82
|
|
|
// Generate DSN by parameters in config |
83
|
|
|
return $this->genDsn($config); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $collection |
88
|
|
|
* @return DriverInterface |
89
|
|
|
*/ |
90
|
|
|
public function setCollection(string $collection): DriverInterface |
91
|
|
|
{ |
92
|
|
|
$this->_collection = $collection; |
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
public function getCollection(): string |
100
|
|
|
{ |
101
|
|
|
return $this->_collection; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Save current config |
106
|
|
|
* |
107
|
|
|
* @param ConfigInterface $config |
108
|
|
|
* @return DriverInterface |
109
|
|
|
*/ |
110
|
|
|
public function setConfig(ConfigInterface $config): DriverInterface |
111
|
|
|
{ |
112
|
|
|
$this->_config = $config; |
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Return config object |
118
|
|
|
* |
119
|
|
|
* @return ConfigInterface |
120
|
|
|
*/ |
121
|
|
|
public function getConfig(): ConfigInterface |
122
|
|
|
{ |
123
|
|
|
return $this->_config; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get some parameter from config by keyname |
128
|
|
|
* |
129
|
|
|
* @param string $param |
130
|
|
|
* @return mixed |
131
|
|
|
*/ |
132
|
|
|
public function getParam(string $param) |
133
|
|
|
{ |
134
|
|
|
$result = $this->getConfig()->get($param); |
135
|
|
|
return $result ?? null; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Save connection with database via driver |
140
|
|
|
* |
141
|
|
|
* @param mixed $instance |
142
|
|
|
* @return DriverInterface |
143
|
|
|
*/ |
144
|
|
|
public function setInstance($instance): DriverInterface |
145
|
|
|
{ |
146
|
|
|
$this->_instance = $instance; |
147
|
|
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Get current connection |
152
|
|
|
* |
153
|
|
|
* @return mixed |
154
|
|
|
*/ |
155
|
|
|
abstract public function getInstance(); |
156
|
|
|
|
157
|
|
|
} |
158
|
|
|
|