|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace EightPoints\Bundle\GuzzleBundle\DataCollector; |
|
4
|
|
|
|
|
5
|
|
|
use EightPoints\Bundle\GuzzleBundle\Log\LogGroup; |
|
6
|
|
|
use EightPoints\Bundle\GuzzleBundle\Log\LoggerInterface; |
|
7
|
|
|
use EightPoints\Bundle\GuzzleBundle\Log\Logger; |
|
8
|
|
|
use EightPoints\Bundle\GuzzleBundle\Log\LogMessage; |
|
9
|
|
|
use Psr\Log\LogLevel; |
|
10
|
|
|
use Symfony\Component\HttpKernel\DataCollector\DataCollector; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
13
|
|
|
use Symfony\Component\HttpKernel\Kernel; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Collecting http data for Symfony profiler |
|
17
|
|
|
* |
|
18
|
|
|
* @version 2.1 |
|
19
|
|
|
* @since 2014-11 |
|
20
|
|
|
*/ |
|
21
|
|
|
class HttpDataCollector extends DataCollector |
|
22
|
|
|
{ |
|
23
|
|
|
/** @var Logger $logger */ |
|
24
|
|
|
protected $logger; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @version 2.1 |
|
28
|
|
|
* @since 2014-11 |
|
29
|
|
|
* |
|
30
|
|
|
* @param LoggerInterface $logger |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(LoggerInterface $logger) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->logger = $logger; |
|
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
$this->reset(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritdoc} |
|
41
|
|
|
* |
|
42
|
|
|
* @version 2.1 |
|
43
|
|
|
* @since 2014-11 |
|
44
|
|
|
*/ |
|
45
|
|
|
public function collect(Request $request, Response $response, \Exception $exception = null) |
|
46
|
|
|
{ |
|
47
|
|
|
$messages = $this->logger->getMessages(); |
|
48
|
|
|
$requestId = $request->getUri(); |
|
49
|
|
|
|
|
50
|
|
|
// clear log to have only messages related to Symfony request context |
|
51
|
|
|
$this->logger->clear(); |
|
52
|
|
|
|
|
53
|
|
|
$logGroup = $this->getLogGroup($requestId); |
|
54
|
|
|
$logGroup->setRequestName($request->getPathInfo()); |
|
55
|
|
|
$logGroup->addMessages($messages); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* {@inheritdoc} |
|
60
|
|
|
* |
|
61
|
|
|
* @version 2.1 |
|
62
|
|
|
* @since 2014-11 |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getName() : string |
|
65
|
|
|
{ |
|
66
|
|
|
return 'eight_points_guzzle'; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Resets this data collector to its initial state. |
|
71
|
|
|
*/ |
|
72
|
|
|
public function reset() |
|
73
|
|
|
{ |
|
74
|
|
|
$this->data = [ |
|
75
|
|
|
'logs' => [], |
|
76
|
|
|
'callCount' => 0, |
|
77
|
|
|
'totalTime' => 0, |
|
78
|
|
|
]; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Returning log entries |
|
83
|
|
|
* |
|
84
|
|
|
* @version 2.1 |
|
85
|
|
|
* @since 2014-11 |
|
86
|
|
|
* |
|
87
|
|
|
* @return array $logs |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getLogs() : array |
|
90
|
|
|
{ |
|
91
|
|
|
return array_key_exists('logs', $this->data) ? $this->data['logs'] : []; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Get all messages |
|
96
|
|
|
* |
|
97
|
|
|
* @version 2.1 |
|
98
|
|
|
* @since 2015-05 |
|
99
|
|
|
* |
|
100
|
|
|
* @return array |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getMessages() : array |
|
103
|
|
|
{ |
|
104
|
|
|
$messages = []; |
|
105
|
|
|
|
|
106
|
|
|
foreach ($this->getLogs() as $log) { |
|
107
|
|
|
foreach ($log->getMessages() as $message) { |
|
108
|
|
|
$messages[] = $message; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return $messages; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Return amount of http calls |
|
117
|
|
|
* |
|
118
|
|
|
* @version 2.1 |
|
119
|
|
|
* @since 2015-05 |
|
120
|
|
|
* |
|
121
|
|
|
* @return integer |
|
122
|
|
|
*/ |
|
123
|
|
|
public function getCallCount() : int |
|
124
|
|
|
{ |
|
125
|
|
|
return count($this->getMessages()); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Get Error Count |
|
130
|
|
|
* |
|
131
|
|
|
* @version 2.2 |
|
132
|
|
|
* @since 2015-05 |
|
133
|
|
|
* |
|
134
|
|
|
* @return integer |
|
135
|
|
|
*/ |
|
136
|
|
|
public function getErrorCount() : int |
|
137
|
|
|
{ |
|
138
|
|
|
return count(array_filter($this->getMessages(), function (LogMessage $message) { |
|
139
|
|
|
return $message->getLevel() === LogLevel::ERROR; |
|
140
|
|
|
})); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Get total time of all requests |
|
145
|
|
|
* |
|
146
|
|
|
* @since 2015-05 |
|
147
|
|
|
* |
|
148
|
|
|
* @return float |
|
149
|
|
|
*/ |
|
150
|
|
|
public function getTotalTime() : float |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->data['totalTime']; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param float $time |
|
157
|
|
|
*/ |
|
158
|
|
|
public function addTotalTime(float $time) |
|
159
|
|
|
{ |
|
160
|
|
|
$this->data['totalTime'] += $time; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Returns (new) LogGroup based on given id |
|
165
|
|
|
* |
|
166
|
|
|
* @version 2.1 |
|
167
|
|
|
* @since 2015-05 |
|
168
|
|
|
* |
|
169
|
|
|
* @param string $id |
|
170
|
|
|
* @return LogGroup |
|
171
|
|
|
*/ |
|
172
|
|
|
protected function getLogGroup(string $id) : LogGroup |
|
173
|
|
|
{ |
|
174
|
|
|
if (!isset($this->data['logs'][$id])) { |
|
175
|
|
|
$this->data['logs'][$id] = new LogGroup(); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
return $this->data['logs'][$id]; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Return the color used version |
|
183
|
|
|
* @since 2016-06 |
|
184
|
|
|
* |
|
185
|
|
|
* @return string |
|
186
|
|
|
*/ |
|
187
|
|
|
public final function getIconColor() : string |
|
188
|
|
|
{ |
|
189
|
|
|
if ((float)$this->getSymfonyVersion() >= 2.8) { |
|
190
|
|
|
return $this->data['iconColor'] = '#AAAAAA'; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
return $this->data['iconColor'] = '#3F3F3F'; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Returns current version symfony |
|
198
|
|
|
* @since 2016-06 |
|
199
|
|
|
* |
|
200
|
|
|
* @return string |
|
201
|
|
|
*/ |
|
202
|
|
|
private function getSymfonyVersion() : string |
|
203
|
|
|
{ |
|
204
|
|
|
$symfonyVersion = Kernel::VERSION; |
|
205
|
|
|
$symfonyVersion = explode('.', $symfonyVersion, -1); |
|
206
|
|
|
$symfonyMajorMinorVersion = implode('.', $symfonyVersion); |
|
207
|
|
|
|
|
208
|
|
|
return $symfonyMajorMinorVersion; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
} |
|
212
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.