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
|
|
|
* @param array $clients |
25
|
|
|
*/ |
26
|
2 |
|
public function __construct(array $clients) |
27
|
|
|
{ |
28
|
2 |
|
$this->data['stacks'] = []; |
29
|
2 |
|
$this->data['clients'] = $clients; |
30
|
2 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
|
|
public function collect(Request $request, Response $response, Exception $exception = null) |
36
|
|
|
{ |
37
|
|
|
// We do not need to collect any data from the Symfony Request and Response |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
1 |
|
public function getName() |
44
|
|
|
{ |
45
|
1 |
|
return 'httplug'; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param Stack $stack |
50
|
|
|
*/ |
51
|
|
|
public function addStack(Stack $stack) |
52
|
|
|
{ |
53
|
|
|
$this->data['stacks'][] = $stack; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return Stack[] |
58
|
|
|
*/ |
59
|
|
|
public function getStacks() |
60
|
|
|
{ |
61
|
|
|
return $this->data['stacks']; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return Stack|bool false if no current stack. |
66
|
|
|
*/ |
67
|
|
|
public function getCurrentStack() |
68
|
|
|
{ |
69
|
|
|
return end($this->data['stacks']); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return Stack[] |
74
|
|
|
*/ |
75
|
|
|
public function getSuccessfulStacks() |
76
|
|
|
{ |
77
|
|
|
return array_filter($this->data['stacks'], function (Stack $stack) { |
78
|
|
|
return !$stack->isFailed(); |
79
|
|
|
}); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return Stack[] |
84
|
|
|
*/ |
85
|
|
|
public function getFailedStacks() |
86
|
|
|
{ |
87
|
|
|
return array_filter($this->data['stacks'], function (Stack $stack) { |
88
|
|
|
return $stack->isFailed(); |
89
|
|
|
}); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
|
|
public function getClients() |
96
|
|
|
{ |
97
|
|
|
return $this->data['clients']; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param $client |
102
|
|
|
* |
103
|
|
|
* @return Stack[] |
104
|
|
|
*/ |
105
|
|
|
public function getClientStacks($client) |
106
|
|
|
{ |
107
|
|
|
return array_filter($this->data['stacks'], function (Stack $stack) use ($client) { |
108
|
|
|
return $stack->getClient() == $client; |
109
|
|
|
}); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return int |
114
|
|
|
*/ |
115
|
|
|
public function getTotalDuration() |
116
|
|
|
{ |
117
|
|
|
return array_reduce($this->data['stacks'], function ($carry, Stack $stack) { |
118
|
|
|
return $carry + $stack->getDuration(); |
119
|
|
|
}, 0); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|