1 | <?php |
||
25 | class StreamInputStream implements InputStream |
||
26 | { |
||
27 | /** |
||
28 | * @var resource |
||
29 | */ |
||
30 | private $stream; |
||
31 | |||
32 | /** |
||
33 | * Creates the input. |
||
34 | * |
||
35 | * @param resource $stream A stream resource. |
||
36 | */ |
||
37 | 250 | public function __construct($stream) |
|
46 | |||
47 | /** |
||
48 | * {@inheritdoc} |
||
49 | */ |
||
50 | 7 | public function read($length) |
|
68 | |||
69 | /** |
||
70 | * {@inheritdoc} |
||
71 | */ |
||
72 | 19 | public function readLine($length = null) |
|
73 | { |
||
74 | 19 | if (null === $this->stream) { |
|
75 | 1 | throw new IOException('Cannot read from a closed input.'); |
|
76 | } |
||
77 | |||
78 | 18 | if (feof($this->stream)) { |
|
79 | 3 | return null; |
|
80 | } |
||
81 | |||
82 | 18 | if (null !== $length) { |
|
83 | 3 | $data = fgets($this->stream, $length); |
|
84 | } else { |
||
85 | 18 | $data = fgets($this->stream); |
|
86 | } |
||
87 | |||
88 | 18 | if (false === $data && !feof($this->stream)) { |
|
89 | throw new IOException('Could not read stream.'); |
||
90 | } |
||
91 | |||
92 | 18 | return $data ?: null; |
|
93 | } |
||
94 | |||
95 | /** |
||
96 | * {@inheritdoc} |
||
97 | */ |
||
98 | 3 | public function close() |
|
99 | { |
||
100 | 3 | if ($this->stream) { |
|
101 | 3 | @fclose($this->stream); |
|
102 | 3 | $this->stream = null; |
|
103 | } |
||
104 | 3 | } |
|
105 | |||
106 | /** |
||
107 | * {@inheritdoc} |
||
108 | */ |
||
109 | public function isClosed() |
||
113 | } |
||
114 |
If you suppress an error, we recommend checking for the error condition explicitly: