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\Datasource\Paginator; |
9
|
|
|
use Cake\ORM\Query; |
10
|
|
|
use Cake\ORM\TableRegistry; |
11
|
|
|
use React\EventLoop\LoopInterface; |
12
|
|
|
use React\Promise\Deferred; |
13
|
|
|
use WyriHaximus\React\ChildProcess\Messenger\ChildInterface; |
14
|
|
|
use WyriHaximus\React\ChildProcess\Messenger\Messages\Payload; |
15
|
|
|
use WyriHaximus\React\ChildProcess\Messenger\Messenger; |
16
|
|
|
|
17
|
|
|
final class WorkerChild implements ChildInterface |
18
|
|
|
{ |
19
|
|
|
private $messenger; |
20
|
|
|
private $loop; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* WorkerChild constructor. |
24
|
|
|
*/ |
25
|
|
|
public function __construct(Messenger $messenger, LoopInterface $loop) |
26
|
|
|
{ |
27
|
|
|
$this->messenger = $messenger; |
28
|
|
|
$this->loop = $loop; |
29
|
|
|
|
30
|
|
View Code Duplication |
$this->messenger->registerRpc('table.call', function (Payload $payload) { |
|
|
|
|
31
|
|
|
$deferred = new Deferred(); |
32
|
|
|
$this->loop->futureTick(function () use ($payload, $deferred) { |
33
|
|
|
$this->handleTableCall($payload, $deferred); |
34
|
|
|
}); |
35
|
|
|
|
36
|
|
|
return $deferred->promise(); |
37
|
|
|
}); |
38
|
|
|
|
39
|
|
View Code Duplication |
$this->messenger->registerRpc('paginate', function (Payload $payload) { |
|
|
|
|
40
|
|
|
$deferred = new Deferred(); |
41
|
|
|
$this->loop->futureTick(function () use ($payload, $deferred) { |
42
|
|
|
$this->handlePaginateCall($payload, $deferred); |
43
|
|
|
}); |
44
|
|
|
|
45
|
|
|
return $deferred->promise(); |
46
|
|
|
}); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @inheritDoc |
51
|
|
|
*/ |
52
|
|
|
public static function create(Messenger $messenger, LoopInterface $loop) |
53
|
|
|
{ |
54
|
|
|
require dirname(dirname(dirname(dirname(__DIR__)))) . '/config/paths.php'; |
55
|
|
|
require CORE_PATH . 'config' . DS . 'bootstrap.php'; |
56
|
|
|
Configure::config('default', new Configure\Engine\PhpConfig()); |
57
|
|
|
Configure::load('app', 'default', false); |
58
|
|
|
Cache::setConfig(Configure::consume('Cache')); |
|
|
|
|
59
|
|
|
ConnectionManager::setConfig(Configure::consume('Datasources')); |
|
|
|
|
60
|
|
|
|
61
|
|
|
return new self($messenger, $loop); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param Payload $payload |
66
|
|
|
* @param Deferred $deferred |
67
|
|
|
*/ |
68
|
|
|
protected function handleTableCall(Payload $payload, Deferred $deferred) |
69
|
|
|
{ |
70
|
|
|
$result = call_user_func_array([ |
71
|
|
|
TableRegistry::get( |
|
|
|
|
72
|
|
|
$payload['table']/*, |
|
|
|
|
73
|
|
|
[ |
74
|
|
|
'className' => $payload['className'], |
75
|
|
|
'table' => $payload['table'], |
76
|
|
|
]*/ |
77
|
|
|
), |
78
|
|
|
$payload['function'], |
79
|
|
|
], unserialize($payload['arguments'])); |
80
|
|
|
|
81
|
|
|
if (!($result instanceof Query)) { |
82
|
|
|
$deferred->resolve([ |
83
|
|
|
'result' => serialize($result), |
84
|
|
|
]); |
85
|
|
|
|
86
|
|
|
return; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
foreach ($result->all() as $row) { |
90
|
|
|
$deferred->notify([ |
|
|
|
|
91
|
|
|
'row' => $row, |
92
|
|
|
]); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$deferred->resolve(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param Payload $payload |
100
|
|
|
* @param Deferred $deferred |
101
|
|
|
*/ |
102
|
|
|
protected function handlePaginateCall(Payload $payload, Deferred $deferred) |
103
|
|
|
{ |
104
|
|
|
$object = TableRegistry::get( |
|
|
|
|
105
|
|
|
$payload['table']/*, |
|
|
|
|
106
|
|
|
[ |
107
|
|
|
'className' => $payload['className'], |
108
|
|
|
'table' => $payload['table'], |
109
|
|
|
]*/ |
110
|
|
|
); |
111
|
|
|
$paginator = new Paginator(); |
112
|
|
|
|
113
|
|
|
return $deferred->resolve([ |
114
|
|
|
'items' => $paginator->paginate($object, $payload['params'], $payload['settings'])->toArray(), |
115
|
|
|
'pagingParams' => $paginator->getPagingParams(), |
116
|
|
|
]); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.