/** * @param HTTPRequestResponseContainer $httpRequestResponse */ public function onRequest(HTTPRequestResponseContainer $httpRequestResponse) { try { $routingResponse = $this->router->route($httpRequestResponse->getRequest()); $this->dispatchRoutingResponseFilter($routingResponse, $httpRequestResponse); } catch (\Exception $e) { if ($e->getCode() == 404) { //This is the fallback "not found" handler. The routing itself can also route to the 404 handler. $routingResponse = $this->router->getNotFoundRoute(); } else { $routingResponse = $this->router->getServerErrorRoute(); $parameters = array_merge(['exception' => $e], $routingResponse->getParameters()); $routingResponse = new RoutingResponse($routingResponse->getStatusCode(), $routingResponse->getClass(), $routingResponse->getMethod(), $parameters); } $this->dispatchRoutingResponseFilter($routingResponse, $httpRequestResponse); } }
public function onRoutingResponse(RoutingResponse $routingResponse, HTTPRequestResponseContainer $httpRequestResponse) { $httpRequestResponse->setResponse($httpRequestResponse->getResponse()->withStatus($routingResponse->getStatusCode())); // Do a locally shared instance for request and response for proper injection. //@todo there should be a smarter way to do this. $dic = clone $this->dic; $dic->share($httpRequestResponse); $dic->share($httpRequestResponse->getRequest()); $dic->share($httpRequestResponse->getResponse()); $controller = $dic->make($routingResponse->getClass()); $controllerResponse = $dic->execute([$controller, $routingResponse->getMethod()], $routingResponse->getParameters()); $this->processControllerResponse($controllerResponse, $routingResponse->getClass(), $routingResponse->getMethod(), $httpRequestResponse); }