| 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_Buffer |
| 12 |
{ |
| 13 |
|
| 14 |
private $hwm; |
| 15 |
|
| 16 |
private $buffer = ''; |
| 17 |
|
| 18 |
/** |
| 19 |
* @param int $hwm High water mark, representing the preferred maximum |
| 20 |
* buffer size. If the size of the buffer exceeds the high |
| 21 |
* water mark, then calls to write will continue to succeed |
| 22 |
* but will return false to inform writers to slow down |
| 23 |
* until the buffer has been drained by reading from it. |
| 24 |
*/ |
| 25 |
public function __construct($hwm = 16384) |
| 26 |
{ |
| 27 |
$this->hwm = $hwm; |
| 28 |
} |
| 29 |
|
| 30 |
public function close() |
| 31 |
{ |
| 32 |
$this->buffer = ''; |
| 33 |
} |
| 34 |
|
| 35 |
public function isSeekable() |
| 36 |
{ |
| 37 |
return false; |
| 38 |
} |
| 39 |
|
| 40 |
public function seek($offset, $whence = SEEK_SET) |
| 41 |
{ |
| 42 |
return false; |
| 43 |
} |
| 44 |
|
| 45 |
public function eof() |
| 46 |
{ |
| 47 |
return strlen($this->buffer) === 0; |
| 48 |
} |
| 49 |
|
| 50 |
public function tell() |
| 51 |
{ |
| 52 |
return false; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Reads data from the buffer. |
| 57 |
* |
| 58 |
* @param $length |
| 59 |
* |
| 60 |
* @return string |
| 61 |
*/ |
| 62 |
public function read($length) |
| 63 |
{ |
| 64 |
$currentLength = strlen($this->buffer); |
| 65 |
|
| 66 |
if ($length >= $currentLength) { |
| 67 |
// No need to slice the buffer because we don't have enough data. |
| 68 |
$result = $this->buffer; |
| 69 |
$this->buffer = ''; |
| 70 |
} else { |
| 71 |
// Slice up the result to provide a subset of the buffer. |
| 72 |
$result = substr($this->buffer, 0, $length); |
| 73 |
$this->buffer = substr($this->buffer, $length); |
| 74 |
} |
| 75 |
|
| 76 |
return $result; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Writes data to the buffer. |
| 81 |
* |
| 82 |
* @param $string |
| 83 |
* |
| 84 |
* @return bool|int |
| 85 |
*/ |
| 86 |
public function write($string) |
| 87 |
{ |
| 88 |
$this->buffer .= $string; |
| 89 |
|
| 90 |
if (strlen($this->buffer) >= $this->hwm) { |
| 91 |
return false; |
| 92 |
} |
| 93 |
|
| 94 |
return strlen($string); |
| 95 |
} |
| 96 |
} |
| 97 |
|