ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
PumpStream.php
Go to the documentation of this file.
1 <?php
2 namespace GuzzleHttp\Psr7;
3 
5 
16 class PumpStream implements StreamInterface
17 {
19  private $source;
20 
22  private $size;
23 
25  private $tellPos = 0;
26 
28  private $metadata;
29 
31  private $buffer;
32 
43  public function __construct(callable $source, array $options = [])
44  {
45  $this->source = $source;
46  $this->size = isset($options['size']) ? $options['size'] : null;
47  $this->metadata = isset($options['metadata']) ? $options['metadata'] : [];
48  $this->buffer = new BufferStream();
49  }
50 
51  public function __toString()
52  {
53  try {
54  return copy_to_string($this);
55  } catch (\Exception $e) {
56  return '';
57  }
58  }
59 
60  public function close()
61  {
62  $this->detach();
63  }
64 
65  public function detach()
66  {
67  $this->tellPos = false;
68  $this->source = null;
69  }
70 
71  public function getSize()
72  {
73  return $this->size;
74  }
75 
76  public function tell()
77  {
78  return $this->tellPos;
79  }
80 
81  public function eof()
82  {
83  return !$this->source;
84  }
85 
86  public function isSeekable()
87  {
88  return false;
89  }
90 
91  public function rewind()
92  {
93  $this->seek(0);
94  }
95 
96  public function seek($offset, $whence = SEEK_SET)
97  {
98  throw new \RuntimeException('Cannot seek a PumpStream');
99  }
100 
101  public function isWritable()
102  {
103  return false;
104  }
105 
106  public function write($string)
107  {
108  throw new \RuntimeException('Cannot write to a PumpStream');
109  }
110 
111  public function isReadable()
112  {
113  return true;
114  }
115 
116  public function read($length)
117  {
118  $data = $this->buffer->read($length);
119  $readLen = strlen($data);
120  $this->tellPos += $readLen;
121  $remaining = $length - $readLen;
122 
123  if ($remaining) {
124  $this->pump($remaining);
125  $data .= $this->buffer->read($remaining);
126  $this->tellPos += strlen($data) - $readLen;
127  }
128 
129  return $data;
130  }
131 
132  public function getContents()
133  {
134  $result = '';
135  while (!$this->eof()) {
136  $result .= $this->read(1000000);
137  }
138 
139  return $result;
140  }
141 
142  public function getMetadata($key = null)
143  {
144  if (!$key) {
145  return $this->metadata;
146  }
147 
148  return isset($this->metadata[$key]) ? $this->metadata[$key] : null;
149  }
150 
151  private function pump($length)
152  {
153  if ($this->source) {
154  do {
155  $data = call_user_func($this->source, $length);
156  if ($data === false || $data === null) {
157  $this->source = null;
158  return;
159  }
160  $this->buffer->write($data);
161  $length -= strlen($data);
162  } while ($length > 0);
163  }
164  }
165 }
__construct(callable $source, array $options=[])
Definition: PumpStream.php:43
detach()
Separates any underlying resources from the stream.
Definition: PumpStream.php:65
$result
tell()
Returns the current position of the file read/write pointer.
Definition: PumpStream.php:76
read($length)
Read data from the stream.
Definition: PumpStream.php:116
isWritable()
Returns whether or not the stream is writable.
Definition: PumpStream.php:101
Provides a buffer stream that can be written to to fill a buffer, and read from to remove bytes from ...
if($state['core:TerminatedAssocId'] !==null) $remaining
write($string)
Write data to the stream.
Definition: PumpStream.php:106
Provides a read only stream that pumps data from a PHP callable.
Definition: PumpStream.php:16
getSize()
Get the size of the stream if known.
Definition: PumpStream.php:71
seek($offset, $whence=SEEK_SET)
Seek to a position in the stream.
Definition: PumpStream.php:96
eof()
Returns true if the stream is at the end of the stream.
Definition: PumpStream.php:81
isReadable()
Returns whether or not the stream is readable.
Definition: PumpStream.php:111
close()
Closes the stream and any underlying resources.
Definition: PumpStream.php:60
rewind()
Seek to the beginning of the stream.
Definition: PumpStream.php:91
font size
Definition: langcheck.php:162
getMetadata($key=null)
Get stream metadata as an associative array or retrieve a specific key.
Definition: PumpStream.php:142
isSeekable()
Returns whether or not the stream is seekable.
Definition: PumpStream.php:86
getContents()
Returns the remaining contents in a string.
Definition: PumpStream.php:132
copy_to_string(StreamInterface $stream, $maxLen=-1)
Copy the contents of a stream into a string until the given number of bytes have been read...
Definition: functions.php:328
$key
Definition: croninfo.php:18
__toString()
Reads all data from the stream into a string, from the beginning to end.
Definition: PumpStream.php:51
Describes a data stream.
$data
Definition: bench.php:6