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