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
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Utils
  • none
  • Tracy
    • Bridges
      • Nette

Classes

  • Application
  • LinkGenerator
  • PresenterFactory
  • Request

Interfaces

  • IPresenter
  • IPresenterFactory
  • IResponse
  • IRouter

Exceptions

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