|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace League\Database; |
|
4
|
|
|
|
|
5
|
|
|
use League\Database\Driver\Engine; |
|
6
|
|
|
use League\Database\Driver\TransactionPDO; |
|
7
|
|
|
|
|
8
|
|
|
final class ConnectionManager |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Master database connection |
|
12
|
|
|
* |
|
13
|
|
|
* @var TransactionPDO |
|
14
|
|
|
*/ |
|
15
|
|
|
private $masterConnection; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Slave database connection |
|
19
|
|
|
* |
|
20
|
|
|
* @var TransactionPDO|null |
|
21
|
|
|
*/ |
|
22
|
|
|
private $slaveConnection; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* PDOManager constructor |
|
26
|
|
|
* Initialize connection to Database for master and slave (if applicable) |
|
27
|
|
|
* |
|
28
|
|
|
* @param string $name Unique name for connection |
|
29
|
|
|
* @param array $config Configuration settings for database connection |
|
30
|
|
|
* @param array $options Some specific options |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(string $name, array $config, array $options = []) |
|
33
|
|
|
{ |
|
34
|
|
|
$masterConf = $config['master'] ?? $config; |
|
35
|
|
|
$slaveConf = $config['slave'] ?? []; |
|
36
|
|
|
|
|
37
|
|
|
$this->masterConnection = Engine::setConnection($name.'_master', $masterConf, $options); |
|
|
|
|
|
|
38
|
|
|
$this->slaveConnection = $slaveConf |
|
39
|
|
|
? Engine::setConnection($name.'_slave', $slaveConf, $options) |
|
40
|
|
|
: null; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Returns master database handler |
|
45
|
|
|
* |
|
46
|
|
|
* @return TransactionPDO |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getMasterConnection() : TransactionPDO |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->masterConnection; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Returns slave database handler |
|
55
|
|
|
* |
|
56
|
|
|
* @return TransactionPDO |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getSlaveConnection() : TransactionPDO |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->slaveConnection ?: $this->masterConnection; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.