1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ez\DbLinker; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Doctrine\DBAL\Connection; |
7
|
|
|
use Doctrine\DBAL\DriverManager; |
8
|
|
|
|
9
|
|
|
abstract class ExtendedServer |
10
|
|
|
{ |
11
|
|
|
protected $host; |
12
|
|
|
protected $port; |
13
|
|
|
protected $user; |
14
|
|
|
protected $password; |
15
|
|
|
protected $dbname; |
16
|
|
|
protected $extraParams = []; |
17
|
|
|
protected $driver; |
18
|
|
|
protected $driverOptions; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var Connection |
22
|
|
|
*/ |
23
|
|
|
protected $connection; |
24
|
|
|
|
25
|
|
|
public function __construct(array $params) |
26
|
|
|
{ |
27
|
|
|
foreach ($params as $key => $value) { |
28
|
|
|
switch ($key) { |
29
|
|
|
case 'host': |
30
|
|
|
case 'port': |
31
|
|
|
case 'user': |
32
|
|
|
case 'password': |
33
|
|
|
case 'dbname': |
34
|
|
|
case 'weight': |
35
|
|
|
case 'driver': |
36
|
|
|
case 'driverOptions': |
37
|
|
|
$this->$key = $value; |
38
|
|
|
break; |
39
|
|
|
default: |
40
|
|
|
throw new \Exception($key . ' = ' . $value); |
41
|
|
|
$extraParams[$key] = $value; |
|
|
|
|
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function dbalConfig(): array { |
47
|
|
|
return [ |
48
|
|
|
'host' => $this->host, |
49
|
|
|
'port' => $this->port, |
50
|
|
|
'user' => $this->user, |
51
|
|
|
'password' => $this->password, |
52
|
|
|
'dbname' => $this->dbname, |
53
|
|
|
'driver' => $this->driver, |
54
|
|
|
'driverOptions' => $this->driverOptions, |
55
|
|
|
]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function isConnected(): bool { |
59
|
|
|
return $this->connection !== null; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function __toString() |
63
|
|
|
{ |
64
|
|
|
return get_class($this) . ' - ' . $this->user . '@' . $this->host . ':' . $this->port . '/' . $this->dbname; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function close() { |
68
|
|
|
if ($this->isConnected()) { |
69
|
|
|
// echo \get_class($this) . ' ' . $this . PHP_EOL; |
70
|
|
|
$this->connection->close(); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return \Doctrine\DBAL\Connection |
76
|
|
|
* @throws \Doctrine\DBAL\DBALException |
77
|
|
|
*/ |
78
|
|
|
public function connection(): Connection { |
79
|
|
|
if (!$this->connection) { |
80
|
|
|
$this->connection = DriverManager::getConnection($this->dbalConfig()); |
81
|
|
|
} |
82
|
|
|
return $this->connection; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function __destruct() |
86
|
|
|
{ |
87
|
|
|
$this->close(); |
88
|
|
|
} |
89
|
|
|
} |
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.