1
|
|
|
<?php |
2
|
|
|
namespace Mezon\PdoCrud; |
3
|
|
|
|
4
|
|
|
use Mezon\Conf\Conf; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class ConnectionTrait |
8
|
|
|
* |
9
|
|
|
* @package Mezon |
10
|
|
|
* @subpackage PdoCrud |
11
|
|
|
* @author Dodonov A.A. |
12
|
|
|
* @version v.1.0 (2020/02/19) |
13
|
|
|
* @copyright Copyright (c) 2020, aeon.org |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Trait for getting connections |
18
|
|
|
*/ |
19
|
|
|
trait ConnectionTrait |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Connection to DB. |
24
|
|
|
*/ |
25
|
|
|
protected static $crud = false; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Method validates dsn fields |
29
|
|
|
* |
30
|
|
|
* @param string $connectionName |
31
|
|
|
* Connectio name |
32
|
|
|
*/ |
33
|
|
|
protected function validateDsn(string $connectionName): void |
34
|
|
|
{ |
35
|
|
|
if (Conf::getConfigValue($connectionName . '/dsn') === false) { |
36
|
|
|
throw (new \Exception($connectionName . '/dsn not set')); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if (Conf::getConfigValue($connectionName . '/user') === false) { |
40
|
|
|
throw (new \Exception($connectionName . '/user not set')); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if (Conf::getConfigValue($connectionName . '/password') === false) { |
44
|
|
|
throw (new \Exception($connectionName . '/password not set')); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Method returns true if the connection exists, false otherwise |
50
|
|
|
* |
51
|
|
|
* @param string $connectionName |
52
|
|
|
* connection name |
53
|
|
|
* @return bool true if the connection exists, false otherwise |
54
|
|
|
*/ |
55
|
|
|
protected function dsnExists(string $connectionName): bool |
56
|
|
|
{ |
57
|
|
|
try { |
58
|
|
|
$this->validateDsn($connectionName); |
59
|
|
|
return true; |
60
|
|
|
} catch (\Exception $e) { |
61
|
|
|
return false; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Contructing connection to database object |
67
|
|
|
* |
68
|
|
|
* @return PdoCrud connection object wich is no initialized |
69
|
|
|
* @codeCoverageIgnore |
70
|
|
|
*/ |
71
|
|
|
protected function constructConnection(): PdoCrud |
72
|
|
|
{ |
73
|
|
|
return new PdoCrud(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Method returns database connection. |
78
|
|
|
* If you will pas array of connection names, then the first existing one will be returned |
79
|
|
|
* |
80
|
|
|
* @param string|array $connectionName |
81
|
|
|
* Connectioт name or array of connection names. |
82
|
|
|
* @return mixed connection |
83
|
|
|
*/ |
84
|
|
|
private function getConnectionScalar(string $connectionName = 'default-db-connection') |
85
|
|
|
{ |
86
|
|
|
$this->validateDsn($connectionName); |
87
|
|
|
|
88
|
|
|
self::$crud = $this->constructConnection(); |
89
|
|
|
|
90
|
|
|
self::$crud->connect( |
91
|
|
|
[ |
92
|
|
|
'dsn' => Conf::getConfigValue($connectionName . '/dsn'), |
93
|
|
|
'user' => Conf::getConfigValue($connectionName . '/user'), |
94
|
|
|
'password' => Conf::getConfigValue($connectionName . '/password') |
95
|
|
|
]); |
96
|
|
|
|
97
|
|
|
return self::$crud; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Method returns database connection. |
102
|
|
|
* If you will pas array of connection names, then the first existing one will be returned |
103
|
|
|
* |
104
|
|
|
* @param string|array $connectionName |
105
|
|
|
* Connectioт name or array of connection names. |
106
|
|
|
* @return mixed connection |
107
|
|
|
*/ |
108
|
|
|
public function getConnection($connectionName = 'default-db-connection') |
109
|
|
|
{ |
110
|
|
|
if (self::$crud !== false) { |
111
|
|
|
return self::$crud; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if (is_string($connectionName)) { |
115
|
|
|
return $this->getConnectionScalar($connectionName); |
116
|
|
|
} elseif (is_array($connectionName)) { |
|
|
|
|
117
|
|
|
foreach ($connectionName as $name) { |
118
|
|
|
if ($this->dsnExists($name)) { |
119
|
|
|
return $this->getConnectionScalar($name); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
throw (new \Exception('Connection with names: "' . implode(', ', $connectionName) . '" were not found')); |
124
|
|
|
} else { |
125
|
|
|
throw (new \Exception('Unsupported type for connection name')); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Method sets connection |
131
|
|
|
* |
132
|
|
|
* @param mixed $connection |
133
|
|
|
* - new connection or it's mock |
134
|
|
|
*/ |
135
|
|
|
public function setConnection($connection): void |
136
|
|
|
{ |
137
|
|
|
self::$crud = $connection; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|