1515namespace MaplePHP \Emitron ;
1616
1717use MaplePHP \Container \Reflection ;
18+ use MaplePHP \Emitron \Contracts \AppInterface ;
1819use MaplePHP \Emitron \Contracts \DispatchConfigInterface ;
1920use MaplePHP \Emitron \Contracts \EmitterInterface ;
2021use MaplePHP \Emitron \Contracts \KernelInterface ;
2122use MaplePHP \Emitron \Emitters \CliEmitter ;
2223use MaplePHP \Emitron \Emitters \HttpEmitter ;
24+ use MaplePHP \Http \Interfaces \PathInterface ;
2325use MaplePHP \Http \ResponseFactory ;
2426use MaplePHP \Http \Stream ;
2527use Psr \Container \ContainerInterface ;
@@ -127,6 +129,7 @@ public function getDispatchConfig(): DispatchConfigInterface
127129 protected function initRequestHandler (
128130 ServerRequestInterface $ request ,
129131 StreamInterface $ stream ,
132+ PathInterface $ path ,
130133 RequestHandlerInterface $ finalHandler ,
131134 array $ middlewares = []
132135 ): ResponseInterface {
@@ -136,11 +139,22 @@ protected function initRequestHandler(
136139 "RequestInterface " => $ request ,
137140 "ServerRequestInterface " => $ request ,
138141 "StreamInterface " => $ stream ,
142+ "PathInterface " => $ path
139143 ]);
140144
141145 $ middlewares = array_merge ($ this ->userMiddlewares , $ middlewares );
142146 $ handler = new RequestHandler ($ middlewares , $ finalHandler );
143- $ response = $ handler ->handle ($ request );
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+ }
144158
145159 return $ response ;
146160 }
0 commit comments