1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace WyriHaximus\React\Cake\Orm; |
4
|
|
|
|
5
|
|
|
use Cake\Cache\Cache; |
6
|
|
|
use Cake\Core\Configure; |
7
|
|
|
use Cake\Datasource\ConnectionManager; |
8
|
|
|
use Cake\ORM\Query; |
9
|
|
|
use Cake\ORM\TableRegistry; |
10
|
|
|
use React\EventLoop\LoopInterface; |
11
|
|
|
use React\Promise\Deferred; |
12
|
|
|
use WyriHaximus\React\ChildProcess\Messenger\ChildInterface; |
13
|
|
|
use WyriHaximus\React\ChildProcess\Messenger\Messages\Payload; |
14
|
|
|
use WyriHaximus\React\ChildProcess\Messenger\Messenger; |
15
|
|
|
|
16
|
|
|
final class WorkerChild implements ChildInterface |
17
|
|
|
{ |
18
|
|
|
private $messenger; |
19
|
|
|
private $loop; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* WorkerChild constructor. |
23
|
|
|
*/ |
24
|
|
|
public function __construct(Messenger $messenger, LoopInterface $loop) |
25
|
|
|
{ |
26
|
|
|
$this->messenger = $messenger; |
27
|
|
|
$this->loop = $loop; |
28
|
|
|
|
29
|
|
|
$this->messenger->registerRpc('table.call', function (Payload $payload) { |
30
|
|
|
$deferred = new Deferred(); |
31
|
|
|
$this->loop->futureTick(function () use ($payload, $deferred) { |
32
|
|
|
$this->handleTableCall($payload, $deferred); |
33
|
|
|
}); |
34
|
|
|
|
35
|
|
|
return $deferred->promise(); |
36
|
|
|
}); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @inheritDoc |
41
|
|
|
*/ |
42
|
|
|
public static function create(Messenger $messenger, LoopInterface $loop) |
43
|
|
|
{ |
44
|
|
|
require dirname(dirname(dirname(dirname(__DIR__)))) . '/config/paths.php'; |
45
|
|
|
require CORE_PATH . 'config' . DS . 'bootstrap.php'; |
46
|
|
|
Configure::config('default', new Configure\Engine\PhpConfig()); |
47
|
|
|
Configure::load('app', 'default', false); |
48
|
|
|
Cache::setConfig(Configure::consume('Cache')); |
|
|
|
|
49
|
|
|
ConnectionManager::setConfig(Configure::consume('Datasources')); |
|
|
|
|
50
|
|
|
|
51
|
|
|
return new self($messenger, $loop); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param Payload $payload |
56
|
|
|
* @param Deferred $deferred |
57
|
|
|
*/ |
58
|
|
|
protected function handleTableCall(Payload $payload, Deferred $deferred) |
59
|
|
|
{ |
60
|
|
|
$result = call_user_func_array([ |
61
|
|
|
TableRegistry::get( |
|
|
|
|
62
|
|
|
$payload['table']/*, |
|
|
|
|
63
|
|
|
[ |
64
|
|
|
'className' => $payload['className'], |
65
|
|
|
'table' => $payload['table'], |
66
|
|
|
]*/ |
67
|
|
|
), |
68
|
|
|
$payload['function'], |
69
|
|
|
], unserialize($payload['arguments'])); |
70
|
|
|
|
71
|
|
|
if (!($result instanceof Query)) { |
72
|
|
|
$deferred->resolve([ |
73
|
|
|
'result' => serialize($result), |
74
|
|
|
]); |
75
|
|
|
|
76
|
|
|
return; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
foreach ($result->all() as $row) { |
80
|
|
|
$deferred->notify([ |
|
|
|
|
81
|
|
|
'row' => $row, |
82
|
|
|
]); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$deferred->resolve(); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.