|
15 | 15 | namespace MaplePHP\Emitron; |
16 | 16 |
|
17 | 17 | use MaplePHP\Container\Reflection; |
| 18 | +use MaplePHP\Emitron\Contracts\AppInterface; |
18 | 19 | use MaplePHP\Emitron\Contracts\DispatchConfigInterface; |
19 | 20 | use MaplePHP\Emitron\Contracts\EmitterInterface; |
20 | 21 | use MaplePHP\Emitron\Contracts\KernelInterface; |
21 | 22 | use MaplePHP\Emitron\Emitters\CliEmitter; |
22 | 23 | use MaplePHP\Emitron\Emitters\HttpEmitter; |
| 24 | +use MaplePHP\Http\Interfaces\PathInterface; |
23 | 25 | use MaplePHP\Http\ResponseFactory; |
24 | 26 | use MaplePHP\Http\Stream; |
25 | 27 | use Psr\Container\ContainerInterface; |
26 | 28 | use Psr\Http\Message\ResponseInterface; |
27 | 29 | use Psr\Http\Message\ServerRequestInterface; |
28 | 30 | use Psr\Http\Message\StreamInterface; |
| 31 | +use Psr\Http\Server\RequestHandlerInterface; |
29 | 32 |
|
30 | 33 | abstract class AbstractKernel implements KernelInterface |
31 | 34 | { |
@@ -118,24 +121,41 @@ public function getDispatchConfig(): DispatchConfigInterface |
118 | 121 | * |
119 | 122 | * @param ServerRequestInterface $request |
120 | 123 | * @param StreamInterface $stream |
| 124 | + * @param RequestHandlerInterface $finalHandler |
121 | 125 | * @param array $middlewares |
122 | 126 | * @return ResponseInterface |
123 | 127 | * @throws \ReflectionException |
124 | 128 | */ |
125 | 129 | protected function initRequestHandler( |
126 | 130 | ServerRequestInterface $request, |
127 | 131 | StreamInterface $stream, |
| 132 | + PathInterface $path, |
| 133 | + RequestHandlerInterface $finalHandler, |
128 | 134 | array $middlewares = [] |
129 | | - ) : ResponseInterface { |
130 | | - $factory = new ResponseFactory($stream); |
| 135 | + ): ResponseInterface { |
| 136 | + |
131 | 137 | $this->bindInterfaces([ |
132 | | - "ContainerInterface" => $this->container, "RequestInterface" => $request, |
133 | | - "ServerRequestInterface" => $request, "StreamInterface" => $stream, |
| 138 | + "ContainerInterface" => $this->container, |
| 139 | + "RequestInterface" => $request, |
| 140 | + "ServerRequestInterface" => $request, |
| 141 | + "StreamInterface" => $stream, |
| 142 | + "PathInterface" => $path |
134 | 143 | ]); |
| 144 | + |
135 | 145 | $middlewares = array_merge($this->userMiddlewares, $middlewares); |
136 | | - $handler = new RequestHandler($middlewares, $factory); |
137 | | - $response = $handler->handle($request); |
138 | | - $this->bindInterfaces(["ResponseInterface" => $response]); |
| 146 | + $handler = new RequestHandler($middlewares, $finalHandler); |
| 147 | + $app = $this->container->has("app") ? $this->container->get("app") : null; |
| 148 | + |
| 149 | + ob_start(); |
| 150 | + $response = $handler->handle($request); |
| 151 | + $output = ob_get_clean(); |
| 152 | + |
| 153 | + if((string)$output !== "" && ($app instanceof AppInterface && !$app->isProd())) { |
| 154 | + throw new \RuntimeException( |
| 155 | + 'Unexpected output detected during request dispatch. Controllers must write to the response body instead of using echo.' |
| 156 | + ); |
| 157 | + } |
| 158 | + |
139 | 159 | return $response; |
140 | 160 | } |
141 | 161 |
|
|
0 commit comments