PluginProbe
ManageWP Worker / 4.9.29
ManageWP Worker v4.9.29
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 / Callable.php

Callable.php in ManageWP Worker 4.9.29, at src/MWP/Stream/Callable.php

125 lines 2.9 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 class MWP_Stream_Callable implements MWP_Stream_Interface
12 {
13
14 /** @var callable */
15 private $source;
16
17 /** @var int */
18 private $tellPos = 0;
19
20 /** @var MWP_Stream_Buffer */
21 private $buffer;
22
23 /** @var array */
24 private $arguments;
25
26 /**
27 * @param callable $source Source of the stream data. The callable MAY
28 * accept an integer argument used to control the
29 * amount of data to return. The callable MUST
30 * return a string when called, or false on error
31 * or EOF.
32 * @param array $arguments
33 */
34 public function __construct($source, array $arguments = array())
35 {
36 $this->source = $source;
37 $this->buffer = new MWP_Stream_Buffer();
38 $this->arguments = $arguments;
39 }
40
41 public function close()
42 {
43 $this->tellPos = false;
44 $this->source = null;
45 $this->arguments = array();
46 }
47
48 public function tell()
49 {
50 return $this->tellPos;
51 }
52
53 public function eof()
54 {
55 if ($this->source !== null) {
56 return false;
57 }
58
59 if ($this->source === null && $this->buffer !== null) {
60 return $this->buffer->eof();
61 }
62
63 return true;
64 }
65
66 public function isSeekable()
67 {
68 return false;
69 }
70
71 public function seek($offset, $whence = SEEK_SET)
72 {
73 return false;
74 }
75
76 public function read($length)
77 {
78 $data = $this->buffer->read($length);
79 $readLen = strlen($data);
80 $this->tellPos += $readLen;
81 $remaining = $length - $readLen;
82
83 if ($remaining) {
84 $this->pump($remaining);
85 $data .= $this->buffer->read($remaining);
86 $this->tellPos += strlen($data) - $readLen;
87 }
88
89 return $data;
90 }
91
92 public function __toString()
93 {
94 $buffer = '';
95
96 while (!$this->eof()) {
97 $buffer .= $this->read(1048576);
98 }
99
100 return $buffer;
101 }
102
103 private function pump($length)
104 {
105 if ($this->source) {
106 do {
107 $data = call_user_func_array($this->source, array_merge(array($length), $this->arguments));
108 if ($data === false || $data === null) {
109 $this->source = null;
110
111 return;
112 }
113 if ($data instanceof MWP_Stream_Interface) {
114 $this->source = null;
115 $this->buffer = $data;
116
117 return;
118 }
119 $this->buffer->write($data);
120 $length -= strlen($data);
121 } while ($length > 0);
122 }
123 }
124 }
125