38 $this->stream = $stream;
39 $this->size = $stream->getSize();
42 $this->resource = fopen(
'php://temp/maxmemory:'.$memoryBuffer,
'rw+');
44 $this->resource = fopen(
'php://memory',
'rw+');
47 if (
false === $this->resource) {
48 throw new \RuntimeException(
'Cannot create a resource over temp or memory implementation');
61 }
catch (\Throwable $throwable) {
63 }
catch (\Exception $exception) {
71 public function close()
73 if (
null === $this->resource) {
74 throw new \RuntimeException(
'Cannot close on a detached stream');
77 $this->stream->close();
78 fclose($this->resource);
86 if (
null === $this->resource) {
93 $resource = $this->resource;
94 $this->stream->close();
96 $this->resource =
null;
106 if (
null === $this->resource) {
110 if (
null === $this->size && $this->stream->eof()) {
111 return $this->written;
120 public function tell()
122 if (
null === $this->resource) {
123 throw new \RuntimeException(
'Cannot tell on a detached stream');
126 return ftell($this->resource);
134 if (
null === $this->resource) {
135 throw new \RuntimeException(
'Cannot call eof on a detached stream');
139 return $this->stream->eof() && (ftell($this->resource) === $this->written);
147 return null !== $this->resource;
153 public function seek($offset, $whence = SEEK_SET)
155 if (
null === $this->resource) {
156 throw new \RuntimeException(
'Cannot seek on a detached stream');
159 fseek($this->resource, $offset, $whence);
167 if (
null === $this->resource) {
168 throw new \RuntimeException(
'Cannot rewind on a detached stream');
185 public function write($string)
187 throw new \RuntimeException(
'Cannot write on this stream');
195 return null !== $this->resource;
201 public function read($length)
203 if (
null === $this->resource) {
204 throw new \RuntimeException(
'Cannot read on a detached stream');
210 if (ftell($this->resource) !== $this->written) {
211 $read = fread($this->resource, $length);
214 $bytesRead = strlen($read);
216 if ($bytesRead < $length) {
217 $streamRead = $this->stream->read($length - $bytesRead);
220 $this->written += fwrite($this->resource, $streamRead);
221 $read .= $streamRead;
232 if (
null === $this->resource) {
233 throw new \RuntimeException(
'Cannot read on a detached stream');
238 while (!$this->
eof()) {
239 $read .= $this->
read(8192);
250 if (
null === $this->resource) {
258 $metadata = stream_get_meta_data($this->resource);
264 if (!array_key_exists($key, $metadata)) {
268 return $metadata[$key];