1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Railt package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace Railt\SDL\Process; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Process |
14
|
|
|
*/ |
15
|
|
|
class Process implements ProcessInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var array|InterceptorInterface[] |
19
|
|
|
*/ |
20
|
|
|
private $interceptors = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array|callable[] |
24
|
|
|
*/ |
25
|
|
|
private $ticks = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var \SplQueue|DeferredInterface[] |
29
|
|
|
*/ |
30
|
|
|
private $deferred; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Process constructor. |
34
|
|
|
*/ |
35
|
|
|
public function __construct() |
36
|
|
|
{ |
37
|
|
|
$this->deferred = new \SplQueue(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param mixed $process |
42
|
|
|
* @return mixed |
43
|
|
|
*/ |
44
|
|
|
public function await($process) |
45
|
|
|
{ |
46
|
|
|
return $this->runDeferredResolvers($this->exec($process)); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param mixed $process |
51
|
|
|
* @return mixed |
52
|
|
|
*/ |
53
|
|
|
private function exec($process) |
54
|
|
|
{ |
55
|
|
|
return $process instanceof \Generator ? $this->coroutine($process) : $this->each($process); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param callable $callable |
60
|
|
|
* @return DeferredInterface |
61
|
|
|
*/ |
62
|
|
|
public function deferred(callable $callable): DeferredInterface |
63
|
|
|
{ |
64
|
|
|
return $this->deferred[] = new Deferred($this, $callable); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return iterable|DeferredInterface[] |
69
|
|
|
*/ |
70
|
|
|
private function getDeferredResolvers(): iterable |
71
|
|
|
{ |
72
|
|
|
while ($this->deferred->count()) { |
73
|
|
|
yield $this->deferred->pop(); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param mixed $value |
79
|
|
|
* @return mixed |
80
|
|
|
*/ |
81
|
|
|
private function runDeferredResolvers($value) |
82
|
|
|
{ |
83
|
|
|
foreach ($this->getDeferredResolvers() as $resolver) { |
84
|
|
|
$this->each($resolver->invoke($value)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $value; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param \Generator $process |
92
|
|
|
* @return mixed |
93
|
|
|
*/ |
94
|
|
|
private function coroutine(\Generator $process) |
95
|
|
|
{ |
96
|
|
|
while ($process->valid()) { |
97
|
|
|
$process->send($this->each($process->current())); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $this->each($process->getReturn()); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param callable $handler |
105
|
|
|
* @return ProcessInterface |
106
|
|
|
*/ |
107
|
|
|
public function tick(callable $handler): ProcessInterface |
108
|
|
|
{ |
109
|
|
|
$this->ticks[] = $handler; |
110
|
|
|
|
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param mixed $value |
116
|
|
|
* @return mixed |
117
|
|
|
*/ |
118
|
|
|
private function each($value) |
119
|
|
|
{ |
120
|
|
|
return $this->runTickHandlers($this->reduce($this->interceptors, $value)); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param array $interceptors |
125
|
|
|
* @param mixed $value |
126
|
|
|
* @return mixed |
127
|
|
|
*/ |
128
|
|
|
private function reduce(array $interceptors, $value) |
129
|
|
|
{ |
130
|
|
|
foreach ($interceptors as $index => $interceptor) { |
131
|
|
|
if ($interceptor->match($value)) { |
132
|
|
|
$value = $interceptor->invoke($value); |
133
|
|
|
|
134
|
|
|
return $this->reduce($this->arrayExcept($interceptors, $index), $value); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $value; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param array $items |
143
|
|
|
* @param int $index |
144
|
|
|
* @return array |
145
|
|
|
*/ |
146
|
|
|
private function arrayExcept(array $items, int $index): array |
147
|
|
|
{ |
148
|
|
|
unset($items[$index]); |
149
|
|
|
|
150
|
|
|
return $items; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param mixed $value |
155
|
|
|
* @return mixed |
156
|
|
|
*/ |
157
|
|
|
private function runTickHandlers($value) |
158
|
|
|
{ |
159
|
|
|
foreach ($this->ticks as $tick) { |
160
|
|
|
$tick($value); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return $value; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param InterceptorInterface $interceptor |
168
|
|
|
* @return ProcessInterface |
169
|
|
|
*/ |
170
|
|
|
public function intercept(InterceptorInterface $interceptor): ProcessInterface |
171
|
|
|
{ |
172
|
|
|
$this->interceptors[] = $interceptor; |
173
|
|
|
|
174
|
|
|
return $this; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param mixed|\Generator $result |
179
|
|
|
* @return \Generator |
180
|
|
|
*/ |
181
|
|
|
public static function toGenerator($result): \Generator |
182
|
|
|
{ |
183
|
|
|
if ($result instanceof \Generator) { |
184
|
|
|
yield from $result; |
185
|
|
|
|
186
|
|
|
return $result->getReturn(); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return $result; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
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: