-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stream.php
183 lines (160 loc) · 4.49 KB
/
Stream.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/*
* This file is part of the Koded package.
*
* (c) Mihail Binev <mihail@kodeart.com>
*
* Please view the LICENSE distributed with this source code
* for the full copyright and license information.
*
*/
namespace Koded\Http;
use Koded\Http\Interfaces\HttpStatus;
use Psr\Http\Message\StreamInterface;
class Stream implements StreamInterface
{
protected const MODES = [
'w+' => 1,
'r+' => 1,
'x+' => 1,
'c+' => 1,
'a+' => 1,
'w+b' => 1,
'r+b' => 1,
'x+b' => 1,
'c+b' => 1,
'w+t' => 1,
'r+t' => 1,
'x+t' => 1,
'c+t' => 1
];
/** @var resource The underlying stream resource */
protected $stream;
protected string $mode = 'w+b';
protected bool $seekable = false;
public function __construct($stream)
{
if (false === \is_resource($stream) || 'stream' !== \get_resource_type($stream)) {
throw new \RuntimeException(
'The provided resource is not a valid stream resource, ' . \gettype($stream) . ' given.',
HttpStatus::UNPROCESSABLE_ENTITY
);
}
$metadata = \stream_get_meta_data($stream);
$this->mode = $metadata['mode'] ?? 'w+b';
$this->seekable = $metadata['seekable'] ?? false;
$this->stream = $stream;
}
public function __destruct()
{
$this->close();
}
public function __toString(): string
{
try {
$this->seek(0);
return $this->getContents();
} catch (\Throwable) {
return '';
}
}
public function close(): void
{
if ($this->stream) {
\fclose($this->stream);
$this->detach();
}
}
public function detach()
{
if (empty($this->stream)) {
return null;
}
$resource = $this->stream;
$this->mode = 'w+b';
$this->stream = null;
$this->seekable = false;
return $resource;
}
public function getSize(): ?int
{
if (empty($this->stream)) {
return null;
}
return \fstat($this->stream)['size'] ?? null;
}
public function tell(): int
{
if (false === $position = \ftell($this->stream)) {
throw new \RuntimeException('Failed to find the position of the file pointer');
}
return $position;
}
public function eof(): bool
{
return \feof($this->stream);
}
public function seek($offset, $whence = SEEK_SET): void
{
if (0 !== @\fseek($this->stream, $offset, $whence)) {
throw new \RuntimeException('Failed to seek to file pointer');
}
}
public function rewind(): void
{
if (false === $this->seekable) {
throw new \RuntimeException('The stream is not seekable');
}
$this->seek(0);
}
public function write($string): int
{
if (false === $this->isWritable()) {
throw new \RuntimeException('The stream is not writable');
}
if (false === $bytes = \fwrite($this->stream, $string)) {
throw new \RuntimeException('Failed to write data to the stream');
}
return $bytes;
}
public function read($length): string
{
if (false === $this->isReadable()) {
throw new \RuntimeException('The stream is not readable');
}
if (empty($length)) {
return '';
}
if (false === $data = \fread($this->stream, $length)) {
throw new \RuntimeException('Failed to read the data from stream');
}
return $data;
}
public function getContents(): string
{
if (false === $content = \stream_get_contents($this->stream)) {
throw new \RuntimeException('Unable to read the stream content');
}
return $content;
}
public function getMetadata($key = null)
{
$metadata = \stream_get_meta_data($this->stream);
if (null === $key) {
return $metadata;
}
return $metadata[$key] ?? null;
}
public function isSeekable(): bool
{
return $this->seekable;
}
public function isReadable(): bool
{
return isset((self::MODES + ['r' => 1, 'rb' => 1, 'rt' => 1])[$this->mode]);
}
public function isWritable(): bool
{
return isset((self::MODES + ['w' => 1, 'rw' => 1, 'a' => 1, 'wb' => 1])[$this->mode]);
}
}