1 | <?php |
||
14 | class CurlCommandFormatter implements Formatter |
||
15 | { |
||
16 | /** |
||
17 | * {@inheritdoc} |
||
18 | */ |
||
19 | 5 | public function formatRequest(RequestInterface $request) |
|
20 | { |
||
21 | 5 | $command = sprintf('curl %s', escapeshellarg((string) $request->getUri()->withFragment(''))); |
|
22 | 5 | if ('1.0' === $request->getProtocolVersion()) { |
|
23 | $command .= ' --http1.0'; |
||
24 | 5 | } elseif ('2.0' === $request->getProtocolVersion()) { |
|
25 | 1 | $command .= ' --http2'; |
|
26 | } |
||
27 | |||
28 | 5 | $method = strtoupper($request->getMethod()); |
|
29 | 5 | if ('HEAD' === $method) { |
|
30 | $command .= ' --head'; |
||
31 | 5 | } elseif ('GET' !== $method) { |
|
32 | 3 | $command .= ' --request '.$method; |
|
33 | } |
||
34 | |||
35 | 5 | $command .= $this->getHeadersAsCommandOptions($request); |
|
36 | |||
37 | 5 | $body = $request->getBody(); |
|
38 | 5 | if ($body->getSize() > 0) { |
|
39 | 3 | if ($body->isSeekable()) { |
|
40 | 2 | $data = $body->__toString(); |
|
41 | 2 | $body->rewind(); |
|
42 | 2 | if (preg_match('/[\x00-\x1F\x7F]/', $data)) { |
|
43 | 2 | $data = '[binary stream omitted]'; |
|
44 | } |
||
45 | } else { |
||
46 | 1 | $data = '[non-seekable stream omitted]'; |
|
47 | } |
||
48 | 3 | $escapedData = @escapeshellarg($data); |
|
49 | 3 | if (empty($escapedData)) { |
|
50 | $escapedData = 'We couldn\'t not escape the data properly'; |
||
51 | } |
||
52 | |||
53 | 3 | $command .= sprintf(' --data %s', $escapedData); |
|
54 | } |
||
55 | |||
56 | 5 | return $command; |
|
57 | } |
||
58 | |||
59 | /** |
||
60 | * {@inheritdoc} |
||
61 | */ |
||
62 | 1 | public function formatResponse(ResponseInterface $response) |
|
66 | |||
67 | /** |
||
68 | * @param RequestInterface $request |
||
69 | * |
||
70 | * @return string |
||
71 | */ |
||
72 | 5 | private function getHeadersAsCommandOptions(RequestInterface $request) |
|
91 | } |
||
92 |