|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace App\Component\Db; |
|
6
|
|
|
|
|
7
|
|
|
use ArrayAccess; |
|
8
|
|
|
use Countable; |
|
9
|
|
|
use PDO; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Pool is a storage to store object identified by a string key |
|
13
|
|
|
*/ |
|
14
|
|
|
class Pool implements ArrayAccess, Countable |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* connections |
|
19
|
|
|
* |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
private $connections = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Assigns a value to the specified offset |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $connexionId |
|
28
|
|
|
* @param PDO $connection |
|
29
|
|
|
* @return void |
|
30
|
|
|
*/ |
|
31
|
1 |
|
public function offsetSet($connexionId, $connection) |
|
32
|
|
|
{ |
|
33
|
1 |
|
if ($this->valid($connexionId, $connection)) { |
|
34
|
1 |
|
$this->connections[$connexionId] = $connection; |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Whether or not an offset exists |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $connexionId |
|
42
|
|
|
* @return boolean |
|
43
|
|
|
*/ |
|
44
|
1 |
|
public function offsetExists($connexionId) |
|
45
|
|
|
{ |
|
46
|
1 |
|
return isset($this->connections[$connexionId]); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Unsets an offset |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $connexionId |
|
53
|
|
|
* @return void |
|
54
|
|
|
*/ |
|
55
|
1 |
|
public function offsetUnset($connexionId) |
|
56
|
|
|
{ |
|
57
|
1 |
|
if ($this->offsetExists($connexionId)) { |
|
58
|
1 |
|
unset($this->connections[$connexionId]); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Returns the value at specified offset |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $connexionId |
|
66
|
|
|
* @return PDO | null |
|
67
|
|
|
*/ |
|
68
|
1 |
|
public function offsetGet($connexionId) |
|
69
|
|
|
{ |
|
70
|
1 |
|
return $this->offsetExists($connexionId) |
|
71
|
1 |
|
? $this->connections[$connexionId] |
|
72
|
1 |
|
: null; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* return number of connections |
|
77
|
|
|
* |
|
78
|
|
|
* @return integer |
|
79
|
|
|
*/ |
|
80
|
1 |
|
public function count() |
|
81
|
|
|
{ |
|
82
|
1 |
|
return count($this->connections); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* return true id connectionId is not null and is string |
|
87
|
|
|
* and connection is object |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $connexionId |
|
90
|
|
|
* @param PDO | null $connection |
|
91
|
|
|
* @return boolean |
|
92
|
|
|
*/ |
|
93
|
23 |
|
protected function valid($connexionId, $connection): bool |
|
94
|
|
|
{ |
|
95
|
23 |
|
return (!is_null($connexionId) |
|
96
|
23 |
|
&& is_string($connexionId) |
|
97
|
23 |
|
&& is_object($connection)); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|