|
1
|
|
|
<?php |
|
2
|
|
|
namespace GuzzleHttp\Command\Guzzle\RequestLocation; |
|
3
|
|
|
|
|
4
|
|
|
use GuzzleHttp\Command\CommandInterface; |
|
5
|
|
|
use GuzzleHttp\Command\Guzzle\Operation; |
|
6
|
|
|
use GuzzleHttp\Command\Guzzle\Parameter; |
|
7
|
|
|
use GuzzleHttp\Psr7; |
|
8
|
|
|
use Psr\Http\Message\RequestInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Adds POST files to a request |
|
12
|
|
|
*/ |
|
13
|
|
|
class MultiPartLocation extends AbstractLocation |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var string $contentType */ |
|
16
|
|
|
protected $contentType = 'multipart/form-data; boundary='; |
|
17
|
|
|
|
|
18
|
|
|
/** @var array $formParamsData */ |
|
19
|
|
|
protected $multipartData = []; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Set the name of the location |
|
23
|
|
|
* |
|
24
|
|
|
* @param string $locationName |
|
25
|
|
|
*/ |
|
26
|
1 |
|
public function __construct($locationName = 'multipart') |
|
27
|
|
|
{ |
|
28
|
1 |
|
parent::__construct($locationName); |
|
29
|
1 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param CommandInterface $command |
|
33
|
|
|
* @param RequestInterface $request |
|
34
|
|
|
* @param Parameter $param |
|
35
|
|
|
* @return RequestInterface |
|
36
|
|
|
*/ |
|
37
|
1 |
|
public function visit( |
|
38
|
|
|
CommandInterface $command, |
|
39
|
|
|
RequestInterface $request, |
|
40
|
|
|
Parameter $param |
|
41
|
|
|
) { |
|
42
|
1 |
|
$this->multipartData[] = [ |
|
43
|
1 |
|
'name' => $param->getWireName(), |
|
44
|
1 |
|
'contents' => $this->prepareValue($command[$param->getName()], $param) |
|
|
|
|
|
|
45
|
1 |
|
]; |
|
46
|
|
|
|
|
47
|
1 |
|
return $request; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param CommandInterface $command |
|
53
|
|
|
* @param RequestInterface $request |
|
54
|
|
|
* @param Operation $operation |
|
55
|
|
|
* @return RequestInterface |
|
56
|
|
|
*/ |
|
57
|
1 |
|
public function after( |
|
58
|
|
|
CommandInterface $command, |
|
59
|
|
|
RequestInterface $request, |
|
60
|
|
|
Operation $operation |
|
61
|
|
|
) { |
|
62
|
1 |
|
$data = $this->multipartData; |
|
63
|
1 |
|
$this->multipartData = []; |
|
64
|
1 |
|
$modify = []; |
|
65
|
|
|
|
|
66
|
1 |
|
$body = new Psr7\MultipartStream($data); |
|
67
|
1 |
|
$modify['body'] = Psr7\stream_for($body); |
|
68
|
1 |
|
$request = Psr7\modify_request($request, $modify); |
|
69
|
1 |
|
if ($request->getBody() instanceof Psr7\MultipartStream) { |
|
|
|
|
|
|
70
|
|
|
// Use a multipart/form-data POST if a Content-Type is not set. |
|
71
|
1 |
|
$request->withHeader('Content-Type', $this->contentType . $request->getBody()->getBoundary()); |
|
72
|
1 |
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
return $request; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|