-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathCollector.php
More file actions
161 lines (135 loc) · 4.19 KB
/
Collector.php
File metadata and controls
161 lines (135 loc) · 4.19 KB
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
<?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 <bourigaultfabien@gmail.com>
*
* @internal
*/
final class Collector extends DataCollector
{
private ?Stack $activeStack = null;
private ?int $capturedBodyLength = null;
public function __construct(?int $capturedBodyLength = null)
{
$this->capturedBodyLength = $capturedBodyLength;
$this->reset();
}
public function __wakeup(): void
{
$this->capturedBodyLength = null;
parent::__wakeup();
}
public function reset(): void
{
$this->data['stacks'] = [];
$this->activeStack = null;
}
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): void
{
if (null !== $this->activeStack) {
$stack->setParent($this->activeStack);
}
$this->activeStack = $stack;
}
/**
* Mark the stack as inactive.
*/
public function deactivateStack(Stack $stack): void
{
$this->activeStack = $stack->getParent();
}
public function getActiveStack(): ?Stack
{
return $this->activeStack;
}
public function addStack(Stack $stack): void
{
$this->data['stacks'][] = $stack;
}
/**
* @return Stack[]
*/
public function getChildrenStacks(Stack $parent): array
{
return array_filter($this->data['stacks'], static fn (Stack $stack) => $stack->getParent() === $parent);
}
/**
* @return Stack[]
*/
public function getStacks(): array
{
return $this->data['stacks'];
}
/**
* @return Stack[]
*/
public function getSuccessfulStacks(): array
{
return array_filter($this->data['stacks'], static fn (Stack $stack) => !$stack->isFailed());
}
/**
* @return Stack[]
*/
public function getFailedStacks(): array
{
return array_filter($this->data['stacks'], static fn (Stack $stack) => $stack->isFailed());
}
/**
* @return string[]
*/
public function getClients(): array
{
$stacks = array_filter($this->data['stacks'], static fn (Stack $stack) => null === $stack->getParent());
return array_unique(array_map(static fn (Stack $stack) => $stack->getClient(), $stacks));
}
/**
* @return Stack[]
*/
public function getClientRootStacks(string $client): array
{
return array_filter($this->data['stacks'], static fn (Stack $stack) => $stack->getClient() == $client && null == $stack->getParent());
}
/**
* Count all messages for a client.
*/
public function countClientMessages(string $client): int
{
return array_sum(array_map(fn (Stack $stack) => $this->countStackMessages($stack), $this->getClientRootStacks($client)));
}
/**
* Recursively count message in stack.
*/
private function countStackMessages(Stack $stack): int
{
return 1 + array_sum(array_map(fn (Stack $child) => $this->countStackMessages($child), $this->getChildrenStacks($stack)));
}
public function getTotalDuration(): int
{
return array_reduce($this->data['stacks'], static fn ($carry, Stack $stack) => $carry + $stack->getDuration(), 0);
}
public function collect(Request $request, Response $response, $exception = null): void
{
// We do not need to collect any data from the Symfony Request and Response
}
}