Skip to content

Commit 96bf3a1

Browse files
committed
Extended data collector
1 parent eb0e789 commit 96bf3a1

File tree

7 files changed

+272
-163
lines changed

7 files changed

+272
-163
lines changed

Collector/ClientDataCollector.php

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
<?php
2+
3+
namespace Http\HttplugBundle\Collector;
4+
5+
/**
6+
* An object to handle the collected data for a client.
7+
*
8+
* The Request object at $requests[0][2] is the state of the object between the third
9+
* and the fourth plugin. The response after that plugin is found in $responses[0][2].
10+
*
11+
* @author Tobias Nyholm <[email protected]>
12+
*/
13+
class ClientDataCollector
14+
{
15+
/**
16+
* A multidimensional array with requests.
17+
* $requests[0][0] is the first request before all plugins.
18+
* $requests[0][1] is the first request after the first plugin.
19+
*
20+
* @var array
21+
*/
22+
private $requests;
23+
24+
/**
25+
* A multidimensional array with responses.
26+
* $responses[0][0] is the first responses before all plugins.
27+
* $responses[0][1] is the first responses after the first plugin.
28+
*
29+
* @var array
30+
*/
31+
private $responses;
32+
33+
/**
34+
*
35+
* @param array $requests
36+
* @param array $responses
37+
*/
38+
public function __construct(array $requests, array $responses)
39+
{
40+
$this->requests = $requests;
41+
$this->responses = $responses;
42+
}
43+
44+
/**
45+
* Create an array of ClientDataCollector from collected data.
46+
*
47+
* @param array $data
48+
*
49+
* @return ClientDataCollector[]
50+
*/
51+
public static function createFromCollectedData(array $data)
52+
{
53+
$clientData = [];
54+
foreach ($data as $clientName => $messages) {
55+
$clientData[$clientName] = static::createOne($messages);
56+
}
57+
58+
return $clientData;
59+
}
60+
61+
/**
62+
* @param array $messages
63+
*
64+
* @return ClientDataCollector
65+
*/
66+
private static function createOne($messages)
67+
{
68+
$orderedRequests = [];
69+
$orderedResponses = [];
70+
71+
foreach ($messages['request'] as $depth => $requests) {
72+
foreach ($requests as $idx => $request) {
73+
$orderedRequests[$idx][$depth] = $request;
74+
}
75+
}
76+
77+
foreach ($messages['response'] as $depth => $responses) {
78+
foreach ($responses as $idx => $response) {
79+
$orderedResponses[$idx][$depth] = $response;
80+
}
81+
}
82+
83+
return new self($orderedRequests, $orderedResponses);
84+
}
85+
86+
/**
87+
* @return array
88+
*/
89+
public function getRequests()
90+
{
91+
return $this->requests;
92+
}
93+
94+
/**
95+
* @param array $requests
96+
*
97+
* @return ClientDataCollector
98+
*/
99+
public function setRequests($requests)
100+
{
101+
$this->requests = $requests;
102+
103+
return $this;
104+
}
105+
106+
/**
107+
* @return array
108+
*/
109+
public function getResponses()
110+
{
111+
return $this->responses;
112+
}
113+
114+
/**
115+
* @param array $responses
116+
*
117+
* @return ClientDataCollector
118+
*/
119+
public function setResponses($responses)
120+
{
121+
$this->responses = $responses;
122+
123+
return $this;
124+
}
125+
126+
/**
127+
* Get the index keys for the request and response stacks.
128+
*
129+
* @return array
130+
*/
131+
public function getStackIndexKeys()
132+
{
133+
return array_keys($this->requests);
134+
}
135+
136+
/**
137+
* @param int $idx
138+
*
139+
* @return array responses
140+
*/
141+
public function getRequstStack($idx)
142+
{
143+
return $this->requests[$idx];
144+
}
145+
146+
/**
147+
* @param int $idx
148+
*
149+
* @return array responses
150+
*/
151+
public function getResponseStack($idx)
152+
{
153+
return $this->responses[$idx];
154+
}
155+
}

Collector/DebugPlugin.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
5252
$collector->addRequest($request, $clientName, ++$depth);
5353

5454
return $next($request)->then(function (ResponseInterface $response) use ($collector, $clientName, &$depth) {
55-
$collector->addResponse($response, $clientName, --$depth);
55+
$collector->addResponse($response, $clientName, $depth--);
5656

5757

5858
return $response;
5959
}, function (Exception $exception) use ($collector, $clientName, &$depth) {
60-
$collector->addFailure($exception, $clientName, --$depth);
60+
$collector->addFailure($exception, $clientName, $depth--);
6161

6262
throw $exception;
6363
});

Collector/DebugPluginCollector.php

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function __construct(Formatter $formatter = null)
3434
*/
3535
public function addRequest(RequestInterface $request, $clientName, $depth)
3636
{
37-
$this->data[$clientName]['requests'][$depth][] = $this->formatter->formatRequest($request);
37+
$this->data[$clientName]['request'][$depth][] = $this->formatter->formatRequest($request);
3838
}
3939

4040
/**
@@ -58,7 +58,62 @@ public function addFailure(Exception $exception, $clientName, $depth)
5858
$formattedResponse = sprintf('Unexpected exception of type "%s"', get_class($exception));
5959
}
6060

61-
$this->data[$clientName]['failure'][$depth][] = $formattedResponse;
61+
$this->data[$clientName]['response'][$depth][] = $formattedResponse;
62+
$this->data[$clientName]['failure'][$depth][] = true;
63+
}
64+
65+
66+
/**
67+
* Returns the successful request-resonse pairs.
68+
*
69+
* @return array
70+
*/
71+
public function getSucessfulRequests()
72+
{
73+
$count = 0;
74+
foreach ($this->data as $client) {
75+
if (isset($client['request'])) {
76+
$count += count($client['request'][0]);
77+
}
78+
}
79+
80+
return $count;
81+
}
82+
83+
/**
84+
* Returns the failed request-resonse pairs.
85+
*
86+
* @return array
87+
*/
88+
public function getFailedRequests()
89+
{
90+
$count = 0;
91+
foreach ($this->data as $client) {
92+
if (isset($client['failure'])) {
93+
$count += count($client['failure'][0]);
94+
}
95+
}
96+
97+
return $count;
98+
}
99+
100+
/**
101+
* Returns the total number of request made.
102+
*
103+
* @return int
104+
*/
105+
public function getTotalRequests()
106+
{
107+
return $this->getSucessfulRequests() + $this->getFailedRequests();
108+
}
109+
110+
/**
111+
*
112+
* @return ClientDataCollector[]
113+
*/
114+
public function getClients()
115+
{
116+
return ClientDataCollector::createFromCollectedData($this->data);
62117
}
63118

64119
/**
@@ -74,6 +129,6 @@ public function collect(Request $request, Response $response, \Exception $except
74129
*/
75130
public function getName()
76131
{
77-
return 'httplug_debug';
132+
return 'httplug';
78133
}
79134
}

Collector/MessageJournal.php

Lines changed: 0 additions & 109 deletions
This file was deleted.

0 commit comments

Comments
 (0)