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; |
8
|
|
|
use AsyncPHP\Remit\Client\ZeroMqClient; |
9
|
|
|
use AsyncPHP\Remit\Location\InMemoryLocation; |
10
|
|
|
use AsyncPHP\Remit\Server; |
11
|
|
|
use AsyncPHP\Remit\Server\ZeroMqServer; |
12
|
|
|
use Aura\Sql\ExtendedPdo; |
13
|
|
|
use Icicle\Loop; |
14
|
|
|
use PDO; |
15
|
|
|
|
16
|
|
|
final class DoormanConnectorHandler implements Handler |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @inheritdoc |
20
|
|
|
* |
21
|
|
|
* @param Task $task |
22
|
|
|
*/ |
23
|
|
|
public function handle(Task $task) |
24
|
|
|
{ |
25
|
|
|
$config = $task->getData(); |
26
|
|
|
|
27
|
|
|
if ($config["driver"] === "mysql") { |
28
|
|
|
$config += [ |
29
|
|
|
"host" => "127.0.0.1", |
30
|
|
|
"port" => 3306, |
31
|
|
|
"charset" => "utf8", |
32
|
|
|
"socket" => null, |
33
|
|
|
]; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if ($config["remit"]["driver"] === "zeromq") { |
37
|
|
|
$config["remit"]["server"] += [ |
38
|
|
|
"host" => "127.0.0.1", |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
$config["remit"]["client"] += [ |
42
|
|
|
"host" => "127.0.0.1", |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
$server = new ZeroMqServer( |
46
|
|
|
new InMemoryLocation( |
47
|
|
|
$config["remit"]["client"]["host"], |
48
|
|
|
$config["remit"]["client"]["port"] |
49
|
|
|
) |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
$client = new ZeroMqClient( |
53
|
|
|
new InMemoryLocation( |
54
|
|
|
$config["remit"]["server"]["host"], |
55
|
|
|
$config["remit"]["server"]["port"] |
56
|
|
|
) |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$connection = new ExtendedPdo( |
61
|
|
|
new PDO($this->newConnectionString($config), $config["username"], $config["password"]) |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
$server->addListener("q", function ($query, $values, $id) use ($client, $connection) { |
|
|
|
|
65
|
|
|
$client->emit("r", [$connection->fetchAll($query, $values), $id]); |
66
|
|
|
}); |
67
|
|
|
|
68
|
|
|
$server->addListener("d", function () use ($connection, $server, $client) { |
|
|
|
|
69
|
|
|
$client->emit("dd"); |
70
|
|
|
|
71
|
|
|
try { |
72
|
|
|
$connection->disconnect(); |
73
|
|
|
} catch (Exception $exception) { |
|
|
|
|
74
|
|
|
// TODO: find an elegant way to deal with this |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$server->disconnect(); |
78
|
|
|
$client->disconnect(); |
79
|
|
|
|
80
|
|
|
Loop\stop(); |
81
|
|
|
}); |
82
|
|
|
|
83
|
|
|
Loop\periodic(0, function () use ($server) { |
|
|
|
|
84
|
|
|
$server->tick(); |
85
|
|
|
}); |
86
|
|
|
|
87
|
|
|
Loop\run(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Returns a new dsn string, for PDO to connect to the database with. |
92
|
|
|
* |
93
|
|
|
* @param array $config |
94
|
|
|
* |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
private function newConnectionString(array $config) |
98
|
|
|
{ |
99
|
|
|
if ($config["driver"] === "mysql") { |
100
|
|
|
return "mysql:host={$config['host']};port={$config['port']};dbname={$config['schema']};unix_socket={$config['socket']};charset={$config['charset']}"; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if ($config["driver"] === "pgsql") { |
104
|
|
|
return "pgsql:host={$config['host']};port={$config['port']};dbname={$config['schema']}"; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if ($config["driver"] === "sqlite") { |
108
|
|
|
return "sqlite:{$config['file']}"; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if ($config["driver"] === "sqlsrv") { |
112
|
|
|
return "sqlsrv:Server={$config['host']},{$config['port']};Database={$config['schema']}"; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
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: