Open Journal Systems  3.3.0
PumpStream.php
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 }
GuzzleHttp\Psr7\PumpStream
Definition: PumpStream.php:16
GuzzleHttp\Psr7\BufferStream
Definition: BufferStream.php:14
GuzzleHttp\Psr7\PumpStream\write
write($string)
Definition: PumpStream.php:121
GuzzleHttp\Psr7\PumpStream\eof
eof()
Definition: PumpStream.php:96
GuzzleHttp\Psr7\PumpStream\close
close()
Definition: PumpStream.php:75
Psr\Http\Message\StreamInterface
Definition: vendor/psr/http-message/src/StreamInterface.php:12
GuzzleHttp\Psr7\PumpStream\__toString
__toString()
Definition: PumpStream.php:66
GuzzleHttp\Psr7\PumpStream\rewind
rewind()
Definition: PumpStream.php:106
GuzzleHttp\Psr7\PumpStream\getMetadata
getMetadata($key=null)
Definition: PumpStream.php:157
GuzzleHttp\Psr7\PumpStream\isSeekable
isSeekable()
Definition: PumpStream.php:101
GuzzleHttp\Psr7\PumpStream\isReadable
isReadable()
Definition: PumpStream.php:126
GuzzleHttp\Psr7\PumpStream\getContents
getContents()
Definition: PumpStream.php:147
GuzzleHttp\Psr7\PumpStream\tell
tell()
Definition: PumpStream.php:91
GuzzleHttp\Psr7\PumpStream\getSize
getSize()
Definition: PumpStream.php:86
GuzzleHttp\Psr7
Definition: AppendStream.php:2
GuzzleHttp\Psr7\PumpStream\seek
seek($offset, $whence=SEEK_SET)
Definition: PumpStream.php:111
GuzzleHttp\Psr7\PumpStream\detach
detach()
Definition: PumpStream.php:80
GuzzleHttp\Psr7\PumpStream\__construct
__construct(callable $source, array $options=[])
Definition: PumpStream.php:58
GuzzleHttp\Psr7\PumpStream\read
read($length)
Definition: PumpStream.php:131
GuzzleHttp\Psr7\copy_to_string
copy_to_string(StreamInterface $stream, $maxLen=-1)
Definition: guzzlehttp/psr7/src/functions.php:332
GuzzleHttp\Psr7\PumpStream\isWritable
isWritable()
Definition: PumpStream.php:116