Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions Collector/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,10 @@ final class Profile

/**
* @param string $plugin
* @param string $request
*/
public function __construct($plugin, $request)
public function __construct($plugin)
{
$this->plugin = $plugin;
$this->request = $request;
}

/**
Expand All @@ -57,6 +55,14 @@ public function getRequest()
return $this->request;
}

/**
* @param string $request
*/
public function setRequest($request)
{
$this->request = $request;
}

/**
* @return string
*/
Expand Down
18 changes: 16 additions & 2 deletions Collector/ProfilePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,28 @@ public function __construct(Plugin $plugin, Collector $collector, Formatter $for
*/
public function handleRequest(RequestInterface $request, callable $next, callable $first)
{
$profile = new Profile($this->pluginName, $this->formatter->formatRequest($request));
$profile = new Profile($this->pluginName);

$stack = $this->collector->getCurrentStack();
if (null !== $stack) {
$stack->addProfile($profile);
}

return $this->plugin->handleRequest($request, $next, $first)->then(function (ResponseInterface $response) use ($profile) {
// wrap the next callback to profile the plugin request changes
$wrappedNext = function (RequestInterface $request) use ($next, $profile) {
$profile->setRequest($this->formatter->formatRequest($request));

return $next($request);
};

// wrap the first callback to profile the plugin request changes
$wrappedFirst = function (RequestInterface $request) use ($first, $profile) {
$profile->setRequest($this->formatter->formatRequest($request));

return $first($request);
};

return $this->plugin->handleRequest($request, $wrappedNext, $wrappedFirst)->then(function (ResponseInterface $response) use ($profile) {
$profile->setResponse($this->formatter->formatResponse($response));

return $response;
Expand Down
15 changes: 14 additions & 1 deletion Tests/Unit/Collector/ProfilePluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ public function setUp()

$this->plugin
->method('handleRequest')
->willReturn($this->promise)
->willReturnCallback(function ($request, $next, $first) {
$next($request);

return $this->promise;
})
;

$this->formatter
Expand Down Expand Up @@ -128,6 +132,15 @@ public function testProfileIsInitialized()
$this->assertCount(1, $this->currentStack->getProfiles());
$profile = $this->currentStack->getProfiles()[0];
$this->assertEquals('http.plugin.mock', $profile->getPlugin());
}

public function testCollectRequestInformations()
{
$this->subject->handleRequest($this->request, function () {
}, function () {
});

$profile = $this->currentStack->getProfiles()[0];
$this->assertEquals('FormattedRequest', $profile->getRequest());
}

Expand Down