1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dazzle\Stream\Sync; |
4
|
|
|
|
5
|
|
|
use Dazzle\Stream\StreamSeeker; |
6
|
|
|
use Dazzle\Throwable\Exception\Runtime\ReadException; |
7
|
|
|
use Dazzle\Throwable\Exception\Logic\InvalidArgumentException; |
8
|
|
|
|
9
|
|
|
class StreamReader extends StreamSeeker implements StreamReaderInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var bool |
13
|
|
|
*/ |
14
|
|
|
protected $readable; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var int |
18
|
|
|
*/ |
19
|
|
|
protected $bufferSize; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param resource $resource |
23
|
|
|
* @param bool $autoClose |
24
|
|
|
* @throws InvalidArgumentException |
25
|
|
|
*/ |
26
|
21 |
|
public function __construct($resource, $autoClose = true) |
27
|
|
|
{ |
28
|
21 |
|
parent::__construct($resource, $autoClose); |
29
|
|
|
|
30
|
20 |
|
$this->readable = true; |
31
|
20 |
|
$this->bufferSize = 4096; |
32
|
20 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* |
36
|
|
|
*/ |
37
|
15 |
|
public function __destruct() |
38
|
|
|
{ |
39
|
15 |
|
unset($this->readable); |
40
|
15 |
|
unset($this->bufferSize); |
41
|
|
|
|
42
|
15 |
|
parent::__destruct(); |
43
|
15 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @override |
47
|
|
|
* @inheritDoc |
48
|
|
|
*/ |
49
|
2 |
|
public function isReadable() |
50
|
|
|
{ |
51
|
2 |
|
return $this->readable; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @override |
56
|
|
|
* @inheritDoc |
57
|
|
|
*/ |
58
|
1 |
|
public function setBufferSize($bufferSize) |
59
|
|
|
{ |
60
|
1 |
|
$this->bufferSize = $bufferSize; |
61
|
1 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @override |
65
|
|
|
* @inheritDoc |
66
|
|
|
*/ |
67
|
2 |
|
public function getBufferSize() |
68
|
|
|
{ |
69
|
2 |
|
return $this->bufferSize; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @override |
74
|
|
|
* @inheritDoc |
75
|
|
|
*/ |
76
|
2 |
View Code Duplication |
public function read($length = null) |
|
|
|
|
77
|
|
|
{ |
78
|
2 |
|
if (!$this->readable) |
79
|
|
|
{ |
80
|
|
|
return $this->throwAndEmitException( |
81
|
|
|
new ReadException('Stream is no longer readable.') |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
2 |
|
if ($length === null) |
86
|
|
|
{ |
87
|
2 |
|
$length = $this->bufferSize; |
88
|
|
|
} |
89
|
|
|
|
90
|
2 |
|
$ret = fread($this->resource, $length); |
91
|
|
|
|
92
|
2 |
|
if ($ret === false) |
93
|
|
|
{ |
94
|
|
|
return $this->throwAndEmitException( |
95
|
|
|
new ReadException('Cannot read stream.') |
96
|
|
|
); |
97
|
|
|
} |
98
|
2 |
|
else if ($ret !== '') |
99
|
|
|
{ |
100
|
2 |
|
$this->emit('data', [ $this, $ret ]); |
101
|
|
|
|
102
|
2 |
|
if (strlen($ret) < $length) |
103
|
|
|
{ |
104
|
2 |
|
$this->emit('end', [ $this ]); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
2 |
|
return $ret; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @override |
113
|
|
|
* @inheritDoc |
114
|
|
|
*/ |
115
|
5 |
View Code Duplication |
public function close() |
|
|
|
|
116
|
|
|
{ |
117
|
5 |
|
if ($this->closing) |
118
|
|
|
{ |
119
|
|
|
return; |
120
|
|
|
} |
121
|
|
|
|
122
|
5 |
|
$this->closing = true; |
123
|
5 |
|
$this->readable = false; |
124
|
5 |
|
$this->writable = false; |
|
|
|
|
125
|
|
|
|
126
|
5 |
|
$this->emit('close', [ $this ]); |
127
|
5 |
|
$this->handleClose(); |
128
|
5 |
|
$this->emit('done', [ $this ]); |
129
|
5 |
|
} |
130
|
|
|
} |
131
|
|
|
|
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.