-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStream.php
More file actions
181 lines (138 loc) · 3.94 KB
/
Stream.php
File metadata and controls
181 lines (138 loc) · 3.94 KB
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
<?php
declare(strict_types=1);
namespace TinyBlocks\Http\Internal\Response\Stream;
use Psr\Http\Message\StreamInterface;
use TinyBlocks\Http\Internal\Exceptions\InvalidResource;
use TinyBlocks\Http\Internal\Exceptions\MissingResourceStream;
use TinyBlocks\Http\Internal\Exceptions\NonReadableStream;
use TinyBlocks\Http\Internal\Exceptions\NonSeekableStream;
use TinyBlocks\Http\Internal\Exceptions\NonWritableStream;
final class Stream implements StreamInterface
{
private const int OFFSET_ZERO = 0;
private string $content = '';
private bool $contentFetched = false;
/**
* @param resource|null $resource
* @param StreamMetaData $metaData
*/
private function __construct(private mixed $resource, private readonly StreamMetaData $metaData)
{
}
public static function from(mixed $resource): Stream
{
if (!is_resource($resource)) {
throw new InvalidResource();
}
$metaData = StreamMetaData::from(data: stream_get_meta_data($resource));
return new Stream(resource: $resource, metaData: $metaData);
}
public function close(): void
{
if ($this->noResource()) {
return;
}
/** @var resource $resource */
$resource = $this->detach();
fclose($resource);
}
public function detach()
{
$resource = $this->resource;
$this->resource = null;
return $resource;
}
public function getSize(): ?int
{
if ($this->noResource()) {
return null;
}
$size = fstat($this->resource);
return is_array($size) ? $size['size'] : null;
}
public function tell(): int
{
if ($this->noResource()) {
throw new MissingResourceStream();
}
return ftell($this->resource);
}
public function eof(): bool
{
return $this->resource && feof($this->resource);
}
public function seek(int $offset, int $whence = SEEK_SET): void
{
if (!$this->isSeekable()) {
throw new NonSeekableStream();
}
fseek($this->resource, $offset, $whence);
}
public function rewind(): void
{
$this->seek(offset: self::OFFSET_ZERO);
}
public function read(int $length): string
{
if (!$this->isReadable()) {
throw new NonReadableStream();
}
return fread($this->resource, $length);
}
public function write(string $string): int
{
if (!$this->isWritable()) {
throw new NonWritableStream();
}
return fwrite($this->resource, $string);
}
public function isReadable(): bool
{
if ($this->noResource()) {
return false;
}
$mode = $this->metaData->getMode();
return $mode === 'r' || strstr($mode, '+');
}
public function isWritable(): bool
{
if ($this->noResource()) {
return false;
}
return strpbrk($this->metaData->getMode(), 'xwca+') !== false;
}
public function isSeekable(): bool
{
return !$this->noResource() && $this->metaData->isSeekable();
}
public function getContents(): string
{
if (!$this->isReadable()) {
throw new NonReadableStream();
}
if (!$this->contentFetched) {
$this->content = stream_get_contents($this->resource);
$this->contentFetched = true;
}
return $this->content;
}
public function getMetadata(?string $key = null): mixed
{
$metaData = $this->metaData->toArray();
if (is_null($key)) {
return $metaData;
}
return $metaData[$key] ?? null;
}
public function __toString(): string
{
if ($this->isSeekable()) {
$this->rewind();
}
return $this->getContents();
}
private function noResource(): bool
{
return empty($this->resource);
}
}