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