1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace mpyw\Co\Internal; |
4
|
|
|
use mpyw\Co\CoInterface; |
5
|
|
|
|
6
|
|
|
class GeneratorContainer |
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* Generator. |
10
|
|
|
* @var \Generator |
11
|
|
|
*/ |
12
|
|
|
private $g; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Generator object hash. |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private $h; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Thrown exception. |
22
|
|
|
* @var \RuntimeException |
23
|
|
|
*/ |
24
|
|
|
private $e; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Parent yield key. |
28
|
|
|
* @var mixed |
29
|
|
|
*/ |
30
|
|
|
private $yieldKey; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Constructor. |
34
|
|
|
* @param \Generator $g |
35
|
|
|
*/ |
36
|
42 |
|
public function __construct(\Generator $g, $yield_key = null) |
37
|
42 |
|
{ |
38
|
42 |
|
$this->g = $g; |
39
|
42 |
|
$this->h = spl_object_hash($g); |
40
|
42 |
|
$this->yieldKey = $yield_key; |
41
|
42 |
|
$this->valid(); |
42
|
42 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Return parent yield key. |
46
|
|
|
* @return mixed |
47
|
|
|
*/ |
48
|
27 |
|
public function getYieldKey() |
49
|
27 |
|
{ |
50
|
27 |
|
return $this->yieldKey; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Return generator hash. |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
28 |
|
public function __toString() |
58
|
28 |
|
{ |
59
|
28 |
|
return $this->h; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Return whether generator is actually working. |
64
|
|
|
* @return bool |
65
|
|
|
*/ |
66
|
42 |
|
public function valid() |
67
|
42 |
|
{ |
68
|
|
|
try { |
69
|
42 |
|
$this->g->current(); |
70
|
42 |
|
return $this->e === null && $this->g->valid() && $this->g->key() !== CoInterface::RETURN_WITH; |
71
|
2 |
|
} catch (\Throwable $e) {} catch (\Exception $e) {} |
|
|
|
|
72
|
2 |
|
$this->e = $e; |
|
|
|
|
73
|
2 |
|
return false; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Return current key. |
78
|
|
|
* @return mixed |
79
|
|
|
*/ |
80
|
32 |
|
public function key() |
81
|
32 |
|
{ |
82
|
32 |
|
$this->validateValidity(); |
83
|
32 |
|
return $this->g->key(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Return current value. |
88
|
|
|
* @return mixed |
89
|
|
|
*/ |
90
|
34 |
|
public function current() |
91
|
34 |
|
{ |
92
|
34 |
|
$this->validateValidity(); |
93
|
34 |
|
return $this->g->current(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Send value into generator. |
98
|
|
|
* @param mixed $value |
99
|
|
|
* @NOTE: This method returns nothing, |
100
|
|
|
* while original generator returns something. |
101
|
|
|
*/ |
102
|
30 |
View Code Duplication |
public function send($value) |
|
|
|
|
103
|
30 |
|
{ |
104
|
30 |
|
$this->validateValidity(); |
105
|
|
|
try { |
106
|
30 |
|
$this->g->send($value); |
107
|
27 |
|
return; |
108
|
19 |
|
} catch (\Throwable $e) {} catch (\Exception $e) {} |
|
|
|
|
109
|
19 |
|
$this->e = $e; |
|
|
|
|
110
|
19 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Throw exception into generator. |
114
|
|
|
* @param \Throwable|\RuntimeException $e |
115
|
|
|
* @NOTE: This method returns nothing, |
116
|
|
|
* while original generator returns something. |
117
|
|
|
*/ |
118
|
19 |
View Code Duplication |
public function throw_($e) |
|
|
|
|
119
|
19 |
|
{ |
120
|
19 |
|
$this->validateValidity(); |
121
|
|
|
try { |
122
|
19 |
|
$this->g->throw($e); |
123
|
15 |
|
return; |
124
|
13 |
|
} catch (\Throwable $e) {} catch (\Exception $e) {} |
|
|
|
|
125
|
13 |
|
$this->e = $e; |
|
|
|
|
126
|
13 |
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Return whether Throwable is thrown. |
130
|
|
|
* @return bool |
131
|
|
|
*/ |
132
|
30 |
|
public function thrown() |
133
|
30 |
|
{ |
134
|
30 |
|
return $this->e !== null; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Return value that generator has returned or thrown. |
139
|
|
|
* @return mixed |
140
|
|
|
*/ |
141
|
33 |
|
public function getReturnOrThrown() |
142
|
33 |
|
{ |
143
|
33 |
|
$this->validateInvalidity(); |
144
|
32 |
|
if ($this->e === null && $this->g->valid() && !$this->valid()) { |
145
|
9 |
|
return $this->g->current(); |
146
|
|
|
} |
147
|
31 |
|
if ($this->e) { |
148
|
23 |
|
return $this->e; |
149
|
|
|
} |
150
|
28 |
|
return method_exists($this->g, 'getReturn') ? $this->g->getReturn() : null; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Validate that generator has finished running. |
155
|
|
|
* @throws LogicException |
156
|
|
|
*/ |
157
|
35 |
|
private function validateValidity() |
158
|
35 |
|
{ |
159
|
35 |
|
if (!$this->valid()) { |
160
|
2 |
|
throw new \BadMethodCallException('Unreachable here.'); |
161
|
|
|
} |
162
|
35 |
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Validate that generator is still running. |
166
|
|
|
* @throws LogicException |
167
|
|
|
*/ |
168
|
33 |
|
private function validateInvalidity() |
169
|
33 |
|
{ |
170
|
33 |
|
if ($this->valid()) { |
171
|
1 |
|
throw new \BadMethodCallException('Unreachable here.'); |
172
|
|
|
} |
173
|
32 |
|
} |
174
|
|
|
} |
175
|
|
|
|