|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/** |
|
4
|
|
|
* Minotaur |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
|
7
|
|
|
* use this file except in compliance with the License. You may obtain a copy of |
|
8
|
|
|
* the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
|
14
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
|
15
|
|
|
* License for the specific language governing permissions and limitations under |
|
16
|
|
|
* the License. |
|
17
|
|
|
* |
|
18
|
|
|
* @copyright 2015-2017 Appertly |
|
19
|
|
|
* @license Apache-2.0 |
|
20
|
|
|
*/ |
|
21
|
|
|
namespace Minotaur\Io; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Allows PSR-7 streams to be used as PHP streams. |
|
25
|
|
|
* |
|
26
|
|
|
* Based on Guzzle PSR7 stream wrapper and MongoDB's PHP library stream wrapper. |
|
27
|
|
|
*/ |
|
28
|
|
|
class StreamWrapper |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var resource|null The stream context |
|
32
|
|
|
*/ |
|
33
|
|
|
public $context; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var StreamInterface|null The PSR-7 stream |
|
37
|
|
|
*/ |
|
38
|
|
|
private $stream; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var string|null The stream mode |
|
42
|
|
|
*/ |
|
43
|
|
|
private $mode; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Registers this stream wrapper |
|
47
|
|
|
*/ |
|
48
|
|
|
public static function register(): void |
|
49
|
|
|
{ |
|
50
|
|
|
if (!in_array('psr7', stream_get_wrappers())) { |
|
51
|
|
|
stream_wrapper_register('psr7', __CLASS__); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Unregisters this stream wrapper |
|
57
|
|
|
*/ |
|
58
|
|
|
public static function unregister(): void |
|
59
|
|
|
{ |
|
60
|
|
|
if (in_array('psr7', stream_get_wrappers())) { |
|
61
|
|
|
stream_wrapper_unregister('psr7'); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Gets a PHP stream resource for the provided PSR-7 stream. |
|
67
|
|
|
* |
|
68
|
|
|
* @param \Psr\Http\Message\StreamInterface $stream The stream to wrap |
|
69
|
|
|
* @return resource The generated resource |
|
70
|
|
|
* @throws \InvalidArgumentException if stream is not readable or writable |
|
71
|
|
|
*/ |
|
72
|
|
|
public static function getResource(\Psr\Http\Message\StreamInterface $stream) |
|
73
|
|
|
{ |
|
74
|
|
|
self::register(); |
|
75
|
|
|
if ($stream->isReadable()) { |
|
76
|
|
|
$mode = $stream->isWritable() ? 'r+' : 'r'; |
|
77
|
|
|
} elseif ($stream->isWritable()) { |
|
78
|
|
|
$mode = 'w'; |
|
79
|
|
|
} else { |
|
80
|
|
|
throw new \InvalidArgumentException('The stream must be readable, writable, or both'); |
|
81
|
|
|
} |
|
82
|
|
|
return fopen('psr7://stream', $mode, false, stream_context_create([ |
|
83
|
|
|
'psr7' => ['stream' => $stream] |
|
84
|
|
|
])); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function stream_open(string $path, string $mode, int $options, ?string &$opened_path) |
|
|
|
|
|
|
88
|
|
|
{ |
|
89
|
|
|
$options = stream_context_get_options($this->context); |
|
90
|
|
|
if (!isset($options['psr7']['stream'])) { |
|
91
|
|
|
return false; |
|
92
|
|
|
} |
|
93
|
|
|
$this->stream = $options['psr7']['stream']; |
|
94
|
|
|
$this->mode = $mode; |
|
95
|
|
|
return true; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function stream_read(int $count): string |
|
|
|
|
|
|
99
|
|
|
{ |
|
100
|
|
|
return $this->stream->read($count); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function stream_write(string $data): int |
|
|
|
|
|
|
104
|
|
|
{ |
|
105
|
|
|
return (int) $this->stream->write($data); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function stream_tell(): int |
|
|
|
|
|
|
109
|
|
|
{ |
|
110
|
|
|
return $this->stream->tell(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function stream_eof(): bool |
|
|
|
|
|
|
114
|
|
|
{ |
|
115
|
|
|
return $this->stream->eof(); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function stream_seek(int $offset, int $whence = SEEK_SET): bool |
|
|
|
|
|
|
119
|
|
|
{ |
|
120
|
|
|
return $this->stream->seek($offset, $whence); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function stream_stat(): array |
|
|
|
|
|
|
124
|
|
|
{ |
|
125
|
|
|
static $modeMap = [ |
|
126
|
|
|
'r' => 33060, |
|
127
|
|
|
'r+' => 33206, |
|
128
|
|
|
'w' => 33188 |
|
129
|
|
|
]; |
|
130
|
|
|
$stat = $this->getStatTemplate(); |
|
131
|
|
|
$stat[2] = $stat['mode'] = $modeMap[$this->mode]; |
|
132
|
|
|
$stat[7] = $stat['size'] = $this->stream->getSize(); |
|
133
|
|
|
return $stat; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Gets a URL stat template with default values. |
|
138
|
|
|
* |
|
139
|
|
|
* Blatently lifted from https://github.com/aws/aws-sdk-php/blob/master/src/S3/StreamWrapper.php |
|
140
|
|
|
* |
|
141
|
|
|
* @return - The stat template. |
|
|
|
|
|
|
142
|
|
|
*/ |
|
143
|
|
|
private function getStatTemplate(): array |
|
144
|
|
|
{ |
|
145
|
|
|
return [ |
|
146
|
|
|
0 => 0, 'dev' => 0, |
|
147
|
|
|
1 => 0, 'ino' => 0, |
|
148
|
|
|
2 => 0, 'mode' => 0, |
|
149
|
|
|
3 => 0, 'nlink' => 0, |
|
150
|
|
|
4 => 0, 'uid' => 0, |
|
151
|
|
|
5 => 0, 'gid' => 0, |
|
152
|
|
|
6 => -1, 'rdev' => -1, |
|
153
|
|
|
7 => 0, 'size' => 0, |
|
154
|
|
|
8 => 0, 'atime' => 0, |
|
155
|
|
|
9 => 0, 'mtime' => 0, |
|
156
|
|
|
10 => 0, 'ctime' => 0, |
|
157
|
|
|
11 => -1, 'blksize' => -1, |
|
158
|
|
|
12 => -1, 'blocks' => -1, |
|
159
|
|
|
]; |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.