Skip to content

Commit ff80f90

Browse files
authored
Merge pull request #169 from fbourigault/fix-collect-request
Collect the request after the plugin execution
2 parents 0faaf3f + e1c2bdb commit ff80f90

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

Collector/Profile.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,10 @@ final class Profile
3333

3434
/**
3535
* @param string $plugin
36-
* @param string $request
3736
*/
38-
public function __construct($plugin, $request)
37+
public function __construct($plugin)
3938
{
4039
$this->plugin = $plugin;
41-
$this->request = $request;
4240
}
4341

4442
/**
@@ -57,6 +55,14 @@ public function getRequest()
5755
return $this->request;
5856
}
5957

58+
/**
59+
* @param string $request
60+
*/
61+
public function setRequest($request)
62+
{
63+
$this->request = $request;
64+
}
65+
6066
/**
6167
* @return string
6268
*/

Collector/ProfilePlugin.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,28 @@ public function __construct(Plugin $plugin, Collector $collector, Formatter $for
5656
*/
5757
public function handleRequest(RequestInterface $request, callable $next, callable $first)
5858
{
59-
$profile = new Profile($this->pluginName, $this->formatter->formatRequest($request));
59+
$profile = new Profile($this->pluginName);
6060

6161
$stack = $this->collector->getCurrentStack();
6262
if (null !== $stack) {
6363
$stack->addProfile($profile);
6464
}
6565

66-
return $this->plugin->handleRequest($request, $next, $first)->then(function (ResponseInterface $response) use ($profile) {
66+
// wrap the next callback to profile the plugin request changes
67+
$wrappedNext = function (RequestInterface $request) use ($next, $profile) {
68+
$profile->setRequest($this->formatter->formatRequest($request));
69+
70+
return $next($request);
71+
};
72+
73+
// wrap the first callback to profile the plugin request changes
74+
$wrappedFirst = function (RequestInterface $request) use ($first, $profile) {
75+
$profile->setRequest($this->formatter->formatRequest($request));
76+
77+
return $first($request);
78+
};
79+
80+
return $this->plugin->handleRequest($request, $wrappedNext, $wrappedFirst)->then(function (ResponseInterface $response) use ($profile) {
6781
$profile->setResponse($this->formatter->formatResponse($response));
6882

6983
return $response;

Tests/Unit/Collector/ProfilePluginTest.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,11 @@ public function setUp()
7777

7878
$this->plugin
7979
->method('handleRequest')
80-
->willReturn($this->promise)
80+
->willReturnCallback(function ($request, $next, $first) {
81+
$next($request);
82+
83+
return $this->promise;
84+
})
8185
;
8286

8387
$this->formatter
@@ -128,6 +132,15 @@ public function testProfileIsInitialized()
128132
$this->assertCount(1, $this->currentStack->getProfiles());
129133
$profile = $this->currentStack->getProfiles()[0];
130134
$this->assertEquals('http.plugin.mock', $profile->getPlugin());
135+
}
136+
137+
public function testCollectRequestInformations()
138+
{
139+
$this->subject->handleRequest($this->request, function () {
140+
}, function () {
141+
});
142+
143+
$profile = $this->currentStack->getProfiles()[0];
131144
$this->assertEquals('FormattedRequest', $profile->getRequest());
132145
}
133146

0 commit comments

Comments
 (0)