|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Spiral\Router; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Http\Message\ResponseFactoryInterface; |
|
8
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
|
9
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
|
10
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
11
|
|
|
use Spiral\Core\CoreInterface; |
|
12
|
|
|
use Spiral\Core\Exception\ControllerException; |
|
13
|
|
|
use Spiral\Core\ScopeInterface; |
|
14
|
|
|
use Spiral\Http\Exception\ClientException; |
|
15
|
|
|
use Spiral\Http\Exception\ClientException\BadRequestException; |
|
16
|
|
|
use Spiral\Http\Exception\ClientException\ForbiddenException; |
|
17
|
|
|
use Spiral\Http\Exception\ClientException\NotFoundException; |
|
18
|
|
|
use Spiral\Http\Exception\ClientException\UnauthorizedException; |
|
19
|
|
|
use Spiral\Http\Stream\GeneratorStream; |
|
20
|
|
|
use Spiral\Http\Traits\JsonTrait; |
|
21
|
|
|
use Spiral\Router\Exception\HandlerException; |
|
22
|
|
|
use Spiral\Telemetry\NullTracer; |
|
23
|
|
|
use Spiral\Telemetry\TracerInterface; |
|
24
|
|
|
|
|
25
|
|
|
final class CoreHandler implements RequestHandlerInterface |
|
26
|
|
|
{ |
|
27
|
|
|
use JsonTrait; |
|
28
|
|
|
|
|
29
|
|
|
private readonly TracerInterface $tracer; |
|
30
|
|
|
|
|
31
|
|
|
/** @readonly */ |
|
32
|
|
|
private ?string $controller = null; |
|
33
|
|
|
/** @readonly */ |
|
34
|
|
|
private ?string $action = null; |
|
35
|
|
|
/** @readonly */ |
|
36
|
|
|
private ?bool $verbActions = null; |
|
37
|
|
|
/** @readonly */ |
|
38
|
|
|
private ?array $parameters = null; |
|
39
|
|
|
|
|
40
|
56 |
|
public function __construct( |
|
41
|
|
|
private readonly CoreInterface $core, |
|
42
|
|
|
private readonly ScopeInterface $scope, |
|
43
|
|
|
private readonly ResponseFactoryInterface $responseFactory, |
|
44
|
|
|
?TracerInterface $tracer = null |
|
45
|
|
|
) { |
|
46
|
56 |
|
$this->tracer = $tracer ?? new NullTracer($scope); |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @mutation-free |
|
51
|
|
|
*/ |
|
52
|
54 |
|
public function withContext(string $controller, string $action, array $parameters): CoreHandler |
|
53
|
|
|
{ |
|
54
|
54 |
|
$handler = clone $this; |
|
55
|
54 |
|
$handler->controller = $controller; |
|
56
|
54 |
|
$handler->action = $action; |
|
57
|
54 |
|
$handler->parameters = $parameters; |
|
58
|
|
|
|
|
59
|
54 |
|
return $handler; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Disable or enable HTTP prefix for actions. |
|
64
|
|
|
* |
|
65
|
|
|
* @mutation-free |
|
66
|
|
|
*/ |
|
67
|
54 |
|
public function withVerbActions(bool $verbActions): CoreHandler |
|
68
|
|
|
{ |
|
69
|
54 |
|
$handler = clone $this; |
|
70
|
54 |
|
$handler->verbActions = $verbActions; |
|
71
|
|
|
|
|
72
|
54 |
|
return $handler; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @psalm-suppress UnusedVariable |
|
77
|
|
|
* @throws \Throwable |
|
78
|
|
|
*/ |
|
79
|
54 |
|
public function handle(Request $request): Response |
|
80
|
|
|
{ |
|
81
|
54 |
|
$this->checkValues(); |
|
82
|
53 |
|
$controller = $this->controller; |
|
83
|
53 |
|
$parameters = $this->parameters; |
|
84
|
|
|
|
|
85
|
53 |
|
$outputLevel = \ob_get_level(); |
|
86
|
53 |
|
\ob_start(); |
|
87
|
|
|
|
|
88
|
53 |
|
$result = null; |
|
89
|
53 |
|
$output = ''; |
|
90
|
|
|
|
|
91
|
53 |
|
$response = $this->responseFactory->createResponse(200); |
|
92
|
|
|
try { |
|
93
|
53 |
|
$action = $this->verbActions |
|
94
|
1 |
|
? \strtolower($request->getMethod()) . \ucfirst($this->action) |
|
|
|
|
|
|
95
|
52 |
|
: $this->action; |
|
96
|
|
|
|
|
97
|
|
|
// run the core withing PSR-7 Request/Response scope |
|
98
|
53 |
|
$result = $this->scope->runScope( |
|
99
|
53 |
|
[ |
|
100
|
53 |
|
Request::class => $request, |
|
101
|
53 |
|
Response::class => $response, |
|
102
|
53 |
|
], |
|
103
|
53 |
|
fn (): mixed => $this->tracer->trace( |
|
104
|
53 |
|
name: 'Controller [' . $controller . ':' . $action . ']', |
|
105
|
53 |
|
callback: fn (): mixed => $this->core->callAction( |
|
106
|
53 |
|
controller: $controller, |
|
|
|
|
|
|
107
|
53 |
|
action: $action, |
|
|
|
|
|
|
108
|
53 |
|
parameters: $parameters, |
|
|
|
|
|
|
109
|
53 |
|
), |
|
110
|
53 |
|
attributes: [ |
|
111
|
53 |
|
'route.controller' => $this->controller, |
|
112
|
53 |
|
'route.action' => $action, |
|
113
|
53 |
|
'route.parameters' => \array_keys($parameters), |
|
|
|
|
|
|
114
|
53 |
|
] |
|
115
|
53 |
|
) |
|
116
|
53 |
|
); |
|
117
|
9 |
|
} catch (ControllerException $e) { |
|
118
|
6 |
|
\ob_get_clean(); |
|
119
|
6 |
|
throw $this->mapException($e); |
|
120
|
3 |
|
} catch (\Throwable $e) { |
|
121
|
3 |
|
\ob_get_clean(); |
|
122
|
3 |
|
throw $e; |
|
123
|
|
|
} finally { |
|
124
|
53 |
|
while (\ob_get_level() > $outputLevel + 1) { |
|
125
|
3 |
|
$output = \ob_get_clean() . $output; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
44 |
|
return $this->wrapResponse( |
|
130
|
44 |
|
$response, |
|
131
|
44 |
|
$result, |
|
132
|
44 |
|
\ob_get_clean() . $output, |
|
133
|
44 |
|
); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Convert endpoint result into valid response. |
|
138
|
|
|
* |
|
139
|
|
|
* @param Response $response Initial pipeline response. |
|
140
|
|
|
* @param mixed $result Generated endpoint output. |
|
141
|
|
|
* @param string $output Buffer output. |
|
142
|
|
|
*/ |
|
143
|
44 |
|
private function wrapResponse(Response $response, mixed $result = null, string $output = ''): Response |
|
144
|
|
|
{ |
|
145
|
44 |
|
if ($result instanceof Response) { |
|
146
|
1 |
|
if ($output !== '' && $result->getBody()->isWritable()) { |
|
147
|
1 |
|
$result->getBody()->write($output); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
1 |
|
return $result; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
43 |
|
if ($result instanceof \Generator) { |
|
154
|
|
|
return $response->withBody(new GeneratorStream($result)); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
43 |
|
if (\is_array($result) || $result instanceof \JsonSerializable) { |
|
158
|
11 |
|
$response = $this->writeJson($response, $result); |
|
159
|
|
|
} else { |
|
160
|
33 |
|
$response->getBody()->write((string)$result); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
//Always glue buffered output |
|
164
|
43 |
|
$response->getBody()->write($output); |
|
165
|
|
|
|
|
166
|
43 |
|
return $response; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Converts core specific ControllerException into HTTP ClientException. |
|
171
|
|
|
*/ |
|
172
|
6 |
|
private function mapException(ControllerException $exception): ClientException |
|
173
|
|
|
{ |
|
174
|
6 |
|
return match ($exception->getCode()) { |
|
175
|
6 |
|
ControllerException::BAD_ACTION, |
|
176
|
6 |
|
ControllerException::NOT_FOUND => new NotFoundException('Not found', $exception), |
|
177
|
6 |
|
ControllerException::FORBIDDEN => new ForbiddenException('Forbidden', $exception), |
|
178
|
6 |
|
ControllerException::UNAUTHORIZED => new UnauthorizedException('Unauthorized', $exception), |
|
179
|
6 |
|
default => new BadRequestException('Bad request', $exception), |
|
180
|
6 |
|
}; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @psalm-assert !null $this->controller |
|
185
|
|
|
* @psalm-assert !null $this->action |
|
186
|
|
|
* @psalm-assert !null $this->parameters |
|
187
|
|
|
* @mutation-free |
|
188
|
|
|
*/ |
|
189
|
54 |
|
private function checkValues(): void |
|
190
|
|
|
{ |
|
191
|
54 |
|
if ($this->controller === null) { |
|
192
|
1 |
|
throw new HandlerException('Controller and action pair are not set.'); |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|