1
|
|
|
<?php |
2
|
|
|
namespace PHPDaemon\Examples; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* @package Examples |
6
|
|
|
* @subpackage Mongo |
7
|
|
|
* |
8
|
|
|
* @author Vasily Zorin <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
class ExampleWithMongo extends \PHPDaemon\Core\AppInstance |
11
|
|
|
{ |
12
|
|
|
public $mongo; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Constructor. |
16
|
|
|
* @return void |
17
|
|
|
*/ |
18
|
|
|
public function init() |
19
|
|
|
{ |
20
|
|
|
$this->mongo = \PHPDaemon\Clients\Mongo\Pool::getInstance(['maxconnperserv' => 100]); |
|
|
|
|
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Creates Request. |
25
|
|
|
* @param object Request. |
26
|
|
|
* @param object Upstream application instance. |
27
|
|
|
* @return ExampleWithMongoRequest Request. |
28
|
|
|
*/ |
29
|
|
|
public function beginRequest($req, $upstream) |
30
|
|
|
{ |
31
|
|
|
return new ExampleWithMongoRequest($this, $upstream, $req); |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
class ExampleWithMongoRequest extends \PHPDaemon\HTTPRequest\Generic |
36
|
|
|
{ |
37
|
|
|
|
38
|
|
|
public $job; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Constructor. |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
|
|
public function init() |
45
|
|
|
{ |
46
|
|
|
$job = $this->job = new \PHPDaemon\Core\ComplexJob(function ($job) { // called when job is done |
47
|
|
|
$job->keep(); // prevent cleaning up results |
48
|
|
|
$this->wakeup(); // wake up the request immediately |
49
|
|
|
|
50
|
|
|
}); |
51
|
|
|
|
52
|
|
|
$collection = $this->appInstance->mongo->{'testdb.testcollection'}; |
|
|
|
|
53
|
|
|
$collection->insert(['a' => microtime(true)]); // just pushing something |
54
|
|
|
|
55
|
|
|
$job('testquery', function ($name, $job) use ($collection) { // registering job named 'testquery' |
56
|
|
|
|
57
|
|
|
$collection->findOne(function ($result) use ($name, $job) { // calling Mongo findOne |
58
|
|
|
|
59
|
|
|
$job->setResult($name, $result); |
60
|
|
|
|
61
|
|
|
}, ['sort' => ['$natural' => -1]]); |
62
|
|
|
}); |
63
|
|
|
|
64
|
|
|
$job(); // let the fun begin |
65
|
|
|
|
66
|
|
|
$this->sleep(1, true); // setting timeout |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Called when request iterated. |
71
|
|
|
* @return integer Status. |
|
|
|
|
72
|
|
|
*/ |
73
|
|
View Code Duplication |
public function run() |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
try { |
76
|
|
|
$this->header('Content-Type: text/html'); |
77
|
|
|
} catch (\Exception $e) { |
|
|
|
|
78
|
|
|
} |
79
|
|
|
?> |
80
|
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
81
|
|
|
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
82
|
|
|
<html xmlns="http://www.w3.org/1999/xhtml"> |
83
|
|
|
<head> |
84
|
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> |
85
|
|
|
<title>Example with Mongo</title> |
86
|
|
|
</head> |
87
|
|
|
<body> |
88
|
|
|
<?php |
89
|
|
|
if ($r = $this->job->getResult('testquery')) { |
90
|
|
|
echo '<h1>It works! Be happy! ;-)</h1>Result of query: <pre>'; |
91
|
|
|
var_dump($r); |
|
|
|
|
92
|
|
|
echo '</pre>'; |
93
|
|
|
} else { |
94
|
|
|
echo '<h1>Something went wrong! We have no result.</h1>'; |
95
|
|
|
} |
96
|
|
|
echo '<br />Request (http) took: ' . round(microtime(true) - $this->attrs->server['REQUEST_TIME_FLOAT'], 6); |
97
|
|
|
?> |
98
|
|
|
</body> |
99
|
|
|
</html> |
100
|
|
|
<?php |
101
|
|
|
|
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: