Skip to content

Commit 63c2313

Browse files
committed
Resolve StreamWrapper todo items for read/write differences
WritableStreams cannot be read and are never EOF. ReadableStreams cannot be written.
1 parent 07cbd47 commit 63c2313

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

src/GridFS/StreamWrapper.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ public function stream_close()
5757
*/
5858
public function stream_eof()
5959
{
60+
if ( ! $this->stream instanceof ReadableStream) {
61+
return false;
62+
}
63+
6064
return $this->stream->isEOF();
6165
}
6266

@@ -93,11 +97,14 @@ public function stream_open($path, $mode, $options, &$openedPath)
9397
*
9498
* @see http://php.net/manual/en/streamwrapper.stream-read.php
9599
* @param integer $count Number of bytes to read
96-
* @return string
100+
* @return string
97101
*/
98102
public function stream_read($count)
99103
{
100-
// TODO: Ensure that $this->stream is a ReadableStream
104+
if ( ! $this->stream instanceof ReadableStream) {
105+
return '';
106+
}
107+
101108
return $this->stream->downloadNumBytes($count);
102109
}
103110

@@ -122,13 +129,15 @@ public function stream_stat()
122129
*
123130
* @see http://php.net/manual/en/streamwrapper.stream-write.php
124131
* @param string $data Data to write
125-
* @return integer The number of bytes successfully stored
132+
* @return integer The number of bytes written
126133
*/
127134
public function stream_write($data)
128135
{
129-
// TODO: Ensure that $this->stream is a WritableStream
130-
$this->stream->insertChunks($data);
136+
if ( ! $this->stream instanceof WritableStream) {
137+
return 0;
138+
}
131139

140+
$this->stream->insertChunks($data);
132141
return strlen($data);
133142
}
134143

src/GridFS/WritableStream.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,6 @@ public function insertChunks($toWrite)
184184
return $readBytes;
185185
}
186186

187-
public function isEOF()
188-
{
189-
return $this->isClosed;
190-
}
191-
192187
private function abort()
193188
{
194189
$this->collectionWrapper->deleteChunksByFilesId($this->file['_id']);

0 commit comments

Comments
 (0)