Пример #1
0
 /**
  * Runs the application.
  *
  * @return void
  */
 function run()
 {
     try {
         try {
             $routeData = $this->router->process($this->request);
             $this->dispatcher->handle($routeData, $this->request);
         } catch (DispatchException $e) {
             $this->handle404($e);
         } catch (RouteException $e) {
             $this->handle404($e);
         }
     } catch (Exception $e) {
         $this->handle500($e);
     }
 }
Пример #2
0
 /**
  * @return Nette\Application\IResponse
  */
 public function run(Application\Request $request)
 {
     $this->request = $request;
     if ($this->httpRequest && $this->router && !$this->httpRequest->isAjax() && ($request->isMethod('get') || $request->isMethod('head'))) {
         $refUrl = clone $this->httpRequest->getUrl();
         $url = $this->router->constructUrl($request, $refUrl->setPath($refUrl->getScriptPath()));
         if ($url !== NULL && !$this->httpRequest->getUrl()->isEqual($url)) {
             return new Responses\RedirectResponse($url, Http\IResponse::S301_MOVED_PERMANENTLY);
         }
     }
     $params = $request->getParameters();
     if (!isset($params['callback'])) {
         throw new Application\BadRequestException('Parameter callback is missing.');
     }
     $params['presenter'] = $this;
     $callback = $params['callback'];
     $reflection = Nette\Utils\Callback::toReflection(Nette\Utils\Callback::check($callback));
     $params = Application\UI\PresenterComponentReflection::combineArgs($reflection, $params);
     foreach ($reflection->getParameters() as $param) {
         if ($param->getClassName()) {
             unset($params[$param->getPosition()]);
         }
     }
     if ($this->context) {
         $params = Nette\DI\Helpers::autowireArguments($reflection, $params, $this->context);
     }
     $response = call_user_func_array($callback, $params);
     if (is_string($response)) {
         $response = array($response, array());
     }
     if (is_array($response)) {
         $response = $this->createTemplate()->setParameters($response[1]);
         if ($response[0] instanceof \SplFileInfo) {
             $response->setFile($response[0]);
         } else {
             $response->setSource($response[0]);
             // TODO
         }
     }
     if ($response instanceof Application\UI\ITemplate) {
         return new Responses\TextResponse($response);
     } else {
         return $response;
     }
 }
 /**
  * Handles the situation when the application failed to found the corresponding route
  *
  * @throws Exception in case of application fault
  * @param Trace $trace trace failed to be handled
  * @return void
  */
 protected function handle404(Trace $trace)
 {
     $this->webContext->getResponse()->setStatus(new HttpStatus(HttpStatus::CODE_404));
     $this->router->getFallbackTrace($trace);
 }
Пример #4
0
	/**
	 * Dispatch a HTTP request to a front controller.
	 * @return void
	 */
	public function run()
	{
		$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);

					$request = $this->router->match($this->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. Presenter is not achievable.');
					}
				}

				$this->requests[] = $request;
				$this->onRequest($this, $request);

				// Instantiate presenter
				$presenterName = $request->getPresenterName();
				try {
					$this->presenter = $this->presenterFactory->createPresenter($presenterName);
				} catch (NInvalidPresenterException $e) {
					throw new NBadRequestException($e->getMessage(), 404, $e);
				}

				$this->presenterFactory->getPresenterClass($presenterName);
				$request->setPresenterName($presenterName);
				$request->freeze();

				// Execute presenter
				$response = $this->presenter->run($request);
				if ($response) {
					$this->onResponse($this, $response);
				}

				// Send response
				if ($response instanceof NForwardResponse) {
					$request = $response->getRequest();
					continue;

				} elseif ($response instanceof IPresenterResponse) {
					$response->send($this->httpRequest, $this->httpResponse);
				}
				break;

			} catch (Exception $e) {
				// fault barrier
				$this->onError($this, $e);

				if (!$this->catchExceptions) {
					$this->onShutdown($this, $e);
					throw $e;
				}

				if ($repeatedError) {
					$e = new NApplicationException('An error occurred while executing error-presenter', 0, $e);
				}

				if (!$this->httpResponse->isSent()) {
					$this->httpResponse->setCode($e instanceof NBadRequestException ? $e->getCode() : 500);
				}

				if (!$repeatedError && $this->errorPresenter) {
					$repeatedError = TRUE;
					if ($this->presenter instanceof NPresenter) {
						try {
							$this->presenter->forward(":$this->errorPresenter:", array('exception' => $e));
						} catch (NAbortException $foo) {
							$request = $this->presenter->getLastCreatedRequest();
						}
					} else {
						$request = new NPresenterRequest(
							$this->errorPresenter,
							NPresenterRequest::FORWARD,
							array('exception' => $e)
						);
					}
					// continue

				} else { // default error handler
					if ($e instanceof NBadRequestException) {
						$code = $e->getCode();
					} else {
						$code = 500;
						NDebugger::log($e, NDebugger::ERROR);
					}
					require dirname(__FILE__) . '/templates/error.phtml';
					break;
				}
			}
		} while (1);

		$this->onShutdown($this, isset($e) ? $e : NULL);
	}