1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sludio\HelperBundle\Guzzle\DataCollector; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Exception\RequestException; |
6
|
|
|
use Psr\Http\Message\StreamInterface; |
7
|
|
|
use Sludio\HelperBundle\Guzzle\GuzzleHttp\History\History; |
8
|
|
|
use Sludio\HelperBundle\Guzzle\GuzzleHttp\Middleware\CacheMiddleware; |
9
|
|
|
use Sludio\HelperBundle\Guzzle\GuzzleHttp\Middleware\MockMiddleware; |
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
11
|
|
|
use Symfony\Component\HttpFoundation\Response; |
12
|
|
|
use Symfony\Component\HttpKernel\DataCollector\DataCollector; |
13
|
|
|
|
14
|
|
|
class GuzzleCollector extends DataCollector |
15
|
|
|
{ |
16
|
|
|
const MAX_BODY_SIZE = 0x10000; |
17
|
|
|
|
18
|
|
|
private $maxBodySize; |
19
|
|
|
private $history; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Constructor. |
23
|
|
|
* |
24
|
|
|
* @param int $maxBodySize The max body size to store in the profiler storage |
25
|
|
|
* @param History|null $history |
26
|
|
|
*/ |
27
|
|
|
public function __construct($maxBodySize = self::MAX_BODY_SIZE, History $history = null) |
28
|
|
|
{ |
29
|
|
|
$this->maxBodySize = $maxBodySize; |
30
|
|
|
$this->history = $history ?: new History(); |
31
|
|
|
|
32
|
|
|
$this->data = []; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
|
|
public function collect(Request $request, Response $response, \Exception $exception = null) |
39
|
|
|
{ |
40
|
|
|
$data = []; |
41
|
|
|
|
42
|
|
|
foreach ($this->history as $historyRequest) { |
43
|
|
|
/* @var \Psr\Http\Message\RequestInterface $historyRequest */ |
44
|
|
|
$transaction = $this->history[$historyRequest]; |
45
|
|
|
/* @var \Psr\Http\Message\ResponseInterface $historyResponse */ |
46
|
|
|
$historyResponse = $transaction['response']; |
47
|
|
|
/* @var \Exception $error */ |
48
|
|
|
$error = $transaction['error']; |
49
|
|
|
/* @var array $info */ |
50
|
|
|
$info = $transaction['info']; |
51
|
|
|
|
52
|
|
|
$req = [ |
53
|
|
|
'request' => [ |
54
|
|
|
'method' => $historyRequest->getMethod(), |
55
|
|
|
'version' => $historyRequest->getProtocolVersion(), |
56
|
|
|
'headers' => $historyRequest->getHeaders(), |
57
|
|
|
'body' => $this->cropContent($historyRequest->getBody()), |
58
|
|
|
], |
59
|
|
|
'info' => $info, |
60
|
|
|
'uri' => urldecode($historyRequest->getUri()), |
61
|
|
|
'httpCode' => 0, |
62
|
|
|
'error' => null, |
63
|
|
|
]; |
64
|
|
|
|
65
|
|
|
if ($historyResponse) { |
66
|
|
|
$req['response'] = [ |
67
|
|
|
'reasonPhrase' => $historyResponse->getReasonPhrase(), |
68
|
|
|
'headers' => $historyResponse->getHeaders(), |
69
|
|
|
'body' => $this->cropContent($historyResponse->getBody()), |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
$req['httpCode'] = $historyResponse->getStatusCode(); |
73
|
|
|
|
74
|
|
|
if ($historyResponse->hasHeader(CacheMiddleware::DEBUG_HEADER)) { |
75
|
|
|
$req['cache'] = $historyResponse->getHeaderLine(CacheMiddleware::DEBUG_HEADER); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if ($historyResponse->hasHeader(MockMiddleware::DEBUG_HEADER)) { |
79
|
|
|
$req['mock'] = $historyResponse->getHeaderLine(MockMiddleware::DEBUG_HEADER); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if ($error && $error instanceof RequestException) { |
84
|
|
|
$req['error'] = [ |
85
|
|
|
'message' => $error->getMessage(), |
86
|
|
|
'line' => $error->getLine(), |
87
|
|
|
'file' => $error->getFile(), |
88
|
|
|
'code' => $error->getCode(), |
89
|
|
|
'trace' => $error->getTraceAsString(), |
90
|
|
|
]; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$data[] = $req; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$this->data = $data; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
private function cropContent(StreamInterface $stream = null) |
100
|
|
|
{ |
101
|
|
|
if (null === $stream) { |
102
|
|
|
return ''; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if ($stream->getSize() <= $this->maxBodySize) { |
106
|
|
|
return (string)$stream; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$stream->seek(0); |
110
|
|
|
|
111
|
|
|
return '(partial content)'.$stream->read($this->maxBodySize).'(...)'; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function getErrors() |
115
|
|
|
{ |
116
|
|
|
return array_filter($this->data, function($call) { |
117
|
|
|
return 0 === $call['httpCode'] || $call['httpCode'] >= 400; |
118
|
|
|
}); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function getTotalTime() |
122
|
|
|
{ |
123
|
|
|
return array_sum(array_map(function($call) { |
124
|
|
|
return isset($call['info']['total_time']) ? $call['info']['total_time'] : 0; |
125
|
|
|
}, $this->data)); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function getCalls() |
129
|
|
|
{ |
130
|
|
|
return $this->data; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritdoc} |
135
|
|
|
*/ |
136
|
|
|
public function getName() |
137
|
|
|
{ |
138
|
|
|
return 'guzzle'; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|