/** * @param Translator $translator * @return string|NULL */ public function resolve(Translator $translator) { if (!$this->session->isStarted() && $this->httpResponse->isSent()) { trigger_error("The advice of session locale resolver is required but the session has not been started and headers had been already sent. " . "Either start your sessions earlier or disabled the SessionResolver.", E_USER_WARNING); return NULL; } if (empty($this->localeSession->locale)) { return NULL; } $short = array_map(function ($locale) { return substr($locale, 0, 2); }, $translator->getAvailableLocales()); if (!in_array(substr($this->localeSession->locale, 0, 2), $short, TRUE)) { return NULL; } return $this->localeSession->locale; }
/** * Destroys all data registered to a session. * @return void */ public function destroy() { if (!self::$started) { throw new Nette\InvalidStateException('Session is not started.'); } session_destroy(); $_SESSION = NULL; self::$started = FALSE; if (!$this->response->isSent()) { $params = session_get_cookie_params(); $this->response->deleteCookie(session_name(), $params['path'], $params['domain'], $params['secure']); } }
public function doProcessException($e) { if (!$e instanceof BadRequestException && $this->httpResponse instanceof Response) { $this->httpResponse->warnOnBuffer = false; } if (!$this->httpResponse->isSent()) { $this->httpResponse->setCode($e instanceof BadRequestException ? $e->getCode() ?: 404 : 500); } $requests = $this->getRequests(); $request = end($requests) ?: $this->initialRequest; $args = ['exception' => $e, 'request' => $request]; $errorPresenter = $request ? $this->findErrorPresenter($request->getPresenterName()) : $this->errorPresenter; if ($this->getPresenter() instanceof Presenter) { try { $this->getPresenter()->forward(":{$errorPresenter}:", $args); } catch (AbortException $_) { $this->processRequest($this->getPresenter()->getLastCreatedRequest()); } } else { $this->processRequest(new Request($errorPresenter, Request::FORWARD, $args)); } }
/** * @return void */ public function processException(\Exception $e) { if (!$this->httpResponse->isSent()) { $this->httpResponse->setCode($e instanceof BadRequestException ? $e->getCode() ?: 404 : 500); } $args = array('exception' => $e, 'request' => end($this->requests) ?: NULL); if ($this->presenter instanceof UI\Presenter) { try { $this->presenter->forward(":{$this->errorPresenter}:", $args); } catch (AbortException $foo) { $this->processRequest($this->presenter->getLastCreatedRequest()); } } else { $this->processRequest(new Request($this->errorPresenter, Request::FORWARD, $args)); } }
/** * @return Nette\Application\IResponse */ public function run(Application\Request $request) { try { // STARTUP $this->request = $request; $this->payload = new \stdClass(); $this->setParent($this->getParent(), $request->getPresenterName()); if (!$this->httpResponse->isSent()) { $this->httpResponse->addHeader('Vary', 'X-Requested-With'); } $this->initGlobalParameters(); $this->checkRequirements($this->getReflection()); $this->startup(); if (!$this->startupCheck) { $class = $this->getReflection()->getMethod('startup')->getDeclaringClass()->getName(); throw new Nette\InvalidStateException("Method {$class}::startup() or its descendant doesn't call parent::startup()."); } // calls $this->action<Action>() $this->tryCall($this->formatActionMethod($this->action), $this->params); // autoload components foreach ($this->globalParams as $id => $foo) { $this->getComponent($id, FALSE); } if ($this->autoCanonicalize) { $this->canonicalize(); } if ($this->httpRequest->isMethod('head')) { $this->terminate(); } // SIGNAL HANDLING // calls $this->handle<Signal>() $this->processSignal(); // RENDERING VIEW $this->beforeRender(); // calls $this->render<View>() $this->tryCall($this->formatRenderMethod($this->view), $this->params); $this->afterRender(); // save component tree persistent state $this->saveGlobalState(); if ($this->isAjax()) { $this->payload->state = $this->getGlobalState(); } // finish template rendering $this->sendTemplate(); } catch (Application\AbortException $e) { // continue with shutting down if ($this->isAjax()) { try { $hasPayload = (array) $this->payload; unset($hasPayload['state']); if ($this->response instanceof Responses\TextResponse && $this->isControlInvalid()) { $this->snippetMode = TRUE; $this->response->send($this->httpRequest, $this->httpResponse); $this->sendPayload(); } elseif (!$this->response && $hasPayload) { // back compatibility for use terminate() instead of sendPayload() $this->sendPayload(); } } catch (Application\AbortException $e) { } } if ($this->hasFlashSession()) { $this->getFlashSession()->setExpiration($this->response instanceof Responses\RedirectResponse ? '+ 30 seconds' : '+ 3 seconds'); } // SHUTDOWN $this->onShutdown($this, $this->response); $this->shutdown($this->response); return $this->response; } }
/** * 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 ApplicationException('Too many loops detected in application life cycle.'); } if (!$request) { $this->onStartup($this); $request = $this->router->match($this->httpRequest); if (!$request instanceof Request) { $request = null; throw new BadRequestException('No route for HTTP request.'); } if (strcasecmp($request->getPresenterName(), $this->errorPresenter) === 0) { throw new BadRequestException('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 (InvalidPresenterException $e) { throw new BadRequestException($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 Responses\ForwardResponse) { $request = $response->getRequest(); continue; } elseif ($response instanceof IResponse) { $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 ApplicationException('An error occurred while executing error-presenter', 0, $e); } if (!$this->httpResponse->isSent()) { $this->httpResponse->setCode($e instanceof BadRequestException ? $e->getCode() : 500); } if (!$repeatedError && $this->errorPresenter) { $repeatedError = true; if ($this->presenter instanceof UI\Presenter) { try { $this->presenter->forward(":{$this->errorPresenter}:", array('exception' => $e)); } catch (AbortException $foo) { $request = $this->presenter->getLastCreatedRequest(); } } else { $request = new Request($this->errorPresenter, Request::FORWARD, array('exception' => $e)); } // continue } else { // default error handler if ($e instanceof BadRequestException) { $code = $e->getCode(); } else { $code = 500; Nette\Diagnostics\Debugger::log($e, Nette\Diagnostics\Debugger::ERROR); } require __DIR__ . '/templates/error.phtml'; break; } } } while (1); $this->onShutdown($this, isset($e) ? $e : null); }
/** * 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 ApplicationException('Too many loops detected in application life cycle.'); } if (!$request) { $this->onStartup($this); $request = $this->router->match($this->httpRequest); if (!$request instanceof Request) { $request = NULL; throw new BadRequestException('No route for HTTP request.'); } if (strcasecmp($request->getPresenterName(), $this->errorPresenter) === 0) { throw new BadRequestException('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 (InvalidPresenterException $e) { throw new BadRequestException($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 Responses\ForwardResponse) { $request = $response->getRequest(); continue; } elseif ($response instanceof IResponse) { $response->send($this->httpRequest, $this->httpResponse); } break; } catch (\Exception $e) { $this->onError($this, $e); if ($repeatedError) { $e = new ApplicationException("An error occurred while executing error-presenter '{$this->errorPresenter}'.", 0, $e); } if ($repeatedError || !$this->catchExceptions) { $this->onShutdown($this, $e); throw $e; } $repeatedError = TRUE; $this->errorPresenter = $this->errorPresenter ?: 'Nette:Error'; if (!$this->httpResponse->isSent()) { $this->httpResponse->setCode($e instanceof BadRequestException ? $e->getCode() : 500); } if ($this->presenter instanceof UI\Presenter) { try { $this->presenter->forward(":{$this->errorPresenter}:", array('exception' => $e)); } catch (AbortException $foo) { $request = $this->presenter->getLastCreatedRequest(); } } else { $request = new Request($this->errorPresenter, Request::FORWARD, array('exception' => $e)); } // continue } } while (1); $this->onShutdown($this, isset($e) ? $e : NULL); }