| 1 |
<?php |
| 2 |
/* |
| 3 |
* This file is part of the ManageWP Worker plugin. |
| 4 |
* |
| 5 |
* (c) ManageWP LLC <contact@managewp.com> |
| 6 |
* |
| 7 |
* For the full copyright and license information, please view the LICENSE |
| 8 |
* file that was distributed with this source code. |
| 9 |
*/ |
| 10 |
|
| 11 |
class MWP_Stream_Stream implements MWP_Stream_Interface |
| 12 |
{ |
| 13 |
|
| 14 |
/** |
| 15 |
* @var resource |
| 16 |
*/ |
| 17 |
private $stream; |
| 18 |
|
| 19 |
/** |
| 20 |
* @var bool |
| 21 |
*/ |
| 22 |
private $seekable; |
| 23 |
|
| 24 |
public function __construct($stream) |
| 25 |
{ |
| 26 |
if (!is_resource($stream)) { |
| 27 |
throw new InvalidArgumentException('Stream must be a resource'); |
| 28 |
} |
| 29 |
|
| 30 |
$this->stream = $stream; |
| 31 |
|
| 32 |
$meta = stream_get_meta_data($this->stream); |
| 33 |
$this->seekable = $meta['seekable']; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Returns true if the stream is at the end of the stream. |
| 38 |
* |
| 39 |
* @return bool |
| 40 |
*/ |
| 41 |
public function eof() |
| 42 |
{ |
| 43 |
return !$this->stream || feof($this->stream); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Read data from the stream |
| 48 |
* |
| 49 |
* @param int $length Read up to $length bytes from the object and return |
| 50 |
* them. Fewer than $length bytes may be returned if |
| 51 |
* underlying stream call returns fewer bytes. |
| 52 |
* |
| 53 |
* @return string Returns the data read from the stream. |
| 54 |
*/ |
| 55 |
public function read($length) |
| 56 |
{ |
| 57 |
return fread($this->stream, $length); |
| 58 |
} |
| 59 |
|
| 60 |
public static function factory($resource) |
| 61 |
{ |
| 62 |
$type = gettype($resource); |
| 63 |
|
| 64 |
if ($type == 'string') { |
| 65 |
$stream = fopen('php://temp', 'r+'); |
| 66 |
if ($resource !== '') { |
| 67 |
fwrite($stream, $resource); |
| 68 |
fseek($stream, 0); |
| 69 |
} |
| 70 |
|
| 71 |
return new self($stream); |
| 72 |
} |
| 73 |
|
| 74 |
if ($type == 'resource') { |
| 75 |
return new self($resource); |
| 76 |
} |
| 77 |
|
| 78 |
if ($resource instanceof MWP_Stream_Interface) { |
| 79 |
return $resource; |
| 80 |
} |
| 81 |
|
| 82 |
throw new InvalidArgumentException('Invalid resource type: '.$type); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* {@inheritdoc} |
| 87 |
*/ |
| 88 |
public function isSeekable() |
| 89 |
{ |
| 90 |
return $this->seekable; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* {@inheritdoc} |
| 95 |
*/ |
| 96 |
public function seek($offset, $whence = SEEK_SET) |
| 97 |
{ |
| 98 |
return $this->seekable |
| 99 |
? fseek($this->stream, $offset, $whence) === 0 |
| 100 |
: false; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* {@inheritdoc} |
| 105 |
*/ |
| 106 |
public function close() |
| 107 |
{ |
| 108 |
if ($this->stream !== null) { |
| 109 |
fclose($this->stream); |
| 110 |
$this->stream = null; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* {@inheritdoc} |
| 116 |
*/ |
| 117 |
public function tell() |
| 118 |
{ |
| 119 |
return $this->stream !== null ? ftell($this->stream) : false; |
| 120 |
} |
| 121 |
|
| 122 |
public function __toString() |
| 123 |
{ |
| 124 |
$buffer = ''; |
| 125 |
|
| 126 |
while (!$this->eof()) { |
| 127 |
$buffer .= $this->read(1048576); |
| 128 |
} |
| 129 |
|
| 130 |
return $buffer; |
| 131 |
} |
| 132 |
} |
| 133 |
|