|
1
|
|
|
<?php |
|
2
|
|
|
namespace Wandu\Http\Psr\Stream; |
|
3
|
|
|
|
|
4
|
|
|
use Generator; |
|
5
|
|
|
use IteratorAggregate; |
|
6
|
|
|
use Psr\Http\Message\StreamInterface; |
|
7
|
|
|
use RuntimeException; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @deprecated use IteratorStream |
|
11
|
|
|
*/ |
|
12
|
|
|
class GeneratorStream implements StreamInterface, IteratorAggregate |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var \Generator */ |
|
15
|
|
|
protected $generator; |
|
16
|
|
|
|
|
17
|
|
|
/** @var string */ |
|
18
|
|
|
protected $cachedContents; |
|
19
|
|
|
|
|
20
|
|
|
/** @var bool */ |
|
21
|
|
|
protected $isEof = false; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param \Generator $generator |
|
25
|
|
|
*/ |
|
26
|
5 |
|
public function __construct(Generator $generator) |
|
27
|
|
|
{ |
|
28
|
5 |
|
$this->generator = $generator; |
|
29
|
5 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* {@inheritdoc} |
|
33
|
|
|
*/ |
|
34
|
1 |
|
public function __toString() |
|
35
|
|
|
{ |
|
36
|
1 |
|
$this->rewind(); |
|
37
|
1 |
|
return $this->getContents(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritdoc} |
|
42
|
|
|
*/ |
|
43
|
|
|
public function getIterator() |
|
44
|
|
|
{ |
|
45
|
|
|
return $this->generator; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
|
|
*/ |
|
51
|
|
|
public function close() |
|
52
|
|
|
{ |
|
53
|
|
|
throw new RuntimeException('can not use the close method.'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritdoc} |
|
58
|
|
|
*/ |
|
59
|
|
|
public function detach() |
|
60
|
|
|
{ |
|
61
|
|
|
throw new RuntimeException('can not use the detach method.'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* {@inheritdoc} |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getSize() |
|
68
|
|
|
{ |
|
69
|
|
|
throw new RuntimeException('GeneratorStream cannot getSize.'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* {@inheritdoc} |
|
74
|
|
|
*/ |
|
75
|
|
|
public function tell() |
|
76
|
|
|
{ |
|
77
|
|
|
throw new RuntimeException('GeneratorStream cannot tell.'); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* {@inheritdoc} |
|
82
|
|
|
*/ |
|
83
|
2 |
|
public function eof() |
|
84
|
|
|
{ |
|
85
|
2 |
|
return $this->isEof; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* {@inheritdoc} |
|
90
|
|
|
*/ |
|
91
|
1 |
|
public function isSeekable() |
|
92
|
|
|
{ |
|
93
|
1 |
|
return false; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* {@inheritdoc} |
|
98
|
|
|
*/ |
|
99
|
1 |
|
public function seek($offset, $whence = SEEK_SET) |
|
100
|
|
|
{ |
|
101
|
1 |
|
throw new RuntimeException('GeneratorStream cannot seek.'); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* {@inheritdoc} |
|
106
|
|
|
*/ |
|
107
|
2 |
|
public function rewind() |
|
108
|
|
|
{ |
|
109
|
|
|
// $this->generator->rewind(); |
|
|
|
|
|
|
110
|
2 |
|
$this->isEof = false; |
|
111
|
2 |
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* {@inheritdoc} |
|
115
|
|
|
*/ |
|
116
|
1 |
|
public function isWritable() |
|
117
|
|
|
{ |
|
118
|
1 |
|
return false; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* {@inheritdoc} |
|
123
|
|
|
*/ |
|
124
|
1 |
|
public function write($string) |
|
125
|
|
|
{ |
|
126
|
1 |
|
throw new RuntimeException('GeneratorStream cannot write.'); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* {@inheritdoc} |
|
131
|
|
|
*/ |
|
132
|
1 |
|
public function isReadable() |
|
133
|
|
|
{ |
|
134
|
1 |
|
return false; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* {@inheritdoc} |
|
139
|
|
|
*/ |
|
140
|
1 |
|
public function read($length) |
|
141
|
|
|
{ |
|
142
|
1 |
|
throw new RuntimeException('GeneratorStream cannot read.'); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* {@inheritdoc} |
|
147
|
|
|
*/ |
|
148
|
2 |
|
public function getContents() |
|
149
|
|
|
{ |
|
150
|
2 |
|
if (!$this->eof()) { |
|
151
|
2 |
|
if (!isset($this->cachedContents)) { |
|
152
|
2 |
|
$contents = ''; |
|
153
|
2 |
|
foreach ($this->generator as $value) { |
|
154
|
2 |
|
$contents .= $value; |
|
155
|
|
|
} |
|
156
|
2 |
|
$this->cachedContents = $contents; |
|
157
|
|
|
} |
|
158
|
2 |
|
$this->isEof = true; |
|
159
|
2 |
|
return $this->cachedContents; |
|
160
|
|
|
} |
|
161
|
1 |
|
return ''; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* {@inheritdoc} |
|
166
|
|
|
*/ |
|
167
|
|
|
public function getMetadata($key = null) |
|
168
|
|
|
{ |
|
169
|
|
|
$metadata = [ |
|
170
|
|
|
'eof' => $this->eof(), |
|
171
|
|
|
'stream_type' => 'generator', |
|
172
|
|
|
'seekable' => false |
|
173
|
|
|
]; |
|
174
|
|
|
if (!isset($key)) { |
|
175
|
|
|
return $metadata; |
|
176
|
|
|
} |
|
177
|
|
|
if (!array_key_exists($key, $metadata)) { |
|
178
|
|
|
return null; |
|
179
|
|
|
} |
|
180
|
|
|
return $metadata[$key]; |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.