/** * Run the application and output the response * * @param Oops_Server_Request $request * Request to dispatch * @return void */ public function Run($request = null) { // @todo skip hander and use error log, or use static handler that will // log errors? $this->_errorHandler = new Oops_Error_Handler(); try { if (!is_object($request)) { $this->_request = new Oops_Server_Request_Http(); $this->_response = new Oops_Server_Response_Http(); } else { $this->_request = $request; $this->_response = new Oops_Server_Response(); } $this->_parseRequest(); $this->_initView(); $this->_routeRequest(); // @todo try to find controller action, then do everything else $this->_initController(); $data = $this->_controller_instance->Run(); // @todo Let the view handler use getRequest and getResponse as it // need it $this->_view->In($data); $this->_view->Set('controller', $this->_router->controller); $this->_view->Set('uri', $this->_request->getUri()); $this->_view->Set('ext', $this->_extension); $this->_view->Set('action', $this->_action); $this->_view->Set('uri_parts', $this->_uri_parts); $this->_response->setHeader("Content-type", $this->_view->getContentType()); $this->_response->setBody($this->_view->Out()); } catch (Oops_Server_Exception $e) { // Here we catch cases where response code was set by router or // controller (302, 301, 404 еtс) switch ($e->getCode()) { case OOPS_SERVER_EXCEPTION_RESPONSE_READY: // // init response code handler here $responseCodeHandler = $this->loadResponseCodeHandler(); $responseCodeHandler->handle($this->_response); break; default: // @todo refactor unknown exceptions catching to log and 500 throw $e; } } catch (Exception $e) { trigger_error($e->getMessage(), E_USER_ERROR); } // @todo refactor error logging etc if ((string) $this->_config->oops->errors2Headers && Oops_Debug::allow()) { $this->_response->reportErrors($this->_errorHandler); } if ((string) $this->_config->oops->errorlog->enabled) { Oops_Log_Error::report($this->_errorHandler, $this->_config->oops->errorlog->path); } restore_error_handler(); return $this->_response; }
/** * Should set body to $response, may change code * * @param Oops_Server_Response $response */ public function handle($response) { // find template, use it or call prev handler if ($response->code >= 400) { /** * * @todo check requested view, use same View */ $templateData = array('errormessage' => $response->message, 'errorcode' => $response->code); $errorTpl = Oops_Template::getInstance('_errorpage/' . $response->code . '.php'); if ($errorTpl->isValid()) { $response->setBody($errorTpl->Out($templateData)); } } }