1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of ReactGuzzleRing. |
5
|
|
|
* |
6
|
|
|
** (c) 2015 Cees-Jan Kiewiet |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
namespace WyriHaximus\React\Guzzle\HttpClient; |
12
|
|
|
|
13
|
|
|
use Psr\Http\Message\StreamInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class Stream |
17
|
|
|
* |
18
|
|
|
* @package WyriHaximus\React\RingPHP\HttpClient |
19
|
|
|
*/ |
20
|
|
|
class Stream implements StreamInterface |
21
|
|
|
{ |
22
|
|
|
const OVERFLOW_LEVEL = 21632; // 2MB |
23
|
|
|
|
24
|
|
|
protected $stream; |
25
|
|
|
protected $eof = false; |
26
|
|
|
protected $size = 0; |
27
|
|
|
protected $buffer = ''; |
28
|
|
|
protected $loop; |
29
|
|
|
protected $overflowStream; |
30
|
|
|
|
31
|
|
|
public function __construct(array $options) |
32
|
|
|
{ |
33
|
|
|
$this->loop = $options['loop']; |
34
|
|
|
|
35
|
|
|
/*if (class_exists('React\Filesystem\Filesystem')) { |
36
|
|
|
$this->setUpFilesystemStream($options); |
37
|
|
|
return; |
38
|
|
|
}*/ |
39
|
|
|
|
40
|
|
|
$this->setUpNormalStream($options); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
protected function setUpFilesystemStream(array $options) |
44
|
|
|
{ |
45
|
|
|
$this->overflowStream = new FileCacheStream([ |
46
|
|
|
'loop' => $this->loop, |
47
|
|
|
]); |
48
|
|
|
|
49
|
|
|
$options['response']->on( |
50
|
|
|
'data', |
51
|
|
|
function ($data) { |
52
|
|
|
if ($this->size >= static::OVERFLOW_LEVEL && $this->overflowStream->isOpen()) { |
53
|
|
|
return $this->overflowStream->write($data); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if ($this->size >= static::OVERFLOW_LEVEL && |
57
|
|
|
!$this->overflowStream->isOpen() && |
58
|
|
|
!$this->overflowStream->isOpening() |
59
|
|
|
) { |
60
|
|
|
$this->overflowStream->open(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$this->buffer .= $data; |
64
|
|
|
$this->size = strlen($this->buffer); |
65
|
|
|
} |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
$options['request']->on( |
69
|
|
|
'end', |
70
|
|
|
function () { |
71
|
|
|
$this->eof = true; |
72
|
|
|
} |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
protected function setUpNormalStream(array $options) |
77
|
|
|
{ |
78
|
|
|
$options['response']->on( |
79
|
|
|
'data', |
80
|
|
|
function ($data) { |
81
|
|
|
$this->buffer .= $data; |
82
|
|
|
$this->size = strlen($this->buffer); |
83
|
|
|
} |
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
$options['request']->on( |
87
|
|
|
'end', |
88
|
|
|
function () { |
89
|
|
|
$this->eof = true; |
90
|
|
|
} |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function eof() |
95
|
|
|
{ |
96
|
|
|
return $this->eof && $this->size === 0; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function getSize() |
100
|
|
|
{ |
101
|
|
|
return $this->size; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function isReadable() |
105
|
|
|
{ |
106
|
|
|
return true; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function tell() |
110
|
|
|
{ |
111
|
|
|
return false; |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function write($string) |
115
|
|
|
{ |
116
|
|
|
return false; |
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function rewind() |
120
|
|
|
{ |
121
|
|
|
return false; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function isWritable() |
125
|
|
|
{ |
126
|
|
|
return false; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function isSeekable() |
130
|
|
|
{ |
131
|
|
|
return false; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function seek($offset, $whence = SEEK_SET) |
135
|
|
|
{ |
136
|
|
|
return false; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function read($length) |
140
|
|
|
{ |
141
|
|
|
$this->toTickOrNotToTick(); |
142
|
|
|
|
143
|
|
|
if (strlen($this->buffer) <= $length) { |
144
|
|
|
$buffer = $this->buffer; |
145
|
|
|
$this->buffer = ''; |
146
|
|
|
$this->size = 0; |
147
|
|
|
return $buffer; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$buffer = substr($this->buffer, 0, $length); |
151
|
|
|
$this->buffer = substr($this->buffer, $length); |
152
|
|
|
$this->size = strlen($this->buffer); |
153
|
|
|
return $buffer; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function getContents($maxLength = -1) |
157
|
|
|
{ |
158
|
|
|
$buffer = ''; |
159
|
|
|
while (!$this->eof()) { |
160
|
|
|
$buffer .= $this->read(1000000); |
161
|
|
|
} |
162
|
|
|
return $buffer; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function __toString() |
166
|
|
|
{ |
167
|
|
|
return $this->getContents(); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function getMetadata($key = null) |
171
|
|
|
{ |
172
|
|
|
$metadata = [ |
173
|
|
|
'timed_out' => '', |
174
|
|
|
'blocked' => false, |
175
|
|
|
'eof' => $this->eof, |
176
|
|
|
'unread_bytes' => '', |
177
|
|
|
'stream_type' => '', |
178
|
|
|
'wrapper_type' => '', |
179
|
|
|
'wrapper_data' => '', |
180
|
|
|
'mode' => '', |
181
|
|
|
'seekable' => false, |
182
|
|
|
'uri' => '', |
183
|
|
|
]; |
184
|
|
|
|
185
|
|
|
if (!$key) { |
|
|
|
|
186
|
|
|
return $metadata; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return isset($metadata[$key]) ? $metadata[$key] : null; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public function attach($stream) |
|
|
|
|
193
|
|
|
{ |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function detach() |
197
|
|
|
{ |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function close() |
201
|
|
|
{ |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
protected function toTickOrNotToTick() |
205
|
|
|
{ |
206
|
|
|
if ($this->size === 0) { |
207
|
|
|
$this->loop->tick(); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* For Guzzle v4 compatibility |
213
|
|
|
*/ |
214
|
|
|
public function flush() |
215
|
|
|
{ |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.