Skip to content

Commit e3f8a10

Browse files
committed
Handled unknown response objects that contain json body
1 parent 0eea269 commit e3f8a10

File tree

1 file changed

+48
-3
lines changed

1 file changed

+48
-3
lines changed

src/Debugger.php

+48-3
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,15 @@ public function profileMe($name, \Closure $action)
107107
protected function updateResponse(Request $request, Response $response)
108108
{
109109
if ($this->needToUpdateResponse($response)) {
110-
$data = $response->getData(true) ?: [];
110+
$data = $this->getResponseData($response);
111+
112+
if ($data === false) {
113+
return;
114+
}
115+
111116
$data[$this->responseKey] = $this->storage->getData();
112117

113-
$response->setData($data);
118+
$this->setResponseData($response, $data);
114119
}
115120
}
116121

@@ -122,7 +127,47 @@ protected function updateResponse(Request $request, Response $response)
122127
*/
123128
protected function needToUpdateResponse(Response $response)
124129
{
125-
return $response instanceof JsonResponse && ! $this->storage->isEmpty();
130+
$isJsonResponse = $response instanceof JsonResponse || $response->headers->contains('content-type',
131+
'application/json');
132+
133+
return $isJsonResponse && !$this->storage->isEmpty();
134+
}
135+
136+
/**
137+
* Fetches the contents of the response and parses them to an assoc array
138+
*
139+
* @param Response $response
140+
* @return array|bool
141+
*/
142+
protected function getResponseData(Response $response)
143+
{
144+
if ($response instanceof JsonResponse) {
145+
/** @var $response JsonResponse */
146+
return $response->getData(true) ?: [];
147+
}
148+
149+
$content = $response->getContent();
150+
151+
return json_decode($content, true) ?: false;
152+
}
153+
154+
/**
155+
* Updates the response content
156+
*
157+
* @param Response $response
158+
* @param array $data
159+
* @return JsonResponse|Response
160+
*/
161+
protected function setResponseData(Response $response, array $data)
162+
{
163+
if ($response instanceof JsonResponse) {
164+
/** @var $response JsonResponse */
165+
return $response->setData($data);
166+
}
167+
168+
$content = json_encode($data, JsonResponse::DEFAULT_ENCODING_OPTIONS);
169+
170+
return $response->setContent($content);
126171
}
127172

128173
/**

0 commit comments

Comments
 (0)