|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace App\Component\Db; |
|
6
|
|
|
|
|
7
|
|
|
use PDO; |
|
8
|
|
|
use Nymfonya\Component\Config; |
|
9
|
|
|
use Nymfonya\Component\Container; |
|
10
|
|
|
use App\Component\Db\Pool; |
|
11
|
|
|
use Monolog\Logger; |
|
12
|
|
|
|
|
13
|
|
|
class Factory |
|
14
|
|
|
{ |
|
15
|
|
|
const _ADAPTER = 'adapter'; |
|
16
|
|
|
const LOGGER_PREFIX = 'Db Factory Pool : '; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* service container |
|
20
|
|
|
* |
|
21
|
|
|
* @var Container |
|
22
|
|
|
*/ |
|
23
|
|
|
private $container; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* factory config |
|
27
|
|
|
* |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
private $config; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* connection pool |
|
34
|
|
|
* |
|
35
|
|
|
* @var Pool |
|
36
|
|
|
*/ |
|
37
|
|
|
private $pool; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* logger |
|
41
|
|
|
* |
|
42
|
|
|
* @var Logger |
|
43
|
|
|
*/ |
|
44
|
|
|
private $logger; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* instanciate factory |
|
48
|
|
|
* |
|
49
|
|
|
* @param Container $container |
|
50
|
|
|
*/ |
|
51
|
8 |
|
public function __construct(Container $container) |
|
52
|
|
|
{ |
|
53
|
8 |
|
$this->container = $container; |
|
54
|
8 |
|
$configService = $this->container->getService(Config::class); |
|
55
|
8 |
|
$this->config = $configService->getSettings(Config::_DB); |
|
56
|
8 |
|
$this->logger = $this->container->getService(Logger::class); |
|
57
|
8 |
|
$this->pool = $this->container->getService(Pool::class); |
|
58
|
8 |
|
$this->logger->info(self::LOGGER_PREFIX . 'instance '); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* return Pdo connection instance from pool |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $slot |
|
65
|
|
|
* @param string $dbname |
|
66
|
|
|
* @return PDO |
|
67
|
|
|
*/ |
|
68
|
1 |
|
public function getConnection(string $slot, string $dbname): PDO |
|
69
|
|
|
{ |
|
70
|
1 |
|
$id = $this->identity($slot, $dbname); |
|
71
|
1 |
|
if (isset($this->pool[$id])) { |
|
72
|
1 |
|
return $this->pool[$id]; |
|
73
|
|
|
} |
|
74
|
1 |
|
$this->connect($slot, $dbname); |
|
75
|
1 |
|
return $this->pool[$id]; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* connect |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $slot |
|
82
|
|
|
* @param string $dbname |
|
83
|
|
|
* @return void |
|
84
|
|
|
*/ |
|
85
|
2 |
|
protected function connect(string $slot, string $dbname): Factory |
|
86
|
|
|
{ |
|
87
|
2 |
|
$params = $this->adapterParams($slot, $dbname); |
|
88
|
|
|
try { |
|
89
|
2 |
|
$adapterClass = $params[self::_ADAPTER]; |
|
90
|
2 |
|
$adapter = new $adapterClass($dbname, $params); |
|
91
|
2 |
|
$adapter->connect(); |
|
92
|
1 |
|
$id = $this->identity($slot, $dbname); |
|
93
|
1 |
|
$this->pool[$id] = $adapter->getConnection(); |
|
94
|
1 |
|
} catch (\PDOException $e) { |
|
95
|
1 |
|
$exMsg = self::LOGGER_PREFIX . ' connection failed'; |
|
96
|
1 |
|
$this->logger->warn($exMsg); |
|
97
|
1 |
|
throw new \Exception($exMsg, $e->getCode()); |
|
98
|
|
|
} |
|
99
|
1 |
|
return $this; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* adapter params |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $slot |
|
106
|
|
|
* @param string $dbname |
|
107
|
|
|
* @return array |
|
108
|
|
|
*/ |
|
109
|
2 |
|
protected function adapterParams(string $slot, string $dbname): array |
|
110
|
|
|
{ |
|
111
|
2 |
|
if (false === isset($this->config[$slot][$dbname])) { |
|
112
|
1 |
|
$exMsg = sprintf( |
|
113
|
1 |
|
'Missing or invalid config on slot %s for db %s', |
|
114
|
|
|
$slot, |
|
115
|
|
|
$dbname |
|
116
|
|
|
); |
|
117
|
1 |
|
$this->logger->warn($exMsg); |
|
118
|
1 |
|
throw new \Exception($exMsg); |
|
119
|
|
|
} |
|
120
|
1 |
|
return $this->config[$slot][$dbname]; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* identify pool index |
|
125
|
|
|
* |
|
126
|
|
|
* @param string $slot |
|
127
|
|
|
* @param string $dbname |
|
128
|
|
|
* @return string |
|
129
|
|
|
*/ |
|
130
|
1 |
|
protected function identity(string $slot, string $dbname): string |
|
131
|
|
|
{ |
|
132
|
1 |
|
return sprintf('%s-%s', $slot, $dbname); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* return pool service |
|
137
|
|
|
* |
|
138
|
|
|
* @return Pool |
|
139
|
|
|
*/ |
|
140
|
2 |
|
protected function getPool(): Pool |
|
141
|
|
|
{ |
|
142
|
2 |
|
return $this->pool; |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|