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