wishthis/vendor/guzzlehttp/psr7/src/Stream.php

271 lines
6.7 KiB
PHP
Raw Normal View History

2022-01-21 08:23:52 +00:00
<?php
namespace GuzzleHttp\Psr7;
use Psr\Http\Message\StreamInterface;
/**
* PHP stream implementation.
2022-01-21 13:12:04 +00:00
*
* @var $stream
2022-01-21 08:23:52 +00:00
*/
class Stream implements StreamInterface
{
/**
2022-01-21 13:12:04 +00:00
* Resource modes.
*
* @var string
*
2022-01-21 08:23:52 +00:00
* @see http://php.net/manual/function.fopen.php
* @see http://php.net/manual/en/function.gzopen.php
*/
2022-01-21 13:12:04 +00:00
const READABLE_MODES = '/r|a\+|ab\+|w\+|wb\+|x\+|xb\+|c\+|cb\+/';
const WRITABLE_MODES = '/a|w|r\+|rb\+|rw|x|c/';
2022-01-21 08:23:52 +00:00
private $stream;
private $size;
private $seekable;
private $readable;
private $writable;
private $uri;
private $customMetadata;
/**
* This constructor accepts an associative array of options.
*
* - size: (int) If a read stream would otherwise have an indeterminate
* size, but the size is known due to foreknowledge, then you can
* provide that size, in bytes.
* - metadata: (array) Any additional metadata to return when the metadata
* of the stream is accessed.
*
2022-01-21 13:12:04 +00:00
* @param resource $stream Stream resource to wrap.
* @param array $options Associative array of options.
2022-01-21 08:23:52 +00:00
*
* @throws \InvalidArgumentException if the stream is not a stream resource
*/
2022-01-21 13:12:04 +00:00
public function __construct($stream, $options = [])
2022-01-21 08:23:52 +00:00
{
if (!is_resource($stream)) {
throw new \InvalidArgumentException('Stream must be a resource');
}
if (isset($options['size'])) {
$this->size = $options['size'];
}
2022-01-21 13:12:04 +00:00
$this->customMetadata = isset($options['metadata'])
? $options['metadata']
: [];
2022-01-21 08:23:52 +00:00
$this->stream = $stream;
$meta = stream_get_meta_data($this->stream);
$this->seekable = $meta['seekable'];
$this->readable = (bool)preg_match(self::READABLE_MODES, $meta['mode']);
$this->writable = (bool)preg_match(self::WRITABLE_MODES, $meta['mode']);
$this->uri = $this->getMetadata('uri');
}
/**
* Closes the stream when the destructed
*/
public function __destruct()
{
$this->close();
}
2022-01-21 13:12:04 +00:00
public function __toString()
2022-01-21 08:23:52 +00:00
{
try {
if ($this->isSeekable()) {
$this->seek(0);
}
return $this->getContents();
2022-01-21 13:12:04 +00:00
} catch (\Exception $e) {
2022-01-21 08:23:52 +00:00
return '';
}
}
2022-01-21 13:12:04 +00:00
public function getContents()
2022-01-21 08:23:52 +00:00
{
if (!isset($this->stream)) {
throw new \RuntimeException('Stream is detached');
}
$contents = stream_get_contents($this->stream);
if ($contents === false) {
throw new \RuntimeException('Unable to read stream contents');
}
return $contents;
}
2022-01-21 13:12:04 +00:00
public function close()
2022-01-21 08:23:52 +00:00
{
if (isset($this->stream)) {
if (is_resource($this->stream)) {
fclose($this->stream);
}
$this->detach();
}
}
public function detach()
{
if (!isset($this->stream)) {
return null;
}
$result = $this->stream;
unset($this->stream);
$this->size = $this->uri = null;
$this->readable = $this->writable = $this->seekable = false;
return $result;
}
2022-01-21 13:12:04 +00:00
public function getSize()
2022-01-21 08:23:52 +00:00
{
if ($this->size !== null) {
return $this->size;
}
if (!isset($this->stream)) {
return null;
}
// Clear the stat cache if the stream has a URI
if ($this->uri) {
clearstatcache(true, $this->uri);
}
$stats = fstat($this->stream);
2022-01-21 13:12:04 +00:00
if (isset($stats['size'])) {
2022-01-21 08:23:52 +00:00
$this->size = $stats['size'];
return $this->size;
}
return null;
}
2022-01-21 13:12:04 +00:00
public function isReadable()
2022-01-21 08:23:52 +00:00
{
return $this->readable;
}
2022-01-21 13:12:04 +00:00
public function isWritable()
2022-01-21 08:23:52 +00:00
{
return $this->writable;
}
2022-01-21 13:12:04 +00:00
public function isSeekable()
2022-01-21 08:23:52 +00:00
{
return $this->seekable;
}
2022-01-21 13:12:04 +00:00
public function eof()
2022-01-21 08:23:52 +00:00
{
if (!isset($this->stream)) {
throw new \RuntimeException('Stream is detached');
}
return feof($this->stream);
}
2022-01-21 13:12:04 +00:00
public function tell()
2022-01-21 08:23:52 +00:00
{
if (!isset($this->stream)) {
throw new \RuntimeException('Stream is detached');
}
$result = ftell($this->stream);
if ($result === false) {
throw new \RuntimeException('Unable to determine stream position');
}
return $result;
}
2022-01-21 13:12:04 +00:00
public function rewind()
2022-01-21 08:23:52 +00:00
{
$this->seek(0);
}
2022-01-21 13:12:04 +00:00
public function seek($offset, $whence = SEEK_SET)
2022-01-21 08:23:52 +00:00
{
$whence = (int) $whence;
if (!isset($this->stream)) {
throw new \RuntimeException('Stream is detached');
}
if (!$this->seekable) {
throw new \RuntimeException('Stream is not seekable');
}
if (fseek($this->stream, $offset, $whence) === -1) {
throw new \RuntimeException('Unable to seek to stream position '
. $offset . ' with whence ' . var_export($whence, true));
}
}
2022-01-21 13:12:04 +00:00
public function read($length)
2022-01-21 08:23:52 +00:00
{
if (!isset($this->stream)) {
throw new \RuntimeException('Stream is detached');
}
if (!$this->readable) {
throw new \RuntimeException('Cannot read from non-readable stream');
}
if ($length < 0) {
throw new \RuntimeException('Length parameter cannot be negative');
}
if (0 === $length) {
return '';
}
$string = fread($this->stream, $length);
if (false === $string) {
throw new \RuntimeException('Unable to read from stream');
}
return $string;
}
2022-01-21 13:12:04 +00:00
public function write($string)
2022-01-21 08:23:52 +00:00
{
if (!isset($this->stream)) {
throw new \RuntimeException('Stream is detached');
}
if (!$this->writable) {
throw new \RuntimeException('Cannot write to a non-writable stream');
}
// We can't know the size after writing anything
$this->size = null;
$result = fwrite($this->stream, $string);
if ($result === false) {
throw new \RuntimeException('Unable to write to stream');
}
return $result;
}
public function getMetadata($key = null)
{
if (!isset($this->stream)) {
return $key ? null : [];
} elseif (!$key) {
return $this->customMetadata + stream_get_meta_data($this->stream);
} elseif (isset($this->customMetadata[$key])) {
return $this->customMetadata[$key];
}
$meta = stream_get_meta_data($this->stream);
2022-01-21 13:12:04 +00:00
return isset($meta[$key]) ? $meta[$key] : null;
2022-01-21 08:23:52 +00:00
}
}