1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Async\Process; |
4
|
|
|
|
5
|
|
|
use ReflectionFunction; |
6
|
|
|
use Throwable; |
7
|
|
|
|
8
|
|
|
trait ProcessCallbacks |
9
|
|
|
{ |
10
|
|
|
protected $successCallbacks = []; |
11
|
|
|
protected $errorCallbacks = []; |
12
|
|
|
protected $timeoutCallbacks = []; |
13
|
|
|
|
14
|
|
|
public function then(callable $callback): self |
15
|
|
|
{ |
16
|
|
|
$this->successCallbacks[] = $callback; |
17
|
|
|
|
18
|
|
|
return $this; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function catch(callable $callback): self |
22
|
|
|
{ |
23
|
|
|
$this->errorCallbacks[] = $callback; |
24
|
|
|
|
25
|
|
|
return $this; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function timeout(callable $callback): self |
29
|
|
|
{ |
30
|
|
|
$this->timeoutCallbacks[] = $callback; |
31
|
|
|
|
32
|
|
|
return $this; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function triggerSuccess() |
36
|
|
|
{ |
37
|
|
|
if ($this->getErrorOutput()) { |
|
|
|
|
38
|
|
|
$this->triggerError(); |
39
|
|
|
|
40
|
|
|
return; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$output = $this->getOutput(); |
|
|
|
|
44
|
|
|
|
45
|
|
|
foreach ($this->successCallbacks as $callback) { |
46
|
|
|
call_user_func_array($callback, [$output]); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $output; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function triggerError() |
53
|
|
|
{ |
54
|
|
|
$exception = $this->resolveErrorOutput(); |
55
|
|
|
|
56
|
|
|
if (! $this->errorCallbacks) { |
57
|
|
|
throw $exception; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
foreach ($this->errorCallbacks as $callback) { |
61
|
|
|
if (! $this->isAllowedThrowableType($exception, $callback)) { |
62
|
|
|
continue; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
call_user_func_array($callback, [$exception]); |
66
|
|
|
|
67
|
|
|
break; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
abstract protected function resolveErrorOutput(): Throwable; |
72
|
|
|
|
73
|
|
|
public function triggerTimeout() |
74
|
|
|
{ |
75
|
|
|
foreach ($this->timeoutCallbacks as $callback) { |
76
|
|
|
call_user_func_array($callback, []); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
protected function isAllowedThrowableType(Throwable $throwable, callable $callable): bool |
81
|
|
|
{ |
82
|
|
|
$reflection = new ReflectionFunction($callable); |
83
|
|
|
|
84
|
|
|
$parameters = $reflection->getParameters(); |
85
|
|
|
|
86
|
|
|
if (! isset($parameters[0])) { |
87
|
|
|
return true; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$firstParameter = $parameters[0]; |
91
|
|
|
|
92
|
|
|
if (! $firstParameter) { |
93
|
|
|
return true; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$type = $firstParameter->getType(); |
97
|
|
|
|
98
|
|
|
if (! $type) { |
99
|
|
|
return true; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if (is_a($throwable, $type->getName())) { |
103
|
|
|
return true; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return false; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.