예제 #1
0
파일: loader.php 프로젝트: GE3/GE3
 function run()
 {
     $httpRequest = $this->getHttpRequest();
     $httpResponse = $this->getHttpResponse();
     $httpRequest->setEncoding('UTF-8');
     if (NEnvironment::getVariable('baseUri') === NULL) {
         NEnvironment::setVariable('baseUri', $httpRequest->getUri()->getBasePath());
     }
     $session = $this->getSession();
     if (!$session->isStarted() && $session->exists()) {
         $session->start();
     }
     NDebug::addPanel(new NRoutingDebugger($this->getRouter(), $httpRequest));
     if ($this->allowedMethods) {
         $method = $httpRequest->getMethod();
         if (!in_array($method, $this->allowedMethods, TRUE)) {
             $httpResponse->setCode(IHttpResponse::S501_NOT_IMPLEMENTED);
             $httpResponse->setHeader('Allow', implode(',', $this->allowedMethods));
             echo '<h1>Method ' . htmlSpecialChars($method) . ' is not implemented</h1>';
             return;
         }
     }
     $request = NULL;
     $repeatedError = FALSE;
     do {
         try {
             if (count($this->requests) > self::$maxLoop) {
                 throw new NApplicationException('Too many loops detected in application life cycle.');
             }
             if (!$request) {
                 $this->onStartup($this);
                 $router = $this->getRouter();
                 if ($router instanceof NMultiRouter && !count($router)) {
                     $router[] = new NSimpleRouter(array('presenter' => 'Default', 'action' => 'default'));
                 }
                 $request = $router->match($httpRequest);
                 if (!$request instanceof NPresenterRequest) {
                     $request = NULL;
                     throw new NBadRequestException('No route for HTTP request.');
                 }
                 if (strcasecmp($request->getPresenterName(), $this->errorPresenter) === 0) {
                     throw new NBadRequestException('Invalid request.');
                 }
             }
             $this->requests[] = $request;
             $this->onRequest($this, $request);
             $presenter = $request->getPresenterName();
             try {
                 $class = $this->getPresenterLoader()->getPresenterClass($presenter);
                 $request->setPresenterName($presenter);
             } catch (NInvalidPresenterException $e) {
                 throw new NBadRequestException($e->getMessage(), 404, $e);
             }
             $request->freeze();
             $this->presenter = new $class();
             $response = $this->presenter->run($request);
             if ($response instanceof NForwardingResponse) {
                 $request = $response->getRequest();
                 continue;
             } elseif ($response instanceof IPresenterResponse) {
                 $response->send();
             }
             break;
         } catch (Exception $e) {
             if ($this->catchExceptions === NULL) {
                 $this->catchExceptions = NEnvironment::isProduction();
             }
             $this->onError($this, $e);
             if (!$this->catchExceptions) {
                 $this->onShutdown($this, $e);
                 throw $e;
             }
             if ($repeatedError) {
                 $e = new NApplicationException('An error occured while executing error-presenter', 0, $e);
             }
             if (!$httpResponse->isSent()) {
                 $httpResponse->setCode($e instanceof NBadRequestException ? $e->getCode() : 500);
             }
             if (!$repeatedError && $this->errorPresenter) {
                 $repeatedError = TRUE;
                 $request = new NPresenterRequest($this->errorPresenter, NPresenterRequest::FORWARD, array('exception' => $e));
             } else {
                 echo "<meta name='robots' content='noindex'>\n\n";
                 if ($e instanceof NBadRequestException) {
                     echo "<title>404 Not Found</title>\n\n<h1>Not Found</h1>\n\n<p>The requested URL was not found on this server.</p>";
                 } else {
                     NDebug::processException($e, FALSE);
                     echo "<title>500 Internal Server Error</title>\n\n<h1>Server Error</h1>\n\n", "<p>The server encountered an internal error and was unable to complete your request. Please try again later.</p>";
                 }
                 echo "\n\n<hr>\n<small><i>Nette Framework</i></small>";
                 break;
             }
         }
     } while (1);
     $this->onShutdown($this, isset($e) ? $e : NULL);
 }