| 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 |
/** |
| 12 |
* Lazily open file stream for reading and close it when EOF is reached. |
| 13 |
*/ |
| 14 |
class MWP_Stream_LazyFile implements MWP_Stream_Interface |
| 15 |
{ |
| 16 |
|
| 17 |
/** |
| 18 |
* @var MWP_Stream_Interface |
| 19 |
*/ |
| 20 |
private $stream = null; |
| 21 |
|
| 22 |
/** |
| 23 |
* @var mixed |
| 24 |
*/ |
| 25 |
private $realPath; |
| 26 |
|
| 27 |
/** |
| 28 |
* @var bool |
| 29 |
*/ |
| 30 |
private $initialized = false; |
| 31 |
|
| 32 |
public function __construct($realPath) |
| 33 |
{ |
| 34 |
$this->realPath = $realPath; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* {@inheritdoc} |
| 39 |
*/ |
| 40 |
public function close() |
| 41 |
{ |
| 42 |
if ($this->stream !== null) { |
| 43 |
$this->stream->close(); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* {@inheritdoc} |
| 49 |
*/ |
| 50 |
public function tell() |
| 51 |
{ |
| 52 |
if (!$this->initialized) { |
| 53 |
return 0; |
| 54 |
} |
| 55 |
|
| 56 |
return $this->stream !== null ? $this->stream->tell() : false; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* {@inheritdoc} |
| 61 |
*/ |
| 62 |
public function isSeekable() |
| 63 |
{ |
| 64 |
$this->initialize(); |
| 65 |
|
| 66 |
return $this->stream !== null ? $this->stream->isSeekable() : false; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* {@inheritdoc} |
| 71 |
*/ |
| 72 |
public function seek($offset, $whence = SEEK_SET) |
| 73 |
{ |
| 74 |
$this->initialize(); |
| 75 |
|
| 76 |
return $this->stream !== null ? $this->stream->seek($offset, $whence) : false; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* {@inheritdoc} |
| 81 |
*/ |
| 82 |
public function eof() |
| 83 |
{ |
| 84 |
$this->initialize(); |
| 85 |
|
| 86 |
return $this->stream !== null ? $this->stream->eof() : true; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* {@inheritdoc} |
| 91 |
*/ |
| 92 |
public function read($length) |
| 93 |
{ |
| 94 |
$this->initialize(); |
| 95 |
|
| 96 |
if ($this->stream === null) { |
| 97 |
return null; |
| 98 |
} |
| 99 |
|
| 100 |
$data = $this->stream->read($length); |
| 101 |
|
| 102 |
if ($this->eof()) { |
| 103 |
$this->close(); |
| 104 |
} |
| 105 |
|
| 106 |
return $data; |
| 107 |
} |
| 108 |
|
| 109 |
public function __toString() |
| 110 |
{ |
| 111 |
$buffer = ''; |
| 112 |
|
| 113 |
while (!$this->eof()) { |
| 114 |
$buffer .= $this->read(1048576); |
| 115 |
} |
| 116 |
|
| 117 |
return $buffer; |
| 118 |
} |
| 119 |
|
| 120 |
private function initialize() |
| 121 |
{ |
| 122 |
if ($this->initialized === false) { |
| 123 |
if (file_exists($this->realPath)) { |
| 124 |
$handle = @fopen($this->realPath, "rb"); |
| 125 |
if ($handle !== false) { |
| 126 |
$this->stream = MWP_Stream_Stream::factory($handle); |
| 127 |
} |
| 128 |
} |
| 129 |
$this->initialized = true; |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|