1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
Database credentials: add to phpd.conf: |
4
|
|
|
Pool:\PHPDaemon\Clients\MySQL\Pool{ |
5
|
|
|
enable 1; |
6
|
|
|
server 'tcp://user:[email protected]/dbname'; |
7
|
|
|
privileged; |
8
|
|
|
} |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace PHPDaemon\Examples; |
12
|
|
|
|
13
|
|
|
use PHPDaemon\HTTPRequest\Generic; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @package Examples |
17
|
|
|
* @subpackage MySQL |
18
|
|
|
* |
19
|
|
|
* @author Vasily Zorin <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class ExampleWithMySQL extends \PHPDaemon\Core\AppInstance |
22
|
|
|
{ |
23
|
|
|
public $sql; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Called when the worker is ready to go. |
27
|
|
|
* @return void |
28
|
|
|
*/ |
29
|
|
|
|
30
|
|
|
public function onReady() |
31
|
|
|
{ |
32
|
|
|
$this->sql = \PHPDaemon\Clients\MySQL\Pool::getInstance(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Creates Request. |
37
|
|
|
* @param object Request. |
38
|
|
|
* @param object Upstream application instance. |
39
|
|
|
* @return ExampleWithMySQLRequest Request. |
40
|
|
|
*/ |
41
|
|
|
public function beginRequest($req, $upstream) |
42
|
|
|
{ |
43
|
|
|
return new ExampleWithMySQLRequest($this, $upstream, $req); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
class ExampleWithMySQLRequest extends Generic |
48
|
|
|
{ |
49
|
|
|
|
50
|
|
|
protected $job; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Constructor. |
54
|
|
|
* @return void |
55
|
|
|
*/ |
56
|
|
|
public function init() |
57
|
|
|
{ |
58
|
|
|
$job = $this->job = new \PHPDaemon\Core\ComplexJob(function ($job) { // called when job is done |
59
|
|
|
|
60
|
|
|
$job->keep(); |
61
|
|
|
$this->wakeup(); // wake up the request immediately |
62
|
|
|
|
63
|
|
|
}); |
64
|
|
|
|
65
|
|
|
$job('select', function ($name, $job) { // registering job named 'select' |
66
|
|
|
|
67
|
|
|
$this->appInstance->sql->getConnection(function ($sql) use ($name, $job) { |
|
|
|
|
68
|
|
|
if (!$sql->isConnected()) { |
69
|
|
|
$job->setResult($name, null); |
70
|
|
|
return null; |
71
|
|
|
} |
72
|
|
|
$sql->query('SELECT 123, "string"', function ($sql, $success) use ($job, $name) { |
|
|
|
|
73
|
|
|
|
74
|
|
|
$job('showdbs', function ($name, $job) use ($sql) { // registering job named 'showdbs' |
75
|
|
|
$sql->query('SHOW DATABASES', function ($sql, $t) use ($job, $name) { |
|
|
|
|
76
|
|
|
$job->setResult($name, $sql->resultRows); |
77
|
|
|
}); |
78
|
|
|
}); |
79
|
|
|
$job->setResult($name, $sql->resultRows); |
80
|
|
|
}); |
81
|
|
|
return null; |
82
|
|
|
}); |
83
|
|
|
}); |
84
|
|
|
|
85
|
|
|
$job(); // let the fun begin |
86
|
|
|
|
87
|
|
|
$this->sleep(5, true); // setting timeout |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Called when request iterated. |
92
|
|
|
* @return integer Status. |
|
|
|
|
93
|
|
|
*/ |
94
|
|
|
public function run() |
95
|
|
|
{ |
96
|
|
|
try { |
97
|
|
|
$this->header('Content-Type: text/html'); |
98
|
|
|
} catch (\Exception $e) { |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
102
|
|
|
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
103
|
|
|
<html xmlns="http://www.w3.org/1999/xhtml"> |
104
|
|
|
<head> |
105
|
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> |
106
|
|
|
<title>Example with MySQL</title> |
107
|
|
|
</head> |
108
|
|
|
<body> |
109
|
|
|
<?php |
110
|
|
|
if ($r = $this->job->getResult('select')) { |
111
|
|
|
echo '<h1>It works! Be happy! ;-)</h1>Result of SELECT 123, "string": <pre>'; |
112
|
|
|
var_dump($r); |
|
|
|
|
113
|
|
|
echo '</pre>'; |
114
|
|
|
|
115
|
|
|
echo '<br />Result of SHOW DATABASES: <pre>'; |
116
|
|
|
var_dump($this->job->getResult('showdbs')); |
117
|
|
|
echo '</pre>'; |
118
|
|
|
} else { |
119
|
|
|
echo '<h1>Something went wrong! We have no result.</h1>'; |
120
|
|
|
} |
121
|
|
|
echo '<br />Request (http) took: ' . round(microtime(true) - $this->attrs->server['REQUEST_TIME_FLOAT'], 6); |
122
|
|
|
?> |
123
|
|
|
</body> |
124
|
|
|
</html> |
125
|
|
|
<?php |
126
|
|
|
|
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.