1
|
|
|
<?php |
2
|
|
|
namespace PHPDaemon\Examples; |
3
|
|
|
|
4
|
|
|
use PHPDaemon\HTTPRequest\Generic; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* @package Examples |
8
|
|
|
* @subpackage PostgreSQL |
9
|
|
|
* |
10
|
|
|
* @author Vasily Zorin <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class ExampleWithPostgreSQL extends \PHPDaemon\Core\AppInstance |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
public $pgsql; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Constructor. |
19
|
|
|
* @return void |
20
|
|
|
*/ |
21
|
|
|
public function init() |
22
|
|
|
{ |
23
|
|
|
$this->pgsql = \PHPDaemon\Clients\PostgreSQL\Pool::getInstance(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Creates Request. |
28
|
|
|
* @param object Request. |
29
|
|
|
* @param object Upstream application instance. |
30
|
|
|
* @return ExampleWithPostgreSQLRequest Request. |
31
|
|
|
*/ |
32
|
|
|
public function beginRequest($req, $upstream) |
33
|
|
|
{ |
34
|
|
|
return new ExampleWithPostgreSQLRequest($this, $upstream, $req); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
class ExampleWithPostgreSQLRequest extends Generic |
39
|
|
|
{ |
40
|
|
|
|
41
|
|
|
public $stime; |
42
|
|
|
public $queryResult; |
43
|
|
|
public $sql; |
44
|
|
|
public $runstate = 0; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Constructor. |
48
|
|
|
* @return void |
49
|
|
|
*/ |
50
|
|
|
public function init() |
51
|
|
|
{ |
52
|
|
|
$this->appInstance->pgsql->getConnection(function ($sql) { |
|
|
|
|
53
|
|
|
if (!$sql->connected) { // failed to connect |
54
|
|
|
$this->wakeup(); // wake up the request immediately |
55
|
|
|
$sql->release(); |
56
|
|
|
return; |
57
|
|
|
} |
58
|
|
|
$sql->query('SELECT 123 as integer, NULL as nul, \'test\' as string', function ($sql, $success) { |
|
|
|
|
59
|
|
|
$this->queryResult = $sql->resultRows; // save the result |
60
|
|
|
$this->wakeup(); // wake up the request immediately |
61
|
|
|
$sql->release(); |
62
|
|
|
}); |
63
|
|
|
}); |
64
|
|
|
$this->sleep(5); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Called when request iterated. |
69
|
|
|
* @return integer Status. |
|
|
|
|
70
|
|
|
*/ |
71
|
|
View Code Duplication |
public function run() |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
try { |
74
|
|
|
$this->header('Content-Type: text/html'); |
75
|
|
|
} catch (\Exception $e) { |
|
|
|
|
76
|
|
|
} |
77
|
|
|
?> |
78
|
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
79
|
|
|
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
80
|
|
|
<html xmlns="http://www.w3.org/1999/xhtml"> |
81
|
|
|
<head> |
82
|
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> |
83
|
|
|
<title>Example with PostgreSQL</title> |
84
|
|
|
</head> |
85
|
|
|
<body> |
86
|
|
|
<?php |
87
|
|
|
|
88
|
|
|
if ($this->queryResult) { |
89
|
|
|
echo '<h1>It works! Be happy! ;-)</h1>Result of `SELECT 123 as integer, NULL as nul, \'test\' as string`: <pre>'; |
90
|
|
|
var_dump($this->queryResult); |
|
|
|
|
91
|
|
|
echo '</pre>'; |
92
|
|
|
} else { |
93
|
|
|
echo '<h1>Something went wrong! We have no result.</h1>'; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
echo '<br />Request (http) took: ' . round(microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'], 6); |
97
|
|
|
|
98
|
|
|
?> |
99
|
|
|
</body> |
100
|
|
|
</html> |
101
|
|
|
<?php |
102
|
|
|
|
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
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.