Open Journal Systems  3.3.0
CachingEntityBody.php
1 <?php
2 
3 namespace Guzzle\Http;
4 
6 
11 {
13  protected $remoteStream;
14 
16  protected $skipReadBytes = 0;
17 
23  {
24  $this->remoteStream = $body;
25  $this->body = new EntityBody(fopen('php://temp', 'r+'));
26  }
27 
35  public function __toString()
36  {
37  $pos = $this->ftell();
38  $this->rewind();
39 
40  $str = '';
41  while (!$this->isConsumed()) {
42  $str .= $this->read(16384);
43  }
44 
45  $this->seek($pos);
46 
47  return $str;
48  }
49 
50  public function getSize()
51  {
52  return max($this->body->getSize(), $this->remoteStream->getSize());
53  }
54 
59  public function seek($offset, $whence = SEEK_SET)
60  {
61  if ($whence == SEEK_SET) {
62  $byte = $offset;
63  } elseif ($whence == SEEK_CUR) {
64  $byte = $offset + $this->ftell();
65  } else {
66  throw new RuntimeException(__CLASS__ . ' supports only SEEK_SET and SEEK_CUR seek operations');
67  }
68 
69  // You cannot skip ahead past where you've read from the remote stream
70  if ($byte > $this->body->getSize()) {
71  throw new RuntimeException(
72  "Cannot seek to byte {$byte} when the buffered stream only contains {$this->body->getSize()} bytes"
73  );
74  }
75 
76  return $this->body->seek($byte);
77  }
78 
79  public function rewind()
80  {
81  return $this->seek(0);
82  }
83 
89  public function setRewindFunction($callable)
90  {
91  throw new RuntimeException(__CLASS__ . ' does not support custom stream rewind functions');
92  }
93 
94  public function read($length)
95  {
96  // Perform a regular read on any previously read data from the buffer
97  $data = $this->body->read($length);
98  $remaining = $length - strlen($data);
99 
100  // More data was requested so read from the remote stream
101  if ($remaining) {
102  // If data was written to the buffer in a position that would have been filled from the remote stream,
103  // then we must skip bytes on the remote stream to emulate overwriting bytes from that position. This
104  // mimics the behavior of other PHP stream wrappers.
105  $remoteData = $this->remoteStream->read($remaining + $this->skipReadBytes);
106 
107  if ($this->skipReadBytes) {
108  $len = strlen($remoteData);
109  $remoteData = substr($remoteData, $this->skipReadBytes);
110  $this->skipReadBytes = max(0, $this->skipReadBytes - $len);
111  }
112 
113  $data .= $remoteData;
114  $this->body->write($remoteData);
115  }
116 
117  return $data;
118  }
119 
120  public function write($string)
121  {
122  // When appending to the end of the currently read stream, you'll want to skip bytes from being read from
123  // the remote stream to emulate other stream wrappers. Basically replacing bytes of data of a fixed length.
124  $overflow = (strlen($string) + $this->ftell()) - $this->remoteStream->ftell();
125  if ($overflow > 0) {
126  $this->skipReadBytes += $overflow;
127  }
128 
129  return $this->body->write($string);
130  }
131 
136  public function readLine($maxLength = null)
137  {
138  $buffer = '';
139  $size = 0;
140  while (!$this->isConsumed()) {
141  $byte = $this->read(1);
142  $buffer .= $byte;
143  // Break when a new line is found or the max length - 1 is reached
144  if ($byte == PHP_EOL || ++$size == $maxLength - 1) {
145  break;
146  }
147  }
148 
149  return $buffer;
150  }
151 
152  public function isConsumed()
153  {
154  return $this->body->isConsumed() && $this->remoteStream->isConsumed();
155  }
156 
160  public function close()
161  {
162  return $this->remoteStream->close() && $this->body->close();
163  }
164 
165  public function setStream($stream, $size = 0)
166  {
167  $this->remoteStream->setStream($stream, $size);
168  }
169 
170  public function getContentType()
171  {
172  return $this->remoteStream->getContentType();
173  }
174 
175  public function getContentEncoding()
176  {
177  return $this->remoteStream->getContentEncoding();
178  }
179 
180  public function getMetaData($key = null)
181  {
182  return $this->remoteStream->getMetaData($key);
183  }
184 
185  public function getStream()
186  {
187  return $this->remoteStream->getStream();
188  }
189 
190  public function getWrapper()
191  {
192  return $this->remoteStream->getWrapper();
193  }
194 
195  public function getWrapperData()
196  {
197  return $this->remoteStream->getWrapperData();
198  }
199 
200  public function getStreamType()
201  {
202  return $this->remoteStream->getStreamType();
203  }
204 
205  public function getUri()
206  {
207  return $this->remoteStream->getUri();
208  }
209 
214  public function getCustomData($key)
215  {
216  return $this->remoteStream->getCustomData($key);
217  }
218 
223  public function setCustomData($key, $value)
224  {
225  $this->remoteStream->setCustomData($key, $value);
226 
227  return $this;
228  }
229 }
Guzzle\Http\CachingEntityBody\getWrapper
getWrapper()
Definition: CachingEntityBody.php:196
Guzzle\Http\AbstractEntityBodyDecorator\ftell
ftell()
Definition: AbstractEntityBodyDecorator.php:208
Guzzle\Http\CachingEntityBody\getSize
getSize()
Definition: CachingEntityBody.php:56
Guzzle\Http\CachingEntityBody\__construct
__construct(EntityBodyInterface $body)
Definition: CachingEntityBody.php:28
Guzzle\Http\AbstractEntityBodyDecorator\$body
$body
Definition: AbstractEntityBodyDecorator.php:16
Guzzle\Http\EntityBodyInterface
Definition: EntityBodyInterface.php:10
Guzzle\Http\CachingEntityBody\getMetaData
getMetaData($key=null)
Definition: CachingEntityBody.php:186
Guzzle\Http\CachingEntityBody\getStream
getStream()
Definition: CachingEntityBody.php:191
Guzzle\Http\CachingEntityBody\read
read($length)
Definition: CachingEntityBody.php:100
Guzzle\Http\CachingEntityBody\setStream
setStream($stream, $size=0)
Definition: CachingEntityBody.php:171
Guzzle\Http\CachingEntityBody
Definition: CachingEntityBody.php:10
Guzzle\Http\AbstractEntityBodyDecorator
Definition: AbstractEntityBodyDecorator.php:10
Guzzle\Http\CachingEntityBody\$remoteStream
$remoteStream
Definition: CachingEntityBody.php:16
Guzzle\Http\CachingEntityBody\getCustomData
getCustomData($key)
Definition: CachingEntityBody.php:220
Guzzle\Http\EntityBody
Definition: EntityBody.php:13
Guzzle\Http\CachingEntityBody\readLine
readLine($maxLength=null)
Definition: CachingEntityBody.php:142
Guzzle\Http\CachingEntityBody\getContentEncoding
getContentEncoding()
Definition: CachingEntityBody.php:181
Guzzle\Http\CachingEntityBody\getContentType
getContentType()
Definition: CachingEntityBody.php:176
Guzzle\Http\CachingEntityBody\write
write($string)
Definition: CachingEntityBody.php:126
Guzzle\Http\CachingEntityBody\getUri
getUri()
Definition: CachingEntityBody.php:211
Guzzle\Http\CachingEntityBody\setRewindFunction
setRewindFunction($callable)
Definition: CachingEntityBody.php:95
Guzzle\Http
Definition: AbstractEntityBodyDecorator.php:3
Guzzle\Http\CachingEntityBody\close
close()
Definition: CachingEntityBody.php:166
Guzzle\Http\CachingEntityBody\seek
seek($offset, $whence=SEEK_SET)
Definition: CachingEntityBody.php:65
Guzzle\Http\CachingEntityBody\getWrapperData
getWrapperData()
Definition: CachingEntityBody.php:201
Guzzle\Http\CachingEntityBody\setCustomData
setCustomData($key, $value)
Definition: CachingEntityBody.php:229
Guzzle\Http\CachingEntityBody\getStreamType
getStreamType()
Definition: CachingEntityBody.php:206
Guzzle\Http\CachingEntityBody\__toString
__toString()
Definition: CachingEntityBody.php:41
Guzzle\Http\CachingEntityBody\rewind
rewind()
Definition: CachingEntityBody.php:85
Guzzle\Common\Exception\RuntimeException
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Common/Exception/RuntimeException.php:5
Guzzle\Http\CachingEntityBody\isConsumed
isConsumed()
Definition: CachingEntityBody.php:158
Guzzle\Http\CachingEntityBody\$skipReadBytes
$skipReadBytes
Definition: CachingEntityBody.php:22