Skip to content

Commit 7c27f5d

Browse files
authored
Merge pull request #10 from MASNathan/handle-unknown-json-response
Handled unknown response objects that contain json body
2 parents 585db6c + e3f8a10 commit 7c27f5d

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
@@ -109,10 +109,15 @@ protected function updateResponse(Request $request, Response $response)
109109
$this->stopProfiling(ProfilingCollection::REQUEST_TIMER);
110110

111111
if ($this->needToUpdateResponse($response)) {
112-
$data = $response->getData(true) ?: [];
112+
$data = $this->getResponseData($response);
113+
114+
if ($data === false) {
115+
return;
116+
}
117+
113118
$data[$this->responseKey] = $this->storage->getData();
114119

115-
$response->setData($data);
120+
$this->setResponseData($response, $data);
116121
}
117122
}
118123

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

130175
/**

0 commit comments

Comments
 (0)