PluginProbe
ManageWP Worker / 4.9.25
ManageWP Worker v4.9.25
4.9.38 4.9.37 4.9.36 4.9.35 4.9.34 3.8.7 3.8.8 3.9.0 3.9.1 3.9.10 3.9.11 3.9.12 3.9.13 3.9.14 3.9.15 3.9.16 3.9.17 3.9.18 3.9.19 3.9.2 3.9.20 3.9.21 3.9.22 3.9.23 3.9.24 All 73 releases
worker / src / MWP / Stream / LazyFile.php

LazyFile.php in ManageWP Worker 4.9.25, at src/MWP/Stream/LazyFile.php

133 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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