|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Pcelta\VirtualStream; |
|
4
|
|
|
|
|
5
|
|
|
use DateTime; |
|
6
|
|
|
|
|
7
|
|
|
class Stream implements StreamWrapperInterface |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var string |
|
11
|
|
|
*/ |
|
12
|
|
|
private $content; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var Mode |
|
16
|
|
|
*/ |
|
17
|
|
|
private $mode; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
private $path; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var int |
|
26
|
|
|
*/ |
|
27
|
|
|
private $pointer; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
private $contentLeft; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var DateTime |
|
36
|
|
|
*/ |
|
37
|
|
|
private $lastAccess; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var DateTime |
|
41
|
|
|
*/ |
|
42
|
|
|
private $lastModification; |
|
43
|
|
|
|
|
44
|
|
|
const HOST = 'host'; |
|
45
|
|
|
const POINTER_INITIAL_POSITION = 0; |
|
46
|
|
|
const DEFAULT_PROTOCOL = 'virtual'; |
|
47
|
|
|
|
|
48
|
9 |
|
public function __construct() |
|
49
|
|
|
{ |
|
50
|
9 |
|
$this->pointer = self::POINTER_INITIAL_POSITION; |
|
51
|
9 |
|
} |
|
52
|
|
|
|
|
53
|
9 |
|
public function stream_open(string $path, string $mode, int $options, &$opened_path) |
|
54
|
|
|
{ |
|
55
|
9 |
|
$this->setLastAccess(); |
|
56
|
|
|
|
|
57
|
9 |
|
$url = parse_url($path); |
|
58
|
9 |
|
$this->mode = new Mode($mode); |
|
59
|
9 |
|
$this->path = $url[self::HOST]; |
|
60
|
|
|
|
|
61
|
9 |
|
return true; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
5 |
|
public function stream_read($count): string |
|
65
|
|
|
{ |
|
66
|
5 |
|
$this->setLastAccess(); |
|
67
|
|
|
|
|
68
|
5 |
|
if (!$this->mode->isReading()) { |
|
69
|
1 |
|
throw new OperationNotPermittedException( |
|
70
|
1 |
|
$this->mode->getMode(), |
|
71
|
1 |
|
OperationNotPermittedException::OPERATION_READING |
|
72
|
|
|
); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
4 |
|
$data = substr($this->content, $this->pointer++, $count); |
|
76
|
4 |
|
$this->contentLeft = str_replace($data, '', $this->contentLeft); |
|
77
|
|
|
|
|
78
|
4 |
|
return $data; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
6 |
|
public function stream_eof(): bool |
|
82
|
|
|
{ |
|
83
|
6 |
|
return $this->pointer > strlen($this->content); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
5 |
|
public function stream_seek(int $position, int $whence = SEEK_SET) |
|
87
|
|
|
{ |
|
88
|
5 |
|
$this->pointer = $position; |
|
89
|
5 |
|
} |
|
90
|
|
|
|
|
91
|
7 |
|
public function stream_write(string $data) |
|
92
|
|
|
{ |
|
93
|
7 |
|
if (!$this->mode->isWriting()) { |
|
94
|
1 |
|
throw new OperationNotPermittedException( |
|
95
|
1 |
|
$this->mode->getMode(), |
|
96
|
1 |
|
OperationNotPermittedException::OPERATION_WRITING |
|
97
|
|
|
); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
6 |
|
$this->setLastModification(); |
|
101
|
|
|
|
|
102
|
6 |
|
if ($this->mode->isAppending()) { |
|
103
|
2 |
|
$this->content .= $data; |
|
104
|
|
|
|
|
105
|
2 |
|
return; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
4 |
|
$this->content = $data . $this->content; |
|
109
|
4 |
|
} |
|
110
|
|
|
|
|
111
|
1 |
|
public function stream_truncate(int $newSize): bool |
|
112
|
|
|
{ |
|
113
|
1 |
|
$this->setLastModification(); |
|
114
|
|
|
|
|
115
|
1 |
|
$this->content = substr($this->content, self::POINTER_INITIAL_POSITION, $newSize); |
|
116
|
|
|
|
|
117
|
1 |
|
return true; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function stream_tell() |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->pointer; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function stream_stat(): array |
|
126
|
|
|
{ |
|
127
|
|
|
return [ |
|
128
|
|
|
0 => 1, |
|
129
|
|
|
1 => '', |
|
130
|
|
|
2 => '', |
|
131
|
|
|
3 => 0, |
|
132
|
|
|
4 => '', |
|
133
|
|
|
5 => '', |
|
134
|
|
|
6 => '', |
|
135
|
|
|
7 => strlen($this->content), |
|
136
|
|
|
8 => '', |
|
137
|
|
|
9 => $this->lastAccess->getTimestamp(), |
|
138
|
|
|
10 => $this->lastModification->getTimestamp(), |
|
139
|
|
|
11 => '', |
|
140
|
|
|
12 => '', |
|
141
|
|
|
]; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
|
|
145
|
|
|
public function stream_flush() |
|
146
|
|
|
{ |
|
147
|
|
|
// As this StreamWrapper does not write into a file or anywhere else, this method will not do anything. |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
12 |
|
public static function register(string $protocol = self::DEFAULT_PROTOCOL): bool |
|
151
|
|
|
{ |
|
152
|
12 |
|
return stream_wrapper_register($protocol, self::class); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
8 |
|
public static function unregister(string $protocol = self::DEFAULT_PROTOCOL): bool |
|
156
|
|
|
{ |
|
157
|
8 |
|
return stream_wrapper_unregister($protocol); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
9 |
|
protected function setLastAccess() |
|
161
|
|
|
{ |
|
162
|
9 |
|
$this->lastAccess = new DateTime(); |
|
163
|
9 |
|
} |
|
164
|
|
|
|
|
165
|
6 |
|
protected function setLastModification() |
|
166
|
|
|
{ |
|
167
|
6 |
|
$this->lastModification = new DateTime(); |
|
168
|
6 |
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|