1 | <?php |
||
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 | public function setRouter(Router $router): Server |
||
30 | { |
||
31 | $this->router = $router; |
||
32 | |||
33 | return $this; |
||
34 | } |
||
35 | |||
36 | public function setHeaderContainer(HeaderContainer $headerContainer): Server |
||
37 | { |
||
38 | $this->headerContainer = $headerContainer; |
||
39 | |||
40 | return $this; |
||
41 | } |
||
42 | |||
43 | public function setErrorHandler(ErrorHandler $errorHandler): Server |
||
44 | { |
||
45 | $this->errorHandler = $errorHandler; |
||
46 | |||
47 | return $this; |
||
48 | } |
||
49 | |||
50 | public function run(): string |
||
51 | { |
||
52 | $response = $this->createResponse(); |
||
53 | |||
54 | return $this->handleResponse($response); |
||
55 | } |
||
56 | |||
57 | protected function getRequest(): Request |
||
58 | { |
||
59 | if (! $this->request) { |
||
60 | $this->request = ServerRequest::fromGlobals(); |
||
|
|||
61 | } |
||
62 | |||
63 | return $this->request; |
||
64 | } |
||
65 | |||
66 | protected function getCurrentPath(): string |
||
67 | { |
||
68 | $path = $this->getRequest()->getUri()->getPath(); |
||
69 | |||
70 | return $path === '' ? '/' : $path; |
||
71 | } |
||
72 | |||
73 | protected function handleDynamicRoute(): ?Response |
||
81 | |||
82 | protected function createResponse(): Response |
||
83 | { |
||
84 | if ( |
||
85 | $this->router |
||
86 | && $redirectTo = $this->router->getRedirectForUrl($this->getCurrentPath()) |
||
87 | ) { |
||
88 | return $this->createRedirectResponse($redirectTo); |
||
89 | } |
||
90 | |||
91 | try { |
||
92 | $response = $this->handleStaticRoute(); |
||
93 | |||
94 | if (! $response) { |
||
95 | $response = $this->handleDynamicRoute(); |
||
96 | } |
||
97 | } catch (StitcherException $e) { |
||
98 | $response = $this->createErrorResponse($e); |
||
99 | } |
||
100 | |||
101 | return $response ?? $this->createErrorResponse( |
||
102 | Http::notFound( |
||
103 | $this->getCurrentPath() |
||
104 | ) |
||
105 | ); |
||
106 | } |
||
107 | |||
108 | protected function createRedirectResponse(string $targetUrl): Response |
||
112 | |||
113 | protected function createErrorResponse(StitcherException $exception): Response |
||
121 | |||
122 | protected function handleResponse(Response $response): string |
||
123 | { |
||
124 | foreach ($response->getHeaders() as $name => $headers) { |
||
125 | header($name . ':'. implode(', ', $headers)); |
||
126 | } |
||
127 | |||
128 | if ($this->headerContainer) { |
||
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..