1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dazzle\Stream; |
4
|
|
|
|
5
|
|
|
use Dazzle\Event\BaseEventEmitter; |
6
|
|
|
use Dazzle\Throwable\Exception\Runtime\ReadException; |
7
|
|
|
use Dazzle\Throwable\Exception\Runtime\WriteException; |
8
|
|
|
use Dazzle\Throwable\Exception\Logic\InvalidArgumentException; |
9
|
|
|
use Error; |
10
|
|
|
use Exception; |
11
|
|
|
|
12
|
|
|
class StreamSeeker extends BaseEventEmitter implements StreamSeekerInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var resource |
16
|
|
|
*/ |
17
|
|
|
protected $resource; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var bool |
21
|
|
|
*/ |
22
|
|
|
protected $autoClose; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var bool |
26
|
|
|
*/ |
27
|
|
|
protected $closing; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param resource $resource |
31
|
|
|
* @param bool $autoClose |
32
|
|
|
* @throws InvalidArgumentException |
33
|
|
|
*/ |
34
|
115 |
|
public function __construct($resource, $autoClose = true) |
35
|
|
|
{ |
36
|
115 |
|
if (!is_resource($resource)) |
37
|
|
|
{ |
38
|
7 |
|
throw new InvalidArgumentException('First parameter must be a valid resource.'); |
39
|
|
|
} |
40
|
|
|
|
41
|
108 |
|
$this->resource = $resource; |
42
|
108 |
|
$this->autoClose = $autoClose; |
43
|
108 |
|
$this->closing = false; |
44
|
|
|
|
45
|
108 |
|
if (function_exists('stream_set_blocking')) |
46
|
|
|
{ |
47
|
108 |
|
stream_set_blocking($this->resource, 0); |
48
|
|
|
} |
49
|
108 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* |
53
|
|
|
*/ |
54
|
89 |
|
public function __destruct() |
55
|
|
|
{ |
56
|
89 |
|
$this->handleClose(); |
57
|
|
|
|
58
|
89 |
|
parent::__destruct(); |
59
|
|
|
|
60
|
89 |
|
unset($this->resource); |
61
|
89 |
|
unset($this->autoClose); |
62
|
89 |
|
unset($this->closing); |
63
|
89 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @override |
67
|
|
|
* @inheritDoc |
68
|
|
|
*/ |
69
|
36 |
|
public function getResource() |
70
|
|
|
{ |
71
|
36 |
|
return $this->resource; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @override |
76
|
|
|
* @inheritDoc |
77
|
|
|
*/ |
78
|
7 |
|
public function getResourceId() |
79
|
|
|
{ |
80
|
7 |
|
return (int) $this->resource; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @override |
85
|
|
|
* @inheritDoc |
86
|
|
|
*/ |
87
|
40 |
|
public function getMetadata() |
88
|
|
|
{ |
89
|
40 |
|
return stream_get_meta_data($this->resource); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @override |
94
|
|
|
* @inheritDoc |
95
|
|
|
*/ |
96
|
|
|
public function getStreamType() |
97
|
|
|
{ |
98
|
|
|
return $this->getMetadata()['stream_type']; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @override |
103
|
|
|
* @inheritDoc |
104
|
|
|
*/ |
105
|
|
|
public function getWrapperType() |
106
|
|
|
{ |
107
|
|
|
return $this->getMetadata()['wrapper_type']; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @override |
112
|
|
|
* @inheritDoc |
113
|
|
|
*/ |
114
|
21 |
|
public function isOpen() |
115
|
|
|
{ |
116
|
21 |
|
return !$this->closing; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @override |
121
|
|
|
* @inheritDoc |
122
|
|
|
*/ |
123
|
33 |
|
public function isSeekable() |
124
|
|
|
{ |
125
|
33 |
|
return $this->getMetadata()['seekable']; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @override |
130
|
|
|
* @inheritDoc |
131
|
|
|
*/ |
132
|
14 |
|
public function tell() |
133
|
|
|
{ |
134
|
14 |
|
if (!$this->isSeekable()) |
135
|
|
|
{ |
136
|
|
|
throw new ReadException('Cannt tell offset of this kind of stream.'); |
137
|
|
|
} |
138
|
|
|
|
139
|
14 |
|
$ret = ftell($this->resource); |
140
|
14 |
|
if ($ret === false) |
141
|
|
|
{ |
142
|
|
|
throw new ReadException('Cannot tell offset of stream.'); |
143
|
|
|
} |
144
|
|
|
|
145
|
14 |
|
return $ret; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @override |
150
|
|
|
* @inheritDoc |
151
|
|
|
*/ |
152
|
7 |
|
public function seek($offset, $whence = SEEK_SET) |
153
|
|
|
{ |
154
|
7 |
|
if (!$this->isSeekable()) |
155
|
|
|
{ |
156
|
|
|
throw new WriteException('Cannt seek on this kind of stream.'); |
157
|
|
|
} |
158
|
|
|
|
159
|
7 |
|
$pointer = fseek($this->resource, $offset, $whence); |
160
|
7 |
|
if ($pointer === false) |
161
|
|
|
{ |
162
|
|
|
throw new WriteException('Cannot seek on stream.'); |
163
|
|
|
} |
164
|
|
|
|
165
|
7 |
|
$this->emit('seek', [ $this, $pointer ]); |
166
|
7 |
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @override |
170
|
|
|
* @inheritDoc |
171
|
|
|
*/ |
172
|
12 |
|
public function rewind() |
173
|
|
|
{ |
174
|
12 |
|
if (!$this->isSeekable()) |
175
|
|
|
{ |
176
|
|
|
throw new WriteException('Cannt rewind this kind of stream.'); |
177
|
|
|
} |
178
|
|
|
|
179
|
12 |
|
if (false === rewind($this->resource)) |
180
|
|
|
{ |
181
|
|
|
throw new WriteException('Cannot rewind stream.'); |
182
|
|
|
} |
183
|
12 |
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @override |
187
|
|
|
* @inheritDoc |
188
|
|
|
*/ |
189
|
6 |
View Code Duplication |
public function close() |
|
|
|
|
190
|
|
|
{ |
191
|
6 |
|
if ($this->closing) |
192
|
|
|
{ |
193
|
|
|
return; |
194
|
|
|
} |
195
|
|
|
|
196
|
6 |
|
$this->closing = true; |
197
|
6 |
|
$this->readable = false; |
|
|
|
|
198
|
6 |
|
$this->writable = false; |
|
|
|
|
199
|
|
|
|
200
|
6 |
|
$this->emit('close', [ $this ]); |
201
|
6 |
|
$this->handleClose(); |
202
|
6 |
|
$this->emit('done', [ $this ]); |
203
|
6 |
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Emit error event and the throws it too. |
207
|
|
|
* |
208
|
|
|
* @param Error|Exception $ex |
209
|
|
|
* @return null |
210
|
|
|
* @throws Error|Exception |
211
|
|
|
*/ |
212
|
|
|
protected function throwAndEmitException($ex) |
213
|
|
|
{ |
214
|
|
|
$this->emit('error', [ $this, $ex ]); |
215
|
|
|
throw $ex; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Handle the close of the stream object. |
220
|
|
|
* |
221
|
|
|
* @internal |
222
|
|
|
*/ |
223
|
100 |
|
public function handleClose() |
224
|
|
|
{ |
225
|
100 |
|
if ($this->autoClose === true && is_resource($this->resource)) |
226
|
|
|
{ |
227
|
100 |
|
fclose($this->resource); |
228
|
|
|
} |
229
|
100 |
|
} |
230
|
|
|
} |
231
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.