1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\RemoteRequestPsr\Adapters; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\RemoteRequest\RequestException; |
7
|
|
|
use kalanis\RemoteRequestPsr\Http\Answer; |
8
|
|
|
use Psr\Http\Message\StreamInterface; |
9
|
|
|
use RuntimeException; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class StreamAdapter |
14
|
|
|
* @package kalanis\RemoteRequestPsr\Adapters |
15
|
|
|
* Adapter of body of HTTP response |
16
|
|
|
* Returns workable stream under interface |
17
|
|
|
*/ |
18
|
|
|
class StreamAdapter implements StreamInterface |
19
|
|
|
{ |
20
|
|
|
/** @var resource|null */ |
21
|
|
|
protected $localStream = null; |
22
|
|
|
protected int $offset = 0; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param Answer $answer |
26
|
|
|
* @throws RequestException |
27
|
|
|
*/ |
28
|
13 |
|
public function __construct(Answer $answer) |
29
|
|
|
{ |
30
|
13 |
|
$content = $answer->getBody(); |
31
|
13 |
|
$this->localStream = is_resource($content) ? $content : $this->toStream($content); |
32
|
13 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string|int|float|bool|null $content |
36
|
|
|
* @throws RequestException |
37
|
|
|
* @return resource |
38
|
|
|
*/ |
39
|
1 |
|
protected function toStream($content) |
40
|
|
|
{ |
41
|
1 |
|
$stream = fopen('php://temp', 'rb+'); |
42
|
1 |
|
if (false === $stream) { |
43
|
|
|
// @codeCoverageIgnoreStart |
44
|
|
|
throw new RequestException('Problem with php internals'); |
45
|
|
|
} |
46
|
|
|
// @codeCoverageIgnoreEnd |
47
|
1 |
|
fwrite($stream, strval($content)); |
48
|
1 |
|
return $stream; |
49
|
|
|
} |
50
|
|
|
|
51
|
3 |
|
public function __toString(): string |
52
|
|
|
{ |
53
|
|
|
try { |
54
|
3 |
|
return strval(stream_get_contents($this->getLocalStream(), -1, 0)); |
55
|
1 |
|
} catch (RuntimeException $ex) { |
56
|
1 |
|
return ''; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
4 |
|
public function close(): void |
61
|
|
|
{ |
62
|
|
|
try { |
63
|
4 |
|
fclose($this->getLocalStream()); |
64
|
4 |
|
$this->localStream = null; |
65
|
1 |
|
} catch (RuntimeException $ex) { |
66
|
|
|
// nothing to do |
67
|
|
|
} |
68
|
4 |
|
} |
69
|
|
|
|
70
|
1 |
|
public function detach() |
71
|
|
|
{ |
72
|
1 |
|
$stream = $this->localStream; |
73
|
1 |
|
$this->localStream = null; |
74
|
1 |
|
$this->offset = 0; |
75
|
1 |
|
return $stream; |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
public function getSize(): ?int |
79
|
|
|
{ |
80
|
1 |
|
$data = fstat($this->getLocalStream()); |
81
|
1 |
|
return (false !== $data) ? $data['size'] : null; |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
public function tell(): int |
85
|
|
|
{ |
86
|
1 |
|
return intval(ftell($this->getLocalStream())); |
87
|
|
|
} |
88
|
|
|
|
89
|
1 |
|
public function eof(): bool |
90
|
|
|
{ |
91
|
1 |
|
return feof($this->getLocalStream()); |
92
|
|
|
} |
93
|
|
|
|
94
|
1 |
|
public function isSeekable(): bool |
95
|
|
|
{ |
96
|
1 |
|
return !empty($this->localStream); |
97
|
|
|
} |
98
|
|
|
|
99
|
1 |
|
public function seek(int $offset, int $whence = SEEK_SET): void |
100
|
|
|
{ |
101
|
1 |
|
fseek($this->getLocalStream(), $offset, $whence); |
102
|
1 |
|
$this->offset = $this->tell(); |
103
|
1 |
|
} |
104
|
|
|
|
105
|
1 |
|
public function rewind(): void |
106
|
|
|
{ |
107
|
1 |
|
$this->seek(0); |
108
|
1 |
|
} |
109
|
|
|
|
110
|
1 |
|
public function isWritable(): bool |
111
|
|
|
{ |
112
|
1 |
|
return false; |
113
|
|
|
} |
114
|
|
|
|
115
|
1 |
|
public function write(string $string): int |
116
|
|
|
{ |
117
|
1 |
|
throw new RuntimeException('Cannot write into output stream.'); |
118
|
|
|
} |
119
|
|
|
|
120
|
1 |
|
public function isReadable(): bool |
121
|
|
|
{ |
122
|
1 |
|
return !empty($this->localStream); |
123
|
|
|
} |
124
|
|
|
|
125
|
2 |
|
public function read(int $length): string |
126
|
|
|
{ |
127
|
2 |
|
$data = strval(stream_get_contents($this->getLocalStream(), $length, $this->offset)); |
128
|
2 |
|
$this->offset += strlen($data); |
129
|
2 |
|
return $data; |
130
|
|
|
} |
131
|
|
|
|
132
|
3 |
|
public function getContents(): string |
133
|
|
|
{ |
134
|
3 |
|
$data = strval(stream_get_contents($this->getLocalStream(), -1, $this->offset)); |
135
|
3 |
|
$this->offset += strlen($data); |
136
|
3 |
|
return $data; |
137
|
|
|
} |
138
|
|
|
|
139
|
1 |
|
public function getMetadata(?string $key = null) |
140
|
|
|
{ |
141
|
|
|
try { |
142
|
1 |
|
$data = stream_get_meta_data($this->getLocalStream()); |
143
|
1 |
|
if (!is_null($key)) { |
144
|
1 |
|
return (isset($data[$key])) ? $data[$key] : null; |
145
|
|
|
} else { |
146
|
1 |
|
return $data; |
147
|
|
|
} |
148
|
1 |
|
} catch (RuntimeException $ex) { |
149
|
1 |
|
return null; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @throws RuntimeException |
155
|
|
|
* @return resource |
156
|
|
|
*/ |
157
|
9 |
|
protected function getLocalStream() |
158
|
|
|
{ |
159
|
9 |
|
if (empty($this->localStream)) { |
160
|
3 |
|
throw new RuntimeException('No stream available!'); |
161
|
|
|
} |
162
|
9 |
|
return $this->localStream; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|