1
|
|
|
<?php |
2
|
|
|
namespace Ackintosh\Snidel; |
3
|
|
|
|
4
|
|
|
use Ackintosh\Snidel\Pcntl; |
5
|
|
|
use Ackintosh\Snidel\Result; |
6
|
|
|
use Ackintosh\Snidel\Task\Task; |
7
|
|
|
use Ackintosh\Snidel\Task\Formatter; |
8
|
|
|
|
9
|
|
|
class Fork |
10
|
|
|
{ |
11
|
|
|
/** @var int */ |
12
|
|
|
private $pid; |
13
|
|
|
|
14
|
|
|
/** @var \Ackintosh\Snidel\Pcntl */ |
15
|
|
|
private $pcntl; |
16
|
|
|
|
17
|
|
|
/** @var int */ |
18
|
|
|
private $status; |
19
|
|
|
|
20
|
|
|
/** @var callable */ |
21
|
|
|
private $callable; |
22
|
|
|
|
23
|
|
|
/** @var array */ |
24
|
|
|
private $args; |
25
|
|
|
|
26
|
|
|
/** @var \Ackintosh\Snidel\Result */ |
27
|
|
|
private $result; |
28
|
|
|
|
29
|
|
|
/** @var string */ |
30
|
|
|
private $tag; |
31
|
|
|
|
32
|
|
|
/** @var \Ackintosh\Snidel\Task */ |
33
|
|
|
private $task; |
34
|
|
|
|
35
|
|
|
/** @var string */ |
36
|
|
|
private $serializedTask; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param int $pid |
40
|
|
|
*/ |
41
|
|
|
public function __construct($pid, $task) |
42
|
|
|
{ |
43
|
|
|
$this->pid = $pid; |
44
|
|
|
$this->task = $task; |
45
|
|
|
$this->pcntl = new Pcntl(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* set exit status |
50
|
|
|
* |
51
|
|
|
* @param int $status |
52
|
|
|
* @return void |
53
|
|
|
*/ |
54
|
|
|
public function setStatus($status) |
55
|
|
|
{ |
56
|
|
|
$this->status = $status; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* return pid |
61
|
|
|
* |
62
|
|
|
* @return int |
63
|
|
|
*/ |
64
|
|
|
public function getPid() |
65
|
|
|
{ |
66
|
|
|
return $this->pid; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* return exit status |
71
|
|
|
* |
72
|
|
|
* @return int |
73
|
|
|
*/ |
74
|
|
|
public function getStatus() |
75
|
|
|
{ |
76
|
|
|
return $this->status; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* set callable |
81
|
|
|
* |
82
|
|
|
* @param callable $callable |
83
|
|
|
* @return void |
84
|
|
|
*/ |
85
|
|
|
public function setCallable($callable) |
86
|
|
|
{ |
87
|
|
|
$this->callable = $callable instanceof \Closure ? '*Closure*' : $callable; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function getCallable() |
91
|
|
|
{ |
92
|
|
|
return $this->callable; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* set arguments |
97
|
|
|
* |
98
|
|
|
* @param array $args |
99
|
|
|
* @return void |
100
|
|
|
*/ |
101
|
|
|
public function setArgs($args) |
102
|
|
|
{ |
103
|
|
|
$this->args = $args; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function getArgs() |
107
|
|
|
{ |
108
|
|
|
return $this->args; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return bool |
113
|
|
|
*/ |
114
|
|
|
public function hasFinishedSuccessfully() |
115
|
|
|
{ |
116
|
|
|
return $this->pcntl->wifexited($this->status) && $this->pcntl->wexitstatus($this->status) === 0; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return bool |
121
|
|
|
*/ |
122
|
|
|
public function hasNotFinishedSuccessfully() |
123
|
|
|
{ |
124
|
|
|
return !$this->hasFinishedSuccessfully(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* |
129
|
|
|
* @param \Ackintosh\Snidel\Result |
130
|
|
|
* @return void |
131
|
|
|
*/ |
132
|
|
|
public function setResult($result) |
133
|
|
|
{ |
134
|
|
|
$this->result = $result; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* return result |
139
|
|
|
* |
140
|
|
|
* @return \Ackintosh\Snidel\Result |
141
|
|
|
*/ |
142
|
|
|
public function getResult() |
143
|
|
|
{ |
144
|
|
|
return $this->result; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function hasNoResult() |
148
|
|
|
{ |
149
|
|
|
return $this->result === null; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param string $tag |
154
|
|
|
* @return void |
155
|
|
|
*/ |
156
|
|
|
public function setTag($tag) |
157
|
|
|
{ |
158
|
|
|
$this->tag = $tag; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return string |
163
|
|
|
*/ |
164
|
|
|
public function getTag() |
165
|
|
|
{ |
166
|
|
|
return $this->task->getTag(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function executeTask() |
170
|
|
|
{ |
171
|
|
|
ob_start(); |
172
|
|
|
$result = new Result(); |
173
|
|
|
$result->setReturn( |
174
|
|
|
call_user_func_array( |
175
|
|
|
$this->task->getCallable(), |
176
|
|
|
(is_array($args = $this->task->getArgs())) ? $args : array($args) |
177
|
|
|
) |
178
|
|
|
); |
179
|
|
|
$result->setOutput(ob_get_clean()); |
180
|
|
|
$result->setFork($this); |
181
|
|
|
|
182
|
|
|
return $result; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param \Ackintosh\Snidel\Fork $fork |
187
|
|
|
* @return string |
188
|
|
|
*/ |
189
|
|
|
public static function serialize($fork) |
190
|
|
|
{ |
191
|
|
|
$cloned = clone $fork; |
192
|
|
|
$cloned->serializeTask(); |
193
|
|
|
|
194
|
|
|
return serialize($cloned); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @return void |
199
|
|
|
*/ |
200
|
|
|
private function serializeTask() |
201
|
|
|
{ |
202
|
|
|
$this->serializedTask = Formatter::serialize($this->task); |
203
|
|
|
unset($this->task); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
public static function minifyAndSerialize($fork) |
207
|
|
|
{ |
208
|
|
|
$cloned = clone $fork; |
209
|
|
|
$cloned->minifyAndSerializeTask(); |
210
|
|
|
|
211
|
|
|
return serialize($cloned); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
private function minifyAndSerializeTask() |
215
|
|
|
{ |
216
|
|
|
$this->serializedTask = Formatter::minifyAndSerialize($this->task); |
217
|
|
|
unset($this->task); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @param string $serializedFork |
222
|
|
|
* @return \Ackintosh\Snidel\Fork |
223
|
|
|
*/ |
224
|
|
|
public static function unserialize($serializedFork) |
225
|
|
|
{ |
226
|
|
|
$fork = unserialize($serializedFork); |
227
|
|
|
$fork->unserializeTask(); |
228
|
|
|
|
229
|
|
|
return $fork; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @return void |
234
|
|
|
*/ |
235
|
|
|
private function unserializeTask() |
236
|
|
|
{ |
237
|
|
|
$this->task = Formatter::unserialize($this->serializedTask); |
|
|
|
|
238
|
|
|
$this->serializedTask = null; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @return void |
243
|
|
|
*/ |
244
|
|
|
public function __clone() |
245
|
|
|
{ |
246
|
|
|
// to avoid point to same object. |
247
|
|
|
$this->task = clone $this->task; |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..