1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WyriHaximus\React\Cake\Orm; |
4
|
|
|
|
5
|
|
|
use Cake\Core\Configure; |
6
|
|
|
use React\ChildProcess\Process; |
7
|
|
|
use React\EventLoop\LoopInterface; |
8
|
|
|
use React\EventLoop\Timer\TimerInterface; |
9
|
|
|
use React\Promise\Deferred; |
10
|
|
|
use React\Promise\PromiseInterface; |
11
|
|
|
use WyriHaximus\React\ChildProcess\Messenger\Messages\Factory; |
12
|
|
|
use WyriHaximus\React\ChildProcess\Pool\Factory\Flexible; |
13
|
|
|
use WyriHaximus\React\ChildProcess\Pool\PoolInfoInterface; |
14
|
|
|
use WyriHaximus\React\ChildProcess\Pool\PoolInterface; |
15
|
|
|
use WyriHaximus\React\ChildProcess\Pool\PoolUtilizerInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class Pool |
19
|
|
|
* @package WyriHaximus\React\Cake\Orm |
20
|
|
|
*/ |
21
|
|
|
class Pool implements PoolUtilizerInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var LoopInterface |
25
|
|
|
*/ |
26
|
|
|
protected $loop; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var PoolInfoInterface |
30
|
|
|
*/ |
31
|
|
|
protected $pool; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var Pool |
35
|
|
|
*/ |
36
|
1 |
|
protected static $instance = null; |
37
|
|
|
|
38
|
1 |
|
/** |
39
|
1 |
|
* @var boolean |
40
|
1 |
|
*/ |
41
|
1 |
|
protected static $reset = false; |
42
|
1 |
|
|
43
|
1 |
|
/** |
44
|
|
|
* @param LoopInterface $loop |
45
|
1 |
|
*/ |
46
|
|
|
protected function __construct(LoopInterface $loop) |
47
|
|
|
{ |
48
|
1 |
|
$this->loop = $loop; |
49
|
1 |
|
Flexible::create( |
50
|
1 |
|
new Process( |
51
|
|
|
Configure::read('WyriHaximus.React.Cake.Orm.Process') |
52
|
|
|
), |
53
|
|
|
$this->loop, |
54
|
|
|
[ |
55
|
|
|
'processOptions' => Configure::read('WyriHaximus.React.Cake.Orm.Line'), |
56
|
|
|
] |
57
|
2 |
|
)->then(function (PoolInterface $pool) { |
58
|
|
|
$this->pool = $pool; |
59
|
2 |
|
}); |
60
|
2 |
|
} |
61
|
1 |
|
|
62
|
|
|
/** |
63
|
|
|
* @param LoopInterface $loop |
64
|
1 |
|
* @return Pool |
65
|
1 |
|
* @throws \Exception |
66
|
|
|
*/ |
67
|
2 |
|
public static function getInstance(LoopInterface $loop = null) |
68
|
|
|
{ |
69
|
|
|
if (null === self::$instance || self::$reset) { |
70
|
18 |
|
if (null === $loop) { |
71
|
|
|
throw new \Exception('Missing event loop'); |
72
|
18 |
|
} |
73
|
18 |
|
self::$instance = new static($loop); |
74
|
|
|
self::$reset = false; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return self::$instance; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public static function reset() |
81
|
|
|
{ |
82
|
|
|
self::$reset = true; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param $tableName |
87
|
|
|
* @param $function |
88
|
|
|
* @param array $arguments |
89
|
|
|
* @return PromiseInterface |
90
|
|
|
*/ |
91
|
|
|
public function call($tableName, $function, array $arguments) |
92
|
|
|
{ |
93
|
|
|
if ($this->pool instanceof PoolInterface) { |
94
|
|
|
return $this->poolCall($tableName, $function, $arguments); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $this->waitForPoolCall($tableName, $function, $arguments); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param $tableName |
102
|
|
|
* @param $function |
103
|
|
|
* @param array $arguments |
104
|
|
|
* @return PromiseInterface |
105
|
|
|
*/ |
106
|
|
|
protected function poolCall($tableName, $function, array $arguments) |
107
|
|
|
{ |
108
|
|
|
return $this->pool->rpc(Factory::rpc('table.call', [ |
|
|
|
|
109
|
|
|
'function' => $function, |
110
|
|
|
'table' => $tableName, |
111
|
|
|
'arguments' => serialize($arguments), |
112
|
|
|
]))->then(function ($result) { |
113
|
1 |
|
return \React\Promise\resolve($result['result']); |
114
|
|
|
}); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param $tableName |
119
|
1 |
|
* @param $function |
120
|
|
|
* @param array $arguments |
121
|
|
|
* @return PromiseInterface |
122
|
|
|
*/ |
123
|
|
|
protected function waitForPoolCall($tableName, $function, array $arguments) |
124
|
1 |
|
{ |
125
|
|
|
$deferred = new Deferred(); |
126
|
|
|
|
127
|
|
|
$this->loop->addPeriodicTimer( |
128
|
|
|
0.1, |
129
|
|
|
function (TimerInterface $timer) use ($deferred, $tableName, $function, $arguments) { |
130
|
|
|
if ($this->pool instanceof PoolInterface) { |
131
|
|
|
$timer->cancel(); |
132
|
|
|
$deferred->resolve($this->call($tableName, $function, $arguments)); |
133
|
1 |
|
} |
134
|
|
|
} |
135
|
1 |
|
); |
136
|
1 |
|
|
137
|
|
|
return $deferred->promise(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @inheritDoc |
142
|
|
|
*/ |
143
|
|
|
public function info() |
144
|
|
|
{ |
145
|
|
|
if ($this->pool instanceof PoolInterface) { |
146
|
|
|
return $this->pool->info(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return []; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @return LoopInterface |
154
|
|
|
*/ |
155
|
|
|
public function getLoop() |
156
|
|
|
{ |
157
|
|
|
return $this->loop; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return PoolInfoInterface |
162
|
|
|
*/ |
163
|
|
|
public function getPool() |
164
|
|
|
{ |
165
|
|
|
return $this->pool; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: