1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Stitcher\Application; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Psr7\Request; |
6
|
|
|
use GuzzleHttp\Psr7\Response; |
7
|
|
|
use GuzzleHttp\Psr7\ServerRequest; |
8
|
|
|
use Pageon\Http\HeaderContainer; |
9
|
|
|
use Stitcher\Exception\ErrorHandler; |
10
|
|
|
use Stitcher\Exception\Http; |
11
|
|
|
use Stitcher\Exception\StitcherException; |
12
|
|
|
|
13
|
|
|
abstract class Server |
14
|
|
|
{ |
15
|
|
|
/** @var Router */ |
16
|
|
|
protected $router; |
17
|
|
|
|
18
|
|
|
/** @var Request */ |
19
|
|
|
protected $request; |
20
|
|
|
|
21
|
|
|
/** @var HeaderContainer */ |
22
|
|
|
protected $headerContainer; |
23
|
|
|
|
24
|
|
|
/** @var \Stitcher\Exception\ErrorHandler */ |
25
|
|
|
protected $errorHandler; |
26
|
|
|
|
27
|
|
|
abstract protected function handleStaticRoute(): ?Response; |
28
|
|
|
|
29
|
3 |
|
public function setRouter(Router $router): Server |
30
|
|
|
{ |
31
|
3 |
|
$this->router = $router; |
32
|
|
|
|
33
|
3 |
|
return $this; |
34
|
|
|
} |
35
|
|
|
|
36
|
3 |
|
public function setHeaderContainer(HeaderContainer $headerContainer): Server |
37
|
|
|
{ |
38
|
3 |
|
$this->headerContainer = $headerContainer; |
39
|
|
|
|
40
|
3 |
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
3 |
|
public function setErrorHandler(ErrorHandler $errorHandler): Server |
44
|
|
|
{ |
45
|
3 |
|
$this->errorHandler = $errorHandler; |
46
|
|
|
|
47
|
3 |
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
4 |
|
public function run(): string |
51
|
|
|
{ |
52
|
4 |
|
$response = $this->createResponse(); |
53
|
|
|
|
54
|
4 |
|
return $this->handleResponse($response); |
55
|
|
|
} |
56
|
|
|
|
57
|
2 |
|
protected function getRequest(): Request |
58
|
|
|
{ |
59
|
2 |
|
if (! $this->request) { |
60
|
2 |
|
$this->request = ServerRequest::fromGlobals(); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
2 |
|
return $this->request; |
64
|
|
|
} |
65
|
|
|
|
66
|
2 |
|
protected function getCurrentPath(): string |
67
|
|
|
{ |
68
|
2 |
|
$path = $this->getRequest()->getUri()->getPath(); |
69
|
|
|
|
70
|
2 |
|
return $path === '' ? '/' : $path; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
protected function handleDynamicRoute(): ?Response |
74
|
|
|
{ |
75
|
|
|
if (! $this->router) { |
76
|
|
|
return null; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $this->router->dispatch($this->getRequest()); |
80
|
|
|
} |
81
|
|
|
|
82
|
4 |
|
protected function createResponse(): Response |
83
|
|
|
{ |
84
|
|
|
if ( |
85
|
4 |
|
$this->router |
86
|
4 |
|
&& $redirectTo = $this->router->getRedirectForUrl($this->getCurrentPath()) |
87
|
|
|
) { |
88
|
|
|
return $this->createRedirectResponse($redirectTo); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
try { |
92
|
4 |
|
$response = $this->handleStaticRoute(); |
93
|
|
|
|
94
|
4 |
|
if (! $response) { |
95
|
4 |
|
$response = $this->handleDynamicRoute(); |
96
|
|
|
} |
97
|
|
|
} catch (StitcherException $e) { |
98
|
|
|
$response = $this->createErrorResponse($e); |
99
|
|
|
} |
100
|
|
|
|
101
|
4 |
|
return $response ?? $this->createErrorResponse( |
102
|
|
|
Http::notFound( |
103
|
4 |
|
$this->getCurrentPath() |
104
|
|
|
) |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
protected function createRedirectResponse(string $targetUrl): Response |
109
|
|
|
{ |
110
|
|
|
return new Response(301, ["Location: {$targetUrl}"]); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
protected function createErrorResponse(StitcherException $exception): Response |
114
|
|
|
{ |
115
|
|
|
$statusCode = $exception instanceof Http ? $exception->statusCode() : 500; |
116
|
|
|
|
117
|
|
|
$responseBody = $this->errorHandler->handle($statusCode, $exception); |
118
|
|
|
|
119
|
|
|
return new Response($statusCode, [], $responseBody); |
120
|
|
|
} |
121
|
|
|
|
122
|
4 |
|
protected function handleResponse(Response $response): string |
123
|
|
|
{ |
124
|
4 |
|
foreach ($response->getHeaders() as $headers) { |
125
|
|
|
header(implode(', ', $headers)); |
126
|
|
|
} |
127
|
|
|
|
128
|
4 |
|
if ($this->headerContainer) { |
129
|
|
|
foreach ($this->headerContainer as $header) { |
130
|
|
|
header((string) $header); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
4 |
|
http_response_code($response->getStatusCode()); |
135
|
|
|
|
136
|
4 |
|
return $response->getBody()->getContents(); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..