1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Debug\Helper\StreamWrapper; |
6
|
|
|
|
7
|
|
|
use Throwable; |
8
|
|
|
|
9
|
|
|
use function trigger_error; |
10
|
|
|
|
11
|
|
|
use const E_USER_ERROR; |
12
|
|
|
use const STREAM_MKDIR_RECURSIVE; |
13
|
|
|
use const STREAM_URL_STAT_QUIET; |
14
|
|
|
use const STREAM_USE_PATH; |
15
|
|
|
|
16
|
|
|
final class StreamWrapper implements StreamWrapperInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var resource|null |
20
|
|
|
*/ |
21
|
|
|
public mixed $context = null; |
22
|
|
|
|
23
|
|
|
public ?string $filename = null; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var resource|null |
27
|
|
|
*/ |
28
|
|
|
public $stream = null; |
29
|
|
|
|
30
|
|
|
public function dir_closedir(): bool |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
return true; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function dir_opendir(string $path, int $options): bool |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
return true; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function dir_readdir(): string |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
return readdir($this->stream); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function dir_rewinddir(): bool |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
return rewinddir($this->stream); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function mkdir(string $path, int $mode, int $options): bool |
51
|
|
|
{ |
52
|
|
|
return mkdir($path, $mode, ($options & STREAM_MKDIR_RECURSIVE) === STREAM_MKDIR_RECURSIVE); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function rename(string $path_from, string $path_to): bool |
56
|
|
|
{ |
57
|
|
|
return rename($path_from, $path_to); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function rmdir(string $path, int $options): bool |
61
|
|
|
{ |
62
|
|
|
return rmdir($path); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function stream_cast(int $cast_as) |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
//???? |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function stream_eof(): bool |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
return feof($this->stream); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function stream_open(string $path, string $mode, int $options, ?string &$opened_path): bool |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
$this->filename = realpath($path) ?: $path; |
78
|
|
|
$this->stream = fopen($path, $mode, ($options & STREAM_USE_PATH) === STREAM_USE_PATH); |
79
|
|
|
|
80
|
|
|
return $this->stream !== false; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function stream_read(int $count): string|false |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
return is_readable($this->filename) ? fread($this->stream, $count) : false; |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function stream_seek(int $offset, int $whence = SEEK_SET): bool |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
return fseek($this->stream, $offset, $whence) === 0; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function stream_set_option(int $option, int $arg1, ?int $arg2): bool |
|
|
|
|
94
|
|
|
{ |
95
|
|
|
return match ($option) { |
96
|
|
|
STREAM_OPTION_BLOCKING => stream_set_blocking($this->stream, $arg1 === STREAM_OPTION_BLOCKING), |
97
|
|
|
STREAM_OPTION_READ_TIMEOUT => stream_set_timeout($this->stream, $arg1, $arg2), |
|
|
|
|
98
|
|
|
STREAM_OPTION_WRITE_BUFFER => stream_set_write_buffer($this->stream, $arg2) === 0, |
|
|
|
|
99
|
|
|
default => false, |
100
|
|
|
}; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function stream_stat(): array |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
return fstat($this->stream); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function stream_tell(): int |
|
|
|
|
109
|
|
|
{ |
110
|
|
|
return ftell($this->stream); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function stream_write(string $data): int |
|
|
|
|
114
|
|
|
{ |
115
|
|
|
return fwrite($this->stream, $data); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function url_stat(string $path, int $flags): array|false |
|
|
|
|
119
|
|
|
{ |
120
|
|
|
try { |
121
|
|
|
return stat($path); |
122
|
|
|
} catch (Throwable $e) { |
123
|
|
|
if (($flags & STREAM_URL_STAT_QUIET) === STREAM_URL_STAT_QUIET) { |
124
|
|
|
return false; |
125
|
|
|
} |
126
|
|
|
trigger_error($e->getMessage(), E_USER_ERROR); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return false; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function stream_metadata(string $path, int $option, mixed $value): bool |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
return match ($option) { |
135
|
|
|
STREAM_META_TOUCH => touch($path, ...$value), |
|
|
|
|
136
|
|
|
STREAM_META_OWNER_NAME, STREAM_META_OWNER => chown($path, $value), |
137
|
|
|
STREAM_META_GROUP_NAME, STREAM_META_GROUP => chgrp($path, $value), |
138
|
|
|
STREAM_META_ACCESS => chmod($path, $value), |
139
|
|
|
default => false |
140
|
|
|
}; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function stream_flush(): bool |
|
|
|
|
144
|
|
|
{ |
145
|
|
|
return fflush($this->stream); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function stream_close(): void |
|
|
|
|
149
|
|
|
{ |
150
|
|
|
fclose($this->stream); |
151
|
|
|
$this->stream = null; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function stream_lock(int $operation): bool |
|
|
|
|
155
|
|
|
{ |
156
|
|
|
return flock($this->stream, $operation); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function stream_truncate(int $new_size): bool |
|
|
|
|
160
|
|
|
{ |
161
|
|
|
return ftruncate($this->stream, $new_size); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function unlink(string $path): bool |
165
|
|
|
{ |
166
|
|
|
return unlink($path, $this->context); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|