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 Parsedown; |
9
|
|
|
use Stitcher\Exception\Http; |
10
|
|
|
use Stitcher\Exception\StitcherException; |
11
|
|
|
|
12
|
|
|
abstract class Server |
13
|
|
|
{ |
14
|
|
|
/** @var Router */ |
15
|
|
|
protected $router; |
16
|
|
|
|
17
|
|
|
/** @var Request */ |
18
|
|
|
protected $request; |
19
|
|
|
|
20
|
|
|
/** @var Parsedown */ |
21
|
|
|
protected $markdownParser; |
22
|
|
|
|
23
|
3 |
|
public function setRouter(Router $router): Server |
24
|
|
|
{ |
25
|
3 |
|
$this->router = $router; |
26
|
|
|
|
27
|
3 |
|
return $this; |
28
|
|
|
} |
29
|
|
|
|
30
|
3 |
|
public function setMarkdownParser(Parsedown $markdownParser): Server |
31
|
|
|
{ |
32
|
3 |
|
$this->markdownParser = $markdownParser; |
33
|
|
|
|
34
|
3 |
|
return $this; |
35
|
|
|
} |
36
|
|
|
|
37
|
4 |
|
public function run(): string |
38
|
|
|
{ |
39
|
4 |
|
$response = $this->createResponse(); |
40
|
|
|
|
41
|
4 |
|
return $this->handleResponse($response); |
42
|
|
|
} |
43
|
|
|
|
44
|
2 |
|
protected function getRequest(): Request |
45
|
|
|
{ |
46
|
2 |
|
if (! $this->request) { |
47
|
2 |
|
$this->request = ServerRequest::fromGlobals(); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
2 |
|
return $this->request; |
51
|
|
|
} |
52
|
|
|
|
53
|
2 |
|
protected function getCurrentPath(): string |
54
|
|
|
{ |
55
|
2 |
|
$path = $this->getRequest()->getUri()->getPath(); |
56
|
|
|
|
57
|
2 |
|
return $path === '' ? '/' : $path; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
abstract protected function handleStaticRoute(): ?Response; |
61
|
|
|
|
62
|
|
|
protected function handleDynamicRoute(): ?Response |
63
|
|
|
{ |
64
|
|
|
if (! $this->router) { |
65
|
|
|
return null; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $this->router->dispatch($this->getRequest()); |
69
|
|
|
} |
70
|
|
|
|
71
|
4 |
|
protected function createResponse(): Response |
72
|
|
|
{ |
73
|
|
|
try { |
74
|
4 |
|
$response = $this->handleStaticRoute(); |
75
|
|
|
|
76
|
4 |
|
if (! $response) { |
77
|
4 |
|
$response = $this->handleDynamicRoute(); |
78
|
|
|
} |
79
|
|
|
} catch (StitcherException $e) { |
80
|
|
|
$response = $this->responseFromException($e); |
81
|
|
|
} |
82
|
|
|
|
83
|
4 |
|
return $response ?? $this->responseFromException( |
84
|
|
|
Http::notFound( |
85
|
4 |
|
$this->getCurrentPath() |
86
|
|
|
) |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
protected function responseFromException(StitcherException $e): Response |
91
|
|
|
{ |
92
|
|
|
$statusCode = $e instanceof Http ? $e->statusCode() : 500; |
93
|
|
|
|
94
|
|
|
$responseBody = file_get_contents(__DIR__ . '/../../static/exception.html'); |
95
|
|
|
|
96
|
|
|
$responseBody = str_replace( |
97
|
|
|
'{{ title }}', |
98
|
|
|
$this->markdownParser->parse($e->title()), |
99
|
|
|
$responseBody |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
$responseBody = str_replace( |
103
|
|
|
'{{ body }}', |
104
|
|
|
$this->markdownParser->parse($e->body()), |
105
|
|
|
$responseBody |
106
|
|
|
); |
107
|
|
|
|
108
|
|
|
return new Response($statusCode, [], $responseBody); |
109
|
|
|
} |
110
|
|
|
|
111
|
4 |
|
protected function handleResponse(Response $response): string |
112
|
|
|
{ |
113
|
4 |
|
http_response_code($response->getStatusCode()); |
114
|
|
|
|
115
|
4 |
|
return $response->getBody()->getContents(); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
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..