Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationDI
      • ApplicationLatte
      • ApplicationTracy
      • CacheDI
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsDI
      • FormsLatte
      • Framework
      • HttpDI
      • HttpTracy
      • MailDI
      • ReflectionDI
      • SecurityDI
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Conventions
      • Drivers
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
      • Traits
    • Reflection
    • Security
    • Tokenizer
    • Utils
  • Tracy
    • Bridges
      • Nette
  • none

Classes

  • Application
  • Helpers
  • LinkGenerator
  • PresenterFactory
  • Request

Interfaces

  • IPresenter
  • IPresenterFactory
  • IResponse
  • IRouter

Exceptions

  • AbortException
  • ApplicationException
  • BadRequestException
  • ForbiddenRequestException
  • InvalidPresenterException
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Other releases
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://nette.org)
  5:  * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette\Application;
  9: 
 10: use Nette;
 11: 
 12: 
 13: /**
 14:  * Front Controller.
 15:  */
 16: class Application
 17: {
 18:     use Nette\SmartObject;
 19: 
 20:     /** @var int */
 21:     public static $maxLoop = 20;
 22: 
 23:     /** @var bool enable fault barrier? */
 24:     public $catchExceptions;
 25: 
 26:     /** @var string|null */
 27:     public $errorPresenter;
 28: 
 29:     /** @var callable[]  function (Application $sender); Occurs before the application loads presenter */
 30:     public $onStartup;
 31: 
 32:     /** @var callable[]  function (Application $sender, \Exception|\Throwable $e = null); Occurs before the application shuts down */
 33:     public $onShutdown;
 34: 
 35:     /** @var callable[]  function (Application $sender, Request $request); Occurs when a new request is received */
 36:     public $onRequest;
 37: 
 38:     /** @var callable[]  function (Application $sender, Presenter $presenter); Occurs when a presenter is created */
 39:     public $onPresenter;
 40: 
 41:     /** @var callable[]  function (Application $sender, IResponse $response); Occurs when a new response is ready for dispatch */
 42:     public $onResponse;
 43: 
 44:     /** @var callable[]  function (Application $sender, \Exception|\Throwable $e); Occurs when an unhandled exception occurs in the application */
 45:     public $onError;
 46: 
 47:     /** @var Request[] */
 48:     private $requests = [];
 49: 
 50:     /** @var IPresenter|null */
 51:     private $presenter;
 52: 
 53:     /** @var Nette\Http\IRequest */
 54:     private $httpRequest;
 55: 
 56:     /** @var Nette\Http\IResponse */
 57:     private $httpResponse;
 58: 
 59:     /** @var IPresenterFactory */
 60:     private $presenterFactory;
 61: 
 62:     /** @var IRouter */
 63:     private $router;
 64: 
 65: 
 66:     public function __construct(IPresenterFactory $presenterFactory, IRouter $router, Nette\Http\IRequest $httpRequest, Nette\Http\IResponse $httpResponse)
 67:     {
 68:         $this->httpRequest = $httpRequest;
 69:         $this->httpResponse = $httpResponse;
 70:         $this->presenterFactory = $presenterFactory;
 71:         $this->router = $router;
 72:     }
 73: 
 74: 
 75:     /**
 76:      * Dispatch a HTTP request to a front controller.
 77:      * @return void
 78:      */
 79:     public function run()
 80:     {
 81:         try {
 82:             $this->onStartup($this);
 83:             $this->processRequest($this->createInitialRequest());
 84:             $this->onShutdown($this);
 85: 
 86:         } catch (\Exception $e) {
 87:         } catch (\Throwable $e) {
 88:         }
 89:         if (isset($e)) {
 90:             $this->onError($this, $e);
 91:             if ($this->catchExceptions && $this->errorPresenter) {
 92:                 try {
 93:                     $this->processException($e);
 94:                     $this->onShutdown($this, $e);
 95:                     return;
 96: 
 97:                 } catch (\Exception $e) {
 98:                     $this->onError($this, $e);
 99:                 } catch (\Throwable $e) {
100:                     $this->onError($this, $e);
101:                 }
102:             }
103:             $this->onShutdown($this, $e);
104:             throw $e;
105:         }
106:     }
107: 
108: 
109:     /**
110:      * @return Request
111:      */
112:     public function createInitialRequest()
113:     {
114:         $request = $this->router->match($this->httpRequest);
115:         if (!$request instanceof Request) {
116:             throw new BadRequestException('No route for HTTP request.');
117:         }
118:         return $request;
119:     }
120: 
121: 
122:     /**
123:      * @return void
124:      */
125:     public function processRequest(Request $request)
126:     {
127:         process:
128:         if (count($this->requests) > self::$maxLoop) {
129:             throw new ApplicationException('Too many loops detected in application life cycle.');
130:         }
131: 
132:         $this->requests[] = $request;
133:         $this->onRequest($this, $request);
134: 
135:         if (!$request->isMethod($request::FORWARD) && !strcasecmp($request->getPresenterName(), $this->errorPresenter)) {
136:             throw new BadRequestException('Invalid request. Presenter is not achievable.');
137:         }
138: 
139:         try {
140:             $this->presenter = $this->presenterFactory->createPresenter($request->getPresenterName());
141:         } catch (InvalidPresenterException $e) {
142:             throw count($this->requests) > 1 ? $e : new BadRequestException($e->getMessage(), 0, $e);
143:         }
144:         $this->onPresenter($this, $this->presenter);
145:         $response = $this->presenter->run(clone $request);
146: 
147:         if ($response instanceof Responses\ForwardResponse) {
148:             $request = $response->getRequest();
149:             goto process;
150: 
151:         } elseif ($response) {
152:             $this->onResponse($this, $response);
153:             $response->send($this->httpRequest, $this->httpResponse);
154:         }
155:     }
156: 
157: 
158:     /**
159:      * @param  \Exception|\Throwable
160:      * @return void
161:      */
162:     public function processException($e)
163:     {
164:         if (!$e instanceof BadRequestException && $this->httpResponse instanceof Nette\Http\Response) {
165:             $this->httpResponse->warnOnBuffer = false;
166:         }
167:         if (!$this->httpResponse->isSent()) {
168:             $this->httpResponse->setCode($e instanceof BadRequestException ? ($e->getHttpCode() ?: 404) : 500);
169:         }
170: 
171:         $args = ['exception' => $e, 'request' => end($this->requests) ?: null];
172:         if ($this->presenter instanceof UI\Presenter) {
173:             try {
174:                 $this->presenter->forward(":$this->errorPresenter:", $args);
175:             } catch (AbortException $foo) {
176:                 $this->processRequest($this->presenter->getLastCreatedRequest());
177:             }
178:         } else {
179:             $this->processRequest(new Request($this->errorPresenter, Request::FORWARD, $args));
180:         }
181:     }
182: 
183: 
184:     /**
185:      * Returns all processed requests.
186:      * @return Request[]
187:      */
188:     public function getRequests()
189:     {
190:         return $this->requests;
191:     }
192: 
193: 
194:     /**
195:      * Returns current presenter.
196:      * @return IPresenter|null
197:      */
198:     public function getPresenter()
199:     {
200:         return $this->presenter;
201:     }
202: 
203: 
204:     /********************* services ****************d*g**/
205: 
206: 
207:     /**
208:      * Returns router.
209:      * @return IRouter
210:      */
211:     public function getRouter()
212:     {
213:         return $this->router;
214:     }
215: 
216: 
217:     /**
218:      * Returns presenter factory.
219:      * @return IPresenterFactory
220:      */
221:     public function getPresenterFactory()
222:     {
223:         return $this->presenterFactory;
224:     }
225: }
226: 
Nette 2.4-20180918 API API documentation generated by ApiGen 2.8.0