1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Http\HttplugBundle\Collector; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Symfony\Component\HttpFoundation\Request; |
7
|
|
|
use Symfony\Component\HttpFoundation\Response; |
8
|
|
|
use Symfony\Component\HttpKernel\DataCollector\DataCollector; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* The Collector hold profiled Stacks pushed by StackPlugin. It also have a list of configured clients. |
12
|
|
|
* All those data are used to display the HTTPlug panel in the Symfony profiler. |
13
|
|
|
* |
14
|
|
|
* The collector is not designed for execution in a threaded application and does not support plugins that execute an |
15
|
|
|
* other request before the current one is sent by the client. |
16
|
|
|
* |
17
|
|
|
* @author Fabien Bourigault <[email protected]> |
18
|
|
|
* |
19
|
|
|
* @internal |
20
|
|
|
*/ |
21
|
|
|
class Collector extends DataCollector |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var Stack|null |
25
|
|
|
*/ |
26
|
|
|
private $activeStack; |
27
|
|
|
|
28
|
18 |
|
public function __construct() |
29
|
|
|
{ |
30
|
18 |
|
$this->reset(); |
31
|
18 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
1 |
|
public function collect(Request $request, Response $response, Exception $exception = null) |
37
|
|
|
{ |
38
|
|
|
// We do not need to collect any data from the Symfony Request and Response |
39
|
1 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
18 |
|
public function reset() |
45
|
|
|
{ |
46
|
18 |
|
$this->data['stacks'] = []; |
47
|
18 |
|
$this->activeStack = null; |
48
|
18 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
5 |
|
public function getName() |
54
|
|
|
{ |
55
|
5 |
|
return 'httplug'; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Mark the stack as active. If a stack was already active, use it as parent for our stack. |
60
|
|
|
* |
61
|
|
|
* @param Stack $stack |
62
|
|
|
*/ |
63
|
7 |
|
public function activateStack(Stack $stack) |
64
|
|
|
{ |
65
|
7 |
|
if ($this->activeStack !== null) { |
66
|
2 |
|
$stack->setParent($this->activeStack); |
67
|
2 |
|
} |
68
|
|
|
|
69
|
7 |
|
$this->activeStack = $stack; |
70
|
7 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Mark the stack as inactive. |
74
|
|
|
* |
75
|
|
|
* @param Stack $stack |
76
|
|
|
*/ |
77
|
5 |
|
public function deactivateStack(Stack $stack) |
78
|
|
|
{ |
79
|
5 |
|
$this->activeStack = $stack->getParent(); |
80
|
5 |
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return Stack|null |
84
|
|
|
*/ |
85
|
7 |
|
public function getActiveStack() |
86
|
|
|
{ |
87
|
7 |
|
return $this->activeStack; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param Stack $stack |
92
|
|
|
*/ |
93
|
6 |
|
public function addStack(Stack $stack) |
94
|
|
|
{ |
95
|
6 |
|
$this->data['stacks'][] = $stack; |
96
|
6 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param Stack $parent |
100
|
|
|
* |
101
|
|
|
* @return Stack[] |
102
|
|
|
*/ |
103
|
|
|
public function getChildrenStacks(Stack $parent) |
104
|
|
|
{ |
105
|
|
|
return array_filter($this->data['stacks'], function (Stack $stack) use ($parent) { |
106
|
|
|
return $stack->getParent() === $parent; |
107
|
|
|
}); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return Stack[] |
112
|
|
|
*/ |
113
|
5 |
|
public function getStacks() |
114
|
|
|
{ |
115
|
5 |
|
return $this->data['stacks']; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return Stack[] |
120
|
|
|
*/ |
121
|
|
|
public function getSuccessfulStacks() |
122
|
|
|
{ |
123
|
|
|
return array_filter($this->data['stacks'], function (Stack $stack) { |
124
|
|
|
return !$stack->isFailed(); |
125
|
|
|
}); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return Stack[] |
130
|
|
|
*/ |
131
|
1 |
|
public function getFailedStacks() |
132
|
|
|
{ |
133
|
|
|
return array_filter($this->data['stacks'], function (Stack $stack) { |
134
|
|
|
return $stack->isFailed(); |
135
|
1 |
|
}); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return array |
140
|
|
|
*/ |
141
|
3 |
|
public function getClients() |
142
|
|
|
{ |
143
|
|
|
$stacks = array_filter($this->data['stacks'], function (Stack $stack) { |
144
|
2 |
|
return $stack->getParent() === null; |
145
|
3 |
|
}); |
146
|
|
|
|
147
|
|
|
return array_unique(array_map(function (Stack $stack) { |
148
|
2 |
|
return $stack->getClient(); |
149
|
3 |
|
}, $stacks)); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param $client |
154
|
|
|
* |
155
|
|
|
* @return Stack[] |
156
|
|
|
*/ |
157
|
1 |
|
public function getClientRootStacks($client) |
158
|
|
|
{ |
159
|
|
|
return array_filter($this->data['stacks'], function (Stack $stack) use ($client) { |
160
|
1 |
|
return $stack->getClient() == $client && $stack->getParent() == null; |
161
|
1 |
|
}); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Count all messages for a client. |
166
|
|
|
* |
167
|
|
|
* @param $client |
168
|
|
|
* |
169
|
|
|
* @return int |
170
|
|
|
*/ |
171
|
|
|
public function countClientMessages($client) |
172
|
|
|
{ |
173
|
|
|
return array_sum(array_map(function (Stack $stack) { |
174
|
|
|
return $this->countStackMessages($stack); |
175
|
|
|
}, $this->getClientRootStacks($client))); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Recursively count message in stack. |
180
|
|
|
* |
181
|
|
|
* @param Stack $stack |
182
|
|
|
* |
183
|
|
|
* @return int |
184
|
|
|
*/ |
185
|
|
|
private function countStackMessages(Stack $stack) |
186
|
|
|
{ |
187
|
|
|
return 1 + array_sum(array_map(function (Stack $child) { |
188
|
|
|
return $this->countStackMessages($child); |
189
|
|
|
}, $this->getChildrenStacks($stack))); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @return int |
194
|
|
|
*/ |
195
|
|
|
public function getTotalDuration() |
196
|
|
|
{ |
197
|
|
|
return array_reduce($this->data['stacks'], function ($carry, Stack $stack) { |
198
|
|
|
return $carry + $stack->getDuration(); |
199
|
|
|
}, 0); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|