@@ -107,10 +107,15 @@ public function profileMe($name, \Closure $action)
107
107
protected function updateResponse (Request $ request , Response $ response )
108
108
{
109
109
if ($ this ->needToUpdateResponse ($ response )) {
110
- $ data = $ response ->getData (true ) ?: [];
110
+ $ data = $ this ->getResponseData ($ response );
111
+
112
+ if ($ data === false ) {
113
+ return ;
114
+ }
115
+
111
116
$ data [$ this ->responseKey ] = $ this ->storage ->getData ();
112
117
113
- $ response -> setData ( $ data );
118
+ $ this -> setResponseData ( $ response , $ data );
114
119
}
115
120
}
116
121
@@ -122,7 +127,47 @@ protected function updateResponse(Request $request, Response $response)
122
127
*/
123
128
protected function needToUpdateResponse (Response $ response )
124
129
{
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 );
126
171
}
127
172
128
173
/**
0 commit comments