1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace League\Route\Strategy; |
4
|
|
|
|
5
|
|
|
<<<<<<< Updated upstream |
|
|
|
|
6
|
|
|
use \Exception; |
7
|
|
|
use League\Route\Http\Exception\MethodNotAllowedException; |
8
|
|
|
use League\Route\Http\Exception\NotFoundException; |
9
|
|
|
======= |
10
|
|
|
use ArrayObject; |
11
|
|
|
use Exception; |
12
|
|
|
>>>>>>> Stashed changes |
13
|
|
|
use League\Route\Http\Exception as HttpException; |
14
|
|
|
use League\Route\Route; |
15
|
|
|
use Psr\Http\Message\ResponseInterface; |
16
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
17
|
|
|
use RuntimeException; |
18
|
|
|
|
19
|
9 |
|
class JsonStrategy implements StrategyInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
9 |
|
* {@inheritdoc} |
23
|
|
|
*/ |
24
|
3 |
|
public function getCallable(Route $route, array $vars) |
25
|
3 |
|
{ |
26
|
|
|
return function (ServerRequestInterface $request, ResponseInterface $response, callable $next) use ($route, $vars) { |
27
|
3 |
|
$return = call_user_func_array($route->getCallable(), [$request, $response, $vars]); |
28
|
|
|
|
29
|
|
|
if (! $return instanceof ResponseInterface) { |
30
|
|
|
throw new RuntimeException( |
31
|
|
|
'Route callables must return an instance of (Psr\Http\Message\ResponseInterface)' |
32
|
|
|
); |
33
|
|
|
} |
34
|
9 |
|
|
35
|
|
|
$response = $return; |
36
|
|
|
$response = $next($request, $response); |
37
|
|
|
|
38
|
|
|
return $response->withAddedHeader('content-type', 'application/json'); |
39
|
|
|
}; |
40
|
3 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
3 |
|
* {@inheritdoc} |
44
|
3 |
|
*/ |
45
|
|
|
public function getNotFoundDecorator(NotFoundException $exception) |
46
|
|
|
{ |
47
|
|
|
return function (ServerRequestInterface $request, ResponseInterface $response) use ($exception) { |
48
|
|
|
return $exception->buildJsonResponse($response); |
49
|
|
|
}; |
50
|
3 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
3 |
|
* {@inheritdoc} |
54
|
3 |
|
*/ |
55
|
|
|
public function getMethodNotAllowedDecorator(MethodNotAllowedException $exception) |
56
|
|
|
{ |
57
|
|
|
return function (ServerRequestInterface $request, ResponseInterface $response) use ($exception) { |
58
|
|
|
return $exception->buildJsonResponse($response); |
59
|
|
|
}; |
60
|
|
|
} |
61
|
|
|
|
62
|
6 |
|
/** |
63
|
6 |
|
* {@inheritdoc} |
64
|
3 |
|
*/ |
65
|
|
|
public function getExceptionDecorator(Exception $exception) |
66
|
|
|
{ |
67
|
3 |
|
return function (ServerRequestInterface $request, ResponseInterface $response) use ($exception) { |
68
|
3 |
|
if ($exception instanceof HttpException) { |
69
|
3 |
|
return $exception->buildJsonResponse($response); |
70
|
3 |
|
} |
71
|
|
|
<<<<<<< Updated upstream |
72
|
3 |
|
======= |
73
|
3 |
|
} catch (HttpException $e) { |
74
|
6 |
|
return $e->buildJsonResponse($this->getResponse()); |
75
|
|
|
} catch (Exception $e) { |
76
|
|
|
$response = $this->getResponse(); |
77
|
|
|
|
78
|
|
|
$response->getBody()->write(json_encode([ |
79
|
|
|
'status_code' => 500, |
80
|
|
|
'reason_phrase' => $e->getMessage() |
81
|
|
|
])); |
82
|
|
|
|
83
|
|
|
return $response->withAddedHeader('content-type', 'application/json')->withStatusCode(500); |
84
|
|
|
} |
85
|
|
|
>>>>>>> Stashed changes |
86
|
|
|
|
87
|
|
|
$response->getBody()->write(json_encode([ |
88
|
|
|
'status_code' => 500, |
89
|
|
|
'reason_phrase' => $exception->getMessage() |
90
|
|
|
])); |
91
|
|
|
|
92
|
|
|
$response = $response->withAddedHeader('content-type', 'application/json'); |
93
|
|
|
return $response->withStatus(500, $exception->getMessage()); |
94
|
|
|
}; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|