1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Thruster\Component\HttpMessage; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\StreamInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class MultipartStream |
9
|
|
|
* |
10
|
|
|
* @package Thruster\Component\HttpMessage |
11
|
|
|
* @author Aurimas Niekis <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class MultipartStream implements StreamInterface |
14
|
|
|
{ |
15
|
|
|
use StreamDecoratorTrait; |
16
|
|
|
|
17
|
|
|
private $boundary; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param array $elements Array of associative arrays, each containing a |
21
|
|
|
* required "name" key mapping to the form field, |
22
|
|
|
* name, a required "contents" key mapping to a |
23
|
|
|
* StreamInterface/resource/string, an optional |
24
|
|
|
* "headers" associative array of custom headers, |
25
|
|
|
* and an optional "filename" key mapping to a |
26
|
|
|
* string to send as the filename in the part. |
27
|
|
|
* @param string $boundary You can optionally provide a specific boundary |
28
|
|
|
* |
29
|
|
|
* @throws \InvalidArgumentException |
30
|
|
|
*/ |
31
|
10 |
|
public function __construct(array $elements = [], $boundary = null) |
32
|
|
|
{ |
33
|
10 |
|
$this->boundary = $boundary ?? uniqid(); |
34
|
10 |
|
$this->stream = $this->createStream($elements); |
|
|
|
|
35
|
8 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Get the boundary |
39
|
|
|
* |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
3 |
|
public function getBoundary() |
43
|
|
|
{ |
44
|
3 |
|
return $this->boundary; |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
public function isWritable() : bool |
48
|
|
|
{ |
49
|
1 |
|
return false; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get the headers needed before transferring the content of a POST file |
54
|
|
|
*/ |
55
|
4 |
|
private function getHeaders(array $headers) |
56
|
|
|
{ |
57
|
4 |
|
$str = ''; |
58
|
4 |
|
foreach ($headers as $key => $value) { |
59
|
4 |
|
$str .= "{$key}: {$value}\r\n"; |
60
|
|
|
} |
61
|
|
|
|
62
|
4 |
|
return "--{$this->boundary}\r\n" . trim($str) . "\r\n\r\n"; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Create the aggregate stream that will be used to upload the POST data |
67
|
|
|
*/ |
68
|
10 |
|
protected function createStream(array $elements) |
69
|
|
|
{ |
70
|
10 |
|
$stream = new AppendStream(); |
71
|
|
|
|
72
|
10 |
|
foreach ($elements as $element) { |
73
|
6 |
|
$this->addElement($stream, $element); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// Add the trailing boundary with CRLF |
77
|
8 |
|
$stream->addStream(stream_for("--{$this->boundary}--\r\n")); |
78
|
|
|
|
79
|
8 |
|
return $stream; |
80
|
|
|
} |
81
|
|
|
|
82
|
6 |
|
private function addElement(AppendStream $stream, array $element) |
83
|
|
|
{ |
84
|
6 |
|
foreach (['contents', 'name'] as $key) { |
85
|
6 |
|
if (false === array_key_exists($key, $element)) { |
86
|
6 |
|
throw new \InvalidArgumentException("A '{$key}' key is required"); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
4 |
|
$element['contents'] = stream_for($element['contents']); |
91
|
|
|
|
92
|
4 |
|
if (empty($element['filename'])) { |
93
|
4 |
|
$uri = $element['contents']->getMetadata('uri'); |
94
|
4 |
|
if ('php://' !== substr($uri, 0, 6)) { |
95
|
3 |
|
$element['filename'] = $uri; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
4 |
|
list($body, $headers) = $this->createElement( |
100
|
4 |
|
$element['name'], |
101
|
4 |
|
$element['contents'], |
102
|
4 |
|
$element['filename'] ?? null, |
103
|
4 |
|
$element['headers'] ?? [] |
104
|
|
|
); |
105
|
|
|
|
106
|
4 |
|
$stream->addStream(stream_for($this->getHeaders($headers))); |
107
|
4 |
|
$stream->addStream($body); |
108
|
4 |
|
$stream->addStream(stream_for("\r\n")); |
109
|
4 |
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
4 |
|
private function createElement($name, $stream, $filename, array $headers) |
115
|
|
|
{ |
116
|
|
|
// Set a default content-disposition header if one was no provided |
117
|
4 |
|
$disposition = $this->getHeader($headers, 'content-disposition'); |
118
|
4 |
|
if (!$disposition) { |
119
|
3 |
|
$headers['Content-Disposition'] = $filename |
120
|
2 |
|
? sprintf('form-data; name="%s"; filename="%s"', |
121
|
|
|
$name, |
122
|
|
|
basename($filename)) |
123
|
1 |
|
: "form-data; name=\"{$name}\""; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
// Set a default content-length header if one was no provided |
127
|
4 |
|
$length = $this->getHeader($headers, 'content-length'); |
128
|
4 |
|
if (!$length) { |
129
|
4 |
|
if ($length = $stream->getSize()) { |
130
|
4 |
|
$headers['Content-Length'] = (string) $length; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
// Set a default Content-Type if one was not supplied |
135
|
4 |
|
$type = $this->getHeader($headers, 'content-type'); |
136
|
4 |
|
if (!$type && $filename) { |
137
|
3 |
|
if ($type = mimetype_from_filename($filename)) { |
138
|
3 |
|
$headers['Content-Type'] = $type; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
4 |
|
return [$stream, $headers]; |
143
|
|
|
} |
144
|
|
|
|
145
|
4 |
|
private function getHeader(array $headers, $key) |
146
|
|
|
{ |
147
|
4 |
|
$lowercaseHeader = strtolower($key); |
148
|
4 |
|
foreach ($headers as $k => $v) { |
149
|
4 |
|
if (strtolower($k) === $lowercaseHeader) { |
150
|
4 |
|
return $v; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
4 |
|
return null; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: