Skip to content

Commit 8fa554d

Browse files
committed
fix the cache plugin case
1 parent bdce237 commit 8fa554d

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

Collector/ProfilePlugin.php

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,50 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
7777
return $first($request);
7878
};
7979

80-
return $this->plugin->handleRequest($request, $wrappedNext, $wrappedFirst)->then(function (ResponseInterface $response) use ($profile) {
80+
return $this->plugin->handleRequest($request, $wrappedNext, $wrappedFirst)->then(function (ResponseInterface $response) use ($profile, $request, $stack) {
8181
$profile->setResponse($this->formatter->formatResponse($response));
82+
$this->collectRequestInformation($request, $stack);
8283

8384
return $response;
84-
}, function (Exception $exception) use ($profile) {
85+
}, function (Exception $exception) use ($profile, $request, $stack) {
8586
$profile->setFailed(true);
8687
$profile->setResponse($this->formatter->formatException($exception));
88+
$this->collectRequestInformation($request, $stack);
8789

8890
throw $exception;
8991
});
9092
}
93+
94+
/**
95+
* Collect request information when not already done by the HTTP client. This happens when using the CachePlugin
96+
* and the cache is hit without re-validation.
97+
*
98+
* @param RequestInterface $request
99+
* @param Stack|null $stack
100+
*/
101+
private function collectRequestInformation(RequestInterface $request, Stack $stack = null)
102+
{
103+
if (null === $stack) {
104+
return;
105+
}
106+
107+
if (empty($stack->getRequestTarget())) {
108+
$stack->setRequestTarget($request->getRequestTarget());
109+
}
110+
if (empty($stack->getRequestMethod())) {
111+
$stack->setRequestMethod($request->getMethod());
112+
}
113+
if (empty($stack->getRequestScheme())) {
114+
$stack->setRequestScheme($request->getUri()->getScheme());
115+
}
116+
if (empty($stack->getRequestHost())) {
117+
$stack->setRequestHost($request->getUri()->getHost());
118+
}
119+
if (empty($stack->getClientRequest())) {
120+
$stack->setClientRequest($this->formatter->formatRequest($request));
121+
}
122+
if (empty($stack->getCurlCommand())) {
123+
$stack->setCurlCommand($this->formatter->formatAsCurlCommand($request));
124+
}
125+
}
91126
}

0 commit comments

Comments
 (0)