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
|
6 |
|
public function __construct() |
24
|
|
|
{ |
25
|
6 |
|
$this->data['stacks'] = []; |
26
|
6 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
|
|
public function collect(Request $request, Response $response, Exception $exception = null) |
32
|
|
|
{ |
33
|
|
|
// We do not need to collect any data from the Symfony Request and Response |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
1 |
|
public function getName() |
40
|
|
|
{ |
41
|
1 |
|
return 'httplug'; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param Stack $stack |
46
|
|
|
*/ |
47
|
4 |
|
public function addStack(Stack $stack) |
48
|
|
|
{ |
49
|
4 |
|
$this->data['stacks'][] = $stack; |
50
|
4 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return Stack[] |
54
|
|
|
*/ |
55
|
3 |
|
public function getStacks() |
56
|
|
|
{ |
57
|
3 |
|
return $this->data['stacks']; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return Stack|null Return null there is no current stack. |
62
|
|
|
*/ |
63
|
3 |
|
public function getCurrentStack() |
64
|
|
|
{ |
65
|
3 |
|
if (false === $stack = end($this->data['stacks'])) { |
66
|
|
|
return null; |
67
|
|
|
} |
68
|
|
|
|
69
|
3 |
|
return $stack; |
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
|
1 |
|
public function getClients() |
96
|
|
|
{ |
97
|
|
|
return array_unique(array_map(function (Stack $stack) { |
98
|
1 |
|
return $stack->getClient(); |
99
|
1 |
|
}, $this->data['stacks'])); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param $client |
104
|
|
|
* |
105
|
|
|
* @return Stack[] |
106
|
|
|
*/ |
107
|
|
|
public function getClientStacks($client) |
108
|
|
|
{ |
109
|
|
|
return array_filter($this->data['stacks'], function (Stack $stack) use ($client) { |
110
|
|
|
return $stack->getClient() == $client; |
111
|
|
|
}); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return int |
116
|
|
|
*/ |
117
|
|
|
public function getTotalDuration() |
118
|
|
|
{ |
119
|
|
|
return array_reduce($this->data['stacks'], function ($carry, Stack $stack) { |
120
|
|
|
return $carry + $stack->getDuration(); |
121
|
|
|
}, 0); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|