|
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
|
|
|
* Class provides simple CRUD operations |
|
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
|
|
|
if (self::$crud !== false) { |
|
87
|
|
|
return self::$crud; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$this->validateDsn($connectionName); |
|
91
|
|
|
|
|
92
|
|
|
self::$crud = $this->constructConnection(); |
|
93
|
|
|
|
|
94
|
|
|
self::$crud->connect( |
|
95
|
|
|
[ |
|
96
|
|
|
'dsn' => Conf::getConfigValue($connectionName . '/dsn'), |
|
97
|
|
|
'user' => Conf::getConfigValue($connectionName . '/user'), |
|
98
|
|
|
'password' => Conf::getConfigValue($connectionName . '/password') |
|
99
|
|
|
]); |
|
100
|
|
|
|
|
101
|
|
|
return self::$crud; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Method returns database connection. |
|
106
|
|
|
* If you will pas array of connection names, then the first existing one will be returned |
|
107
|
|
|
* |
|
108
|
|
|
* @param string|array $connectionName |
|
109
|
|
|
* Connectioт name or array of connection names. |
|
110
|
|
|
* @return mixed connection |
|
111
|
|
|
*/ |
|
112
|
|
|
public function getConnection($connectionName = 'default-db-connection') |
|
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 name "' . serialize($connectionName) . '" was 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
|
|
|
|