1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** @noinspection UnserializeExploitsInspection */ |
4
|
|
|
|
5
|
|
|
namespace Tleckie\Async; |
6
|
|
|
|
7
|
|
|
use Symfony\Component\Process\Process; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Task |
11
|
|
|
* |
12
|
|
|
* @package Tleckie\Async |
13
|
|
|
* @author Teodoro Leckie Westberg <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class Task implements TaskInterface |
16
|
|
|
{ |
17
|
|
|
/** @var int */ |
18
|
|
|
protected int $id; |
19
|
|
|
|
20
|
|
|
/** @var int */ |
21
|
|
|
protected int $pid; |
22
|
|
|
|
23
|
|
|
/** @var Process */ |
24
|
|
|
protected Process $process; |
25
|
|
|
|
26
|
|
|
/** @var callable[] */ |
27
|
|
|
protected array $success = []; |
28
|
|
|
|
29
|
|
|
/** @var callable[] */ |
30
|
|
|
protected array $error = []; |
31
|
|
|
|
32
|
|
|
/** @var Encoder */ |
33
|
|
|
protected Encoder $encoder; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Task constructor. |
37
|
|
|
* |
38
|
|
|
* @param Process $process |
39
|
|
|
* @param Encoder $encoder |
40
|
|
|
* @param int $id |
41
|
|
|
*/ |
42
|
|
|
public function __construct( |
43
|
|
|
Process $process, |
44
|
|
|
Encoder $encoder, |
45
|
|
|
int $id |
46
|
|
|
) { |
47
|
|
|
$this->process = $process; |
48
|
|
|
$this->encoder = $encoder; |
49
|
|
|
$this->id = $id; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @inheritdoc |
54
|
|
|
*/ |
55
|
|
|
public function then(callable $callback): self |
56
|
|
|
{ |
57
|
|
|
$this->success[] = $callback; |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param callable $callback |
64
|
|
|
* @return $this |
65
|
|
|
*/ |
66
|
|
|
public function catch(callable $callback): self |
67
|
|
|
{ |
68
|
|
|
$this->error[] = $callback; |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @inheritdoc |
75
|
|
|
*/ |
76
|
|
|
public function handle(): self |
77
|
|
|
{ |
78
|
|
|
$output = [$this->output()]; |
79
|
|
|
|
80
|
|
|
foreach ($this->success as $callback) { |
81
|
|
|
$this->call($callback, $output); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @inheritdoc |
89
|
|
|
*/ |
90
|
|
|
public function output(): mixed |
91
|
|
|
{ |
92
|
|
|
return $this->encoder->decode( |
93
|
|
|
$this->process->getOutput() |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param callable $callback |
99
|
|
|
* @param array $arguments |
100
|
|
|
*/ |
101
|
|
|
protected function call(callable $callback, array $arguments): void |
102
|
|
|
{ |
103
|
|
|
call_user_func_array($callback, $arguments); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @inheritdoc |
108
|
|
|
*/ |
109
|
|
|
public function error(): self |
110
|
|
|
{ |
111
|
|
|
$output = $this->getErrorOutput(); |
112
|
|
|
foreach ($this->error as $callback) { |
113
|
|
|
$this->call($callback, [$output->exception()]); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @inheritdoc |
121
|
|
|
*/ |
122
|
|
|
public function getErrorOutput(): mixed |
123
|
|
|
{ |
124
|
|
|
return $this->encoder->decode( |
125
|
|
|
$this->process->getErrorOutput() |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @inheritdoc |
131
|
|
|
*/ |
132
|
|
|
public function id(): int |
133
|
|
|
{ |
134
|
|
|
return $this->id; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @inheritdoc |
139
|
|
|
*/ |
140
|
|
|
public function pid(): int |
141
|
|
|
{ |
142
|
|
|
return $this->pid; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @inheritdoc |
147
|
|
|
*/ |
148
|
|
|
public function start(): self |
149
|
|
|
{ |
150
|
|
|
$this->process->start(); |
151
|
|
|
|
152
|
|
|
$this->pid = $this->process->getPid(); |
153
|
|
|
|
154
|
|
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @inheritdoc |
159
|
|
|
*/ |
160
|
|
|
public function isRunning(): bool |
161
|
|
|
{ |
162
|
|
|
return $this->process->isRunning(); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @inheritdoc |
167
|
|
|
*/ |
168
|
|
|
public function isSuccessful(): bool |
169
|
|
|
{ |
170
|
|
|
return $this->process->isSuccessful(); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @inheritdoc |
175
|
|
|
*/ |
176
|
|
|
public function isTerminated(): bool |
177
|
|
|
{ |
178
|
|
|
return $this->process->isTerminated(); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|