1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace League\Route\Strategy; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use League\Route\{ContainerAwareInterface, ContainerAwareTrait}; |
7
|
|
|
use League\Route\Http\Exception as HttpException; |
8
|
|
|
use League\Route\Http\Exception\{MethodNotAllowedException, NotFoundException}; |
9
|
|
|
use League\Route\Route; |
10
|
|
|
use Psr\Http\Message\{ResponseFactoryInterface, ResponseInterface, ServerRequestInterface}; |
11
|
|
|
use Psr\Http\Server\{MiddlewareInterface, RequestHandlerInterface}; |
12
|
|
|
|
13
|
|
|
class JsonStrategy implements ContainerAwareInterface, StrategyInterface |
14
|
|
|
{ |
15
|
|
|
use ContainerAwareTrait; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var \Psr\Http\Message\ResponseFactoryInterface |
19
|
|
|
*/ |
20
|
|
|
protected $responseFactory; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Construct. |
24
|
|
|
* |
25
|
|
|
* @param \Psr\Http\Message\ResponseFactoryInterface $responseFactory |
26
|
|
|
*/ |
27
|
22 |
|
public function __construct(ResponseFactoryInterface $responseFactory) |
28
|
|
|
{ |
29
|
22 |
|
$this->responseFactory = $responseFactory; |
30
|
22 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
8 |
|
public function invokeRouteCallable(Route $route, ServerRequestInterface $request) : ResponseInterface |
36
|
|
|
{ |
37
|
8 |
|
$response = call_user_func_array($route->getCallable($this->getContainer()), [$request, $route->getVars()]); |
38
|
|
|
|
39
|
4 |
|
if (is_array($response)) { |
40
|
2 |
|
$body = json_encode($response); |
41
|
2 |
|
$response = $this->responseFactory->createResponse(); |
42
|
2 |
|
$response = $response->withStatus(200); |
43
|
2 |
|
$response->getBody()->write($body); |
44
|
|
|
} |
45
|
|
|
|
46
|
4 |
|
if ($response instanceof ResponseInterface && ! $response->hasHeader('content-type')) { |
47
|
4 |
|
$response->withAddedHeader('content-type', 'application/json'); |
48
|
|
|
} |
49
|
|
|
|
50
|
4 |
|
return $response; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
4 |
|
public function getNotFoundDecorator(NotFoundException $exception) : MiddlewareInterface |
57
|
|
|
{ |
58
|
4 |
|
return $this->buildJsonResponseMiddleware($exception); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
4 |
|
public function getMethodNotAllowedDecorator(MethodNotAllowedException $exception) : MiddlewareInterface |
65
|
|
|
{ |
66
|
4 |
|
return $this->buildJsonResponseMiddleware($exception); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Return a middleware that simply throws and exception. |
71
|
|
|
* |
72
|
|
|
* @param \Exception $exception |
73
|
|
|
* |
74
|
|
|
* @return \Psr\Http\Server\MiddlewareInterface |
75
|
|
|
*/ |
76
|
8 |
|
protected function buildJsonResponseMiddleware(HttpException $exception) : MiddlewareInterface |
77
|
|
|
{ |
78
|
|
|
return new class($this->responseFactory->createResponse(), $exception) implements MiddlewareInterface |
79
|
|
|
{ |
80
|
|
|
protected $response; |
81
|
|
|
protected $exception; |
82
|
|
|
|
83
|
|
|
public function __construct(ResponseInterface $response, HttpException $exception) |
84
|
|
|
{ |
85
|
8 |
|
$this->response = $response; |
86
|
8 |
|
$this->exception = $exception; |
87
|
8 |
|
} |
88
|
|
|
|
89
|
|
|
public function process( |
90
|
|
|
ServerRequestInterface $request, |
91
|
|
|
RequestHandlerInterface $requestHandler |
92
|
|
|
) : ResponseInterface { |
93
|
8 |
|
return $this->exception->buildJsonResponse($this->response); |
94
|
|
|
} |
95
|
|
|
}; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
|
|
public function getExceptionHandler() : MiddlewareInterface |
102
|
|
|
{ |
103
|
|
|
return new class($this->responseFactory->createResponse()) implements MiddlewareInterface |
104
|
|
|
{ |
105
|
|
|
protected $response; |
106
|
|
|
|
107
|
8 |
|
public function __construct(ResponseInterface $response) |
108
|
|
|
{ |
109
|
8 |
|
$this->response = $response; |
110
|
8 |
|
} |
111
|
|
|
|
112
|
8 |
|
public function process( |
113
|
|
|
ServerRequestInterface $request, |
114
|
|
|
RequestHandlerInterface $requestHandler |
115
|
|
|
) : ResponseInterface { |
116
|
|
|
try { |
117
|
8 |
|
return $requestHandler->handle($request); |
118
|
8 |
|
} catch (Exception $exception) { |
119
|
8 |
|
$response = $this->response; |
120
|
|
|
|
121
|
8 |
|
if ($exception instanceof HttpException) { |
122
|
4 |
|
return $exception->buildJsonResponse($response); |
123
|
|
|
} |
124
|
|
|
|
125
|
4 |
|
$response->getBody()->write(json_encode([ |
126
|
4 |
|
'status_code' => 500, |
127
|
4 |
|
'reason_phrase' => $exception->getMessage() |
128
|
|
|
])); |
129
|
|
|
|
130
|
4 |
|
$response = $response->withAddedHeader('content-type', 'application/json'); |
131
|
4 |
|
return $response->withStatus(500, strtok($exception->getMessage(), "\n")); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
}; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|