@@ -109,10 +109,15 @@ protected function updateResponse(Request $request, Response $response)
109
109
$ this ->stopProfiling (ProfilingCollection::REQUEST_TIMER );
110
110
111
111
if ($ this ->needToUpdateResponse ($ response )) {
112
- $ data = $ response ->getData (true ) ?: [];
112
+ $ data = $ this ->getResponseData ($ response );
113
+
114
+ if ($ data === false ) {
115
+ return ;
116
+ }
117
+
113
118
$ data [$ this ->responseKey ] = $ this ->storage ->getData ();
114
119
115
- $ response -> setData ( $ data );
120
+ $ this -> setResponseData ( $ response , $ data );
116
121
}
117
122
}
118
123
@@ -124,7 +129,47 @@ protected function updateResponse(Request $request, Response $response)
124
129
*/
125
130
protected function needToUpdateResponse (Response $ response )
126
131
{
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 );
128
173
}
129
174
130
175
/**
0 commit comments