1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* DeferredCallChain |
4
|
|
|
* |
5
|
|
|
* @package php-deferred-callchain |
6
|
|
|
* @author Jean Claveau |
7
|
|
|
*/ |
8
|
|
|
namespace JClaveau\Async; |
9
|
|
|
use BadMethodCallException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* This class stores an arbitrary stack of calls (methods or array entries access) |
13
|
|
|
* that will be callable on any future variable. |
14
|
|
|
*/ |
15
|
|
|
class DeferredCallChain implements \JsonSerializable, \ArrayAccess |
16
|
|
|
{ |
17
|
|
|
use Jclaveau\Traits\Fluent\New_; |
|
|
|
|
18
|
|
|
|
19
|
|
|
/** @var array $stack The stack of deferred calls */ |
20
|
|
|
protected $stack = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* ArrayAccess interface |
24
|
|
|
* |
25
|
|
|
* @param string $key The entry to acces |
26
|
|
|
*/ |
27
|
|
|
public function &offsetGet($key) |
28
|
|
|
{ |
29
|
|
|
$this->stack[] = [ |
30
|
|
|
'entry' => $key, |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
return $this; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Stores any call in the the stack. |
38
|
|
|
* |
39
|
|
|
* @param string $method |
40
|
|
|
* @param array $arguments |
41
|
|
|
* |
42
|
|
|
* @return $this |
43
|
|
|
*/ |
44
|
|
|
public final function __call($method, array $arguments) |
45
|
|
|
{ |
46
|
|
|
$this->stack[] = [ |
47
|
|
|
'method' => $method, |
48
|
|
|
'arguments' => $arguments, |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* For implementing JsonSerializable interface. |
56
|
|
|
* |
57
|
|
|
* @see https://secure.php.net/manual/en/jsonserializable.jsonserialize.php |
58
|
|
|
*/ |
59
|
|
|
public function jsonSerialize() |
60
|
|
|
{ |
61
|
|
|
return $this->stack; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Outputs the PHP code producing the current call chain while it's casted |
66
|
|
|
* as a string. |
67
|
|
|
* |
68
|
|
|
* @return string The PHP code corresponding to this call chain |
69
|
|
|
*/ |
70
|
|
|
public function __toString() |
71
|
|
|
{ |
72
|
|
|
$string = '(new ' . get_called_class() . ')'; |
73
|
|
|
|
74
|
|
|
foreach ($this->stack as $i => $call) { |
75
|
|
|
if (isset($call['method'])) { |
76
|
|
|
$string .= '->'; |
77
|
|
|
$string .= $call['method'].'('; |
78
|
|
|
$string .= implode(', ', array_map(function($argument) { |
79
|
|
|
return var_export($argument, true); |
80
|
|
|
}, $call['arguments'])); |
81
|
|
|
$string .= ')'; |
82
|
|
|
} |
83
|
|
|
else { |
84
|
|
|
$string .= '[' . var_export($call['entry'], true) . ']'; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $string; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Invoking the instance produces the call of the stack |
93
|
|
|
* |
94
|
|
|
* @param $target The target to apply the callchain on |
|
|
|
|
95
|
|
|
* @return The value returned once the call chain is called uppon $target |
96
|
|
|
*/ |
97
|
|
|
public function __invoke($target) |
98
|
|
|
{ |
99
|
|
|
$out = $target; |
100
|
|
|
foreach ($this->stack as $i => $call) { |
101
|
|
|
try { |
102
|
|
|
if (isset($call['method'])) { |
103
|
|
|
$out = call_user_func_array([$out, $call['method']], $call['arguments']); |
104
|
|
|
} |
105
|
|
|
else { |
106
|
|
|
$out = $out[ $call['entry'] ]; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
catch (\Exception $e) { |
110
|
|
|
// Throw $e with the good stack (usage exception) |
111
|
|
|
throw $e; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $out; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Unused part of the ArrayAccess interface |
120
|
|
|
* |
121
|
|
|
* @param $offset |
122
|
|
|
* @param $value |
123
|
|
|
* @throws \BadMethodCallException |
124
|
|
|
*/ |
125
|
|
|
public function offsetSet($offset, $value) |
126
|
|
|
{ |
127
|
|
|
throw new BadMethodCallException( |
128
|
|
|
"not implemented" |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Unused part of the ArrayAccess interface |
134
|
|
|
* |
135
|
|
|
* @param $offset |
136
|
|
|
* @throws \BadMethodCallException |
137
|
|
|
*/ |
138
|
|
|
public function offsetExists($offset) |
139
|
|
|
{ |
140
|
|
|
throw new BadMethodCallException( |
141
|
|
|
"not implemented" |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Unused part of the ArrayAccess interface |
147
|
|
|
* |
148
|
|
|
* @param $offset |
149
|
|
|
* @throws \BadMethodCallException |
150
|
|
|
*/ |
151
|
|
|
public function offsetUnset($offset) |
152
|
|
|
{ |
153
|
|
|
throw new BadMethodCallException( |
154
|
|
|
"not implemented" |
155
|
|
|
); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/**/ |
159
|
|
|
} |
160
|
|
|
|