|
2 | 2 |
|
3 | 3 | namespace DreamFactory\Core\Utility; |
4 | 4 |
|
5 | | -use DreamFactory\Core\Components\ExceptionResponse; |
6 | | -use DreamFactory\Core\Exceptions\BadRequestException; |
7 | | -use DreamFactory\Core\Exceptions\RestException; |
8 | 5 | use DreamFactory\Core\Components\DfResponse; |
| 6 | +use DreamFactory\Core\Components\ExceptionResponse; |
9 | 7 | use DreamFactory\Core\Contracts\HttpStatusCodeInterface; |
10 | | -use DreamFactory\Core\Enums\HttpStatusCodes; |
| 8 | +use DreamFactory\Core\Contracts\ServiceRequestInterface; |
11 | 9 | use DreamFactory\Core\Contracts\ServiceResponseInterface; |
12 | 10 | use DreamFactory\Core\Enums\DataFormats; |
| 11 | +use DreamFactory\Core\Enums\HttpStatusCodes; |
13 | 12 | use DreamFactory\Core\Exceptions\DfException; |
| 13 | +use DreamFactory\Core\Exceptions\RestException; |
14 | 14 |
|
15 | 15 | /** |
16 | 16 | * Class ResponseFactory |
@@ -52,24 +52,21 @@ public static function createExceptionFromResponse(ServiceResponseInterface $res |
52 | 52 | } |
53 | 53 |
|
54 | 54 | /** |
55 | | - * @param \DreamFactory\Core\Contracts\ServiceResponseInterface $response |
56 | | - * @param null $accepts |
57 | | - * @param null $asFile |
58 | | - * @param string $resource |
| 55 | + * @param ServiceResponseInterface $response |
| 56 | + * @param ServiceRequestInterface|null $request |
| 57 | + * @param null $asFile |
| 58 | + * @param string $resource |
59 | 59 | * |
60 | 60 | * @return \Symfony\Component\HttpFoundation\Response |
61 | | - * @throws \DreamFactory\Core\Exceptions\BadRequestException |
62 | 61 | * @throws \DreamFactory\Core\Exceptions\NotImplementedException |
63 | 62 | */ |
64 | 63 | public static function sendResponse( |
65 | 64 | ServiceResponseInterface $response, |
66 | | - $accepts = null, |
| 65 | + ServiceRequestInterface $request = null, |
67 | 66 | $asFile = null, |
68 | 67 | $resource = 'resource' |
69 | 68 | ) { |
70 | | - if (empty($accepts)) { |
71 | | - $accepts = static::getAcceptedTypes(); |
72 | | - } |
| 69 | + $accepts = static::getAcceptedTypes($request); |
73 | 70 |
|
74 | 71 | if (empty($asFile)) { |
75 | 72 | $asFile = \Request::input('file'); |
@@ -200,7 +197,6 @@ public static function sendResponse( |
200 | 197 | * @param ServiceResponseInterface $response |
201 | 198 | * |
202 | 199 | * @return array|mixed|string |
203 | | - * @throws BadRequestException |
204 | 200 | */ |
205 | 201 | public static function sendScriptResponse(ServiceResponseInterface $response) |
206 | 202 | { |
@@ -234,28 +230,31 @@ public static function sendScriptResponse(ServiceResponseInterface $response) |
234 | 230 | } |
235 | 231 |
|
236 | 232 | /** |
237 | | - * @param \Exception $e |
238 | | - * @param \Illuminate\Http\Request $request |
| 233 | + * @param \Exception $e |
| 234 | + * @param ServiceRequestInterface $request |
239 | 235 | * |
240 | 236 | * @return array|mixed|string |
| 237 | + * @throws \DreamFactory\Core\Exceptions\NotImplementedException |
241 | 238 | */ |
242 | | - public static function sendException( |
243 | | - \Exception $e, |
244 | | - /** @noinspection PhpUnusedParameterInspection */ |
245 | | - $request = null |
246 | | - ) { |
| 239 | + public static function sendException(\Exception $e, ServiceRequestInterface $request = null) |
| 240 | + { |
247 | 241 | $response = static::exceptionToServiceResponse($e); |
248 | 242 |
|
249 | | - return ResponseFactory::sendResponse($response); |
| 243 | + return ResponseFactory::sendResponse($response, $request); |
250 | 244 | } |
251 | 245 |
|
252 | 246 | /** |
253 | 247 | * |
| 248 | + * @param ServiceRequestInterface|null $request |
254 | 249 | * @return array |
255 | 250 | */ |
256 | | - public static function getAcceptedTypes() |
| 251 | + public static function getAcceptedTypes(ServiceRequestInterface $request = null) |
257 | 252 | { |
258 | | - $accepts = \Request::query('accept', \Request::header('ACCEPT')); |
| 253 | + if ($request) { |
| 254 | + $accepts = $request->getParameter('accept', $request->getHeader('accept')); |
| 255 | + } else { |
| 256 | + $accepts = \Request::query('accept', \Request::header('accept')); |
| 257 | + } |
259 | 258 | $accepts = array_map('trim', explode(',', $accepts)); |
260 | 259 |
|
261 | 260 | return $accepts; |
|
0 commit comments