1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AsyncPHP\Icicle\Database\Connector; |
4
|
|
|
|
5
|
|
|
use AsyncPHP\Doorman\Handler; |
6
|
|
|
use AsyncPHP\Doorman\Task; |
7
|
|
|
use AsyncPHP\Remit\Client\ZeroMqClient; |
8
|
|
|
use AsyncPHP\Remit\Location\InMemoryLocation; |
9
|
|
|
use AsyncPHP\Remit\Server\ZeroMqServer; |
10
|
|
|
use Aura\Sql\ExtendedPdo; |
11
|
|
|
use Icicle\Loop; |
12
|
|
|
use PDO; |
13
|
|
|
|
14
|
|
|
final class DoormanConnectorHandler implements Handler |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @inheritdoc |
18
|
|
|
* |
19
|
|
|
* @param Task $task |
20
|
|
|
*/ |
21
|
|
|
public function handle(Task $task) |
22
|
|
|
{ |
23
|
|
|
$config = $task->getData(); |
24
|
|
|
|
25
|
|
|
if ($config["driver"] === "mysql") { |
26
|
|
|
$config += [ |
27
|
|
|
"host" => "127.0.0.1", |
28
|
|
|
"port" => 3306, |
29
|
|
|
"charset" => "utf8", |
30
|
|
|
"socket" => null, |
31
|
|
|
]; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
if ($config["remit"]["driver"] === "zeromq") { |
35
|
|
|
$config["remit"]["server"] += [ |
36
|
|
|
"host" => "127.0.0.1", |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
$config["remit"]["client"] += [ |
40
|
|
|
"host" => "127.0.0.1", |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
$server = new ZeroMqServer( |
44
|
|
|
new InMemoryLocation( |
45
|
|
|
$config["remit"]["client"]["host"], |
46
|
|
|
$config["remit"]["client"]["port"] |
47
|
|
|
) |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
$client = new ZeroMqClient( |
51
|
|
|
new InMemoryLocation( |
52
|
|
|
$config["remit"]["server"]["host"], |
53
|
|
|
$config["remit"]["server"]["port"] |
54
|
|
|
) |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$connection = new ExtendedPdo( |
59
|
|
|
new PDO($this->newConnectionString($config), $config["username"], $config["password"]) |
60
|
|
|
); |
61
|
|
|
|
62
|
|
|
$server->addListener("q", function ($query, $values, $id) use ($client, $connection) { |
|
|
|
|
63
|
|
|
$client->emit("r", [$connection->fetchAll($query, $values), $id]); |
64
|
|
|
}); |
65
|
|
|
|
66
|
|
|
Loop\periodic(0, function () use ($server) { |
|
|
|
|
67
|
|
|
$server->tick(); |
68
|
|
|
}); |
69
|
|
|
|
70
|
|
|
Loop\run(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Returns a new dsn string, for PDO to connect to the database with. |
75
|
|
|
* |
76
|
|
|
* @param array $config |
77
|
|
|
* |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
private function newConnectionString(array $config) |
81
|
|
|
{ |
82
|
|
|
if ($config["driver"] === "mysql") { |
83
|
|
|
return "mysql:host={$config['host']};port={$config['port']};dbname={$config['database']};unix_socket={$config['socket']};charset={$config['charset']}"; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if ($config["driver"] === "pgsql") { |
87
|
|
|
return "pgsql:host={$config['host']};port={$config['port']};dbname={$config['database']}"; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if ($config["driver"] === "sqlite") { |
91
|
|
|
return "sqlite:{$config['file']}"; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if ($config["driver"] === "sqlsrv") { |
95
|
|
|
return "sqlsrv:Server={$config['host']},{$config['port']};Database={$config['database']}"; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: