1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Http\Message; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\StreamInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Represent a stream cloned into memory. |
9
|
|
|
*/ |
10
|
|
|
class MemoryClonedStream implements StreamInterface |
11
|
|
|
{ |
12
|
|
|
const COPY_BUFFER = 8192; |
13
|
|
|
|
14
|
|
|
private $resource; |
15
|
|
|
|
16
|
|
|
private $size; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param StreamInterface $stream Stream to clone |
20
|
|
|
* @param bool $useFileBuffer Use a file buffer to avoid memory consumption on PHP script (default to true) |
21
|
|
|
* @param int $memoryBuffer The amount of memory of which the stream is buffer into a file when setting |
22
|
|
|
* $useFileBuffer to true (default to 2MB) |
23
|
|
|
*/ |
24
|
1 |
|
public function __construct(StreamInterface $stream, $useFileBuffer = true, $memoryBuffer = 2097152) |
25
|
|
|
{ |
26
|
1 |
|
$this->size = 0; |
27
|
|
|
|
28
|
1 |
|
if ($useFileBuffer) { |
29
|
1 |
|
$this->resource = fopen('php://temp/maxmemory:'.$memoryBuffer, 'rw+'); |
30
|
1 |
|
} else { |
31
|
|
|
$this->resource = fopen('php://memory', 'rw+'); |
32
|
|
|
} |
33
|
|
|
|
34
|
1 |
|
$position = null; |
35
|
|
|
|
36
|
1 |
|
if ($stream->isSeekable()) { |
37
|
1 |
|
$position = $stream->tell(); |
38
|
1 |
|
$stream->rewind(); |
39
|
1 |
|
} |
40
|
|
|
|
41
|
1 |
|
while (!$stream->eof()) { |
42
|
1 |
|
$this->size += fwrite($this->resource, $stream->read(self::COPY_BUFFER)); |
43
|
1 |
|
} |
44
|
|
|
|
45
|
1 |
|
if ($stream->isSeekable()) { |
46
|
1 |
|
$stream->seek($position); |
47
|
1 |
|
} |
48
|
|
|
|
49
|
1 |
|
rewind($this->resource); |
50
|
1 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
public function __toString() |
56
|
|
|
{ |
57
|
|
|
return $this->getContents(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function close() |
64
|
|
|
{ |
65
|
|
|
fclose($this->resource); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
|
|
public function detach() |
72
|
|
|
{ |
73
|
|
|
$resource = $this->resource; |
74
|
|
|
$this->resource = null; |
75
|
|
|
|
76
|
|
|
return $resource; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
|
|
public function getSize() |
83
|
|
|
{ |
84
|
|
|
return $this->size; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
|
|
public function tell() |
91
|
|
|
{ |
92
|
|
|
return ftell($this->resource); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
|
|
public function eof() |
99
|
|
|
{ |
100
|
|
|
return feof($this->resource); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritdoc} |
105
|
|
|
*/ |
106
|
|
|
public function isSeekable() |
107
|
|
|
{ |
108
|
|
|
return true; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritdoc} |
113
|
|
|
*/ |
114
|
|
|
public function seek($offset, $whence = SEEK_SET) |
115
|
|
|
{ |
116
|
|
|
return fseek($this->resource, $offset); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritdoc} |
121
|
|
|
*/ |
122
|
|
|
public function rewind() |
123
|
|
|
{ |
124
|
|
|
return rewind($this->resource); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* {@inheritdoc} |
129
|
|
|
*/ |
130
|
|
|
public function isWritable() |
131
|
|
|
{ |
132
|
|
|
return true; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* {@inheritdoc} |
137
|
|
|
*/ |
138
|
|
|
public function write($string) |
139
|
|
|
{ |
140
|
|
|
return fwrite($this->resource, $string); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* {@inheritdoc} |
145
|
|
|
*/ |
146
|
|
|
public function isReadable() |
147
|
|
|
{ |
148
|
|
|
return true; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* {@inheritdoc} |
153
|
|
|
*/ |
154
|
|
|
public function read($length) |
155
|
|
|
{ |
156
|
|
|
return fread($this->resource, $length); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* {@inheritdoc} |
161
|
|
|
*/ |
162
|
|
|
public function getContents() |
163
|
|
|
{ |
164
|
|
|
$this->rewind(); |
165
|
|
|
|
166
|
|
|
return $this->read($this->size); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* {@inheritdoc} |
171
|
|
|
*/ |
172
|
|
|
public function getMetadata($key = null) |
173
|
|
|
{ |
174
|
|
|
$metadata = stream_get_meta_data($this->resource); |
175
|
|
|
|
176
|
|
|
if (null === $key) { |
177
|
|
|
return $metadata; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $metadata[$key]; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|