1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mpyw\StreamInterfaceResource\StreamWrapper; |
4
|
|
|
|
5
|
|
|
use Mpyw\StreamInterfaceResource\Registry\StreamRegistrar; |
6
|
|
|
use Mpyw\StreamInterfaceResource\Registry\StreamRegistrarInterface; |
7
|
|
|
use Mpyw\StreamInterfaceResource\Unserializable; |
8
|
|
|
use Psr\Http\Message\StreamInterface; |
9
|
|
|
|
10
|
|
|
class StreamWrapper implements StreamWrapperInterface |
11
|
|
|
{ |
12
|
|
|
use Unserializable; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var StreamInterface |
16
|
|
|
*/ |
17
|
|
|
protected $stream; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var null|StreamRegistrar |
21
|
|
|
*/ |
22
|
|
|
public static $defaultRegistrar = null; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var StreamRegistrarInterface |
26
|
|
|
*/ |
27
|
|
|
protected $registrar; |
28
|
|
|
|
29
|
15 |
|
public function __construct(?StreamRegistrarInterface $registrar = null) |
30
|
|
|
{ |
31
|
15 |
|
$this->registrar = $registrar ?? static::$defaultRegistrar ?? StreamRegistrar::getInstance(); |
32
|
|
|
} |
33
|
|
|
|
34
|
15 |
|
public function __destruct() |
35
|
|
|
{ |
36
|
15 |
|
$this->registrar->remove($this->stream); |
37
|
|
|
} |
38
|
|
|
|
39
|
15 |
|
public function stream_close(): void |
40
|
|
|
{ |
41
|
15 |
|
$this->stream->close(); |
42
|
|
|
} |
43
|
|
|
|
44
|
8 |
|
public function stream_eof(): bool |
45
|
|
|
{ |
46
|
8 |
|
return $this->stream->eof(); |
47
|
|
|
} |
48
|
|
|
|
49
|
15 |
|
public function stream_open(string $path, string $mode, int $options, &$opened_path): bool |
50
|
|
|
{ |
51
|
15 |
|
$this->stream = $this->registrar->streamFor($path); |
52
|
15 |
|
return true; |
53
|
|
|
} |
54
|
|
|
|
55
|
8 |
|
public function stream_read(int $count): string |
56
|
|
|
{ |
57
|
8 |
|
return $this->stream->read($count); |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
public function stream_seek(int $offset, int $whence = SEEK_SET): bool |
61
|
|
|
{ |
62
|
1 |
|
$this->stream->seek($offset, $whence); |
63
|
1 |
|
return true; |
64
|
|
|
} |
65
|
|
|
|
66
|
5 |
|
public function stream_stat(): array |
67
|
|
|
{ |
68
|
|
|
return [ |
69
|
5 |
|
'size' => $this->stream->getSize(), |
70
|
|
|
]; |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
public function stream_tell(): int |
74
|
|
|
{ |
75
|
1 |
|
return $this->stream->tell(); |
76
|
|
|
} |
77
|
|
|
|
78
|
2 |
|
public function stream_write(string $data): int |
79
|
|
|
{ |
80
|
2 |
|
return $this->stream->write($data); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|