Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationLatte
      • ApplicationTracy
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsLatte
      • Framework
      • HttpTracy
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • none
  • Tracy

Classes

  • Application
  • PresenterFactory
  • Request

Interfaces

  • IPresenter
  • IPresenterFactory
  • IResponse
  • IRouter

Exceptions

  • AbortException
  • ApplicationException
  • BadRequestException
  • ForbiddenRequestException
  • InvalidPresenterException
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  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:  * @author     David Grudl
 17:  */
 18: class Application extends Nette\Object
 19: {
 20:     /** @var int */
 21:     public static $maxLoop = 20;
 22: 
 23:     /** @var bool enable fault barrier? */
 24:     public $catchExceptions;
 25: 
 26:     /** @var string */
 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 $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 $e); Occurs when an unhandled exception occurs in the application */
 45:     public $onError;
 46: 
 47:     /** @var Request[] */
 48:     private $requests = array();
 49: 
 50:     /** @var IPresenter */
 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:             $this->onError($this, $e);
 88:             if ($this->catchExceptions && $this->errorPresenter) {
 89:                 try {
 90:                     $this->processException($e);
 91:                     $this->onShutdown($this, $e);
 92:                     return;
 93: 
 94:                 } catch (\Exception $e) {
 95:                     $this->onError($this, $e);
 96:                 }
 97:             }
 98:             $this->onShutdown($this, $e);
 99:             throw $e;
100:         }
101:     }
102: 
103: 
104:     /**
105:      * @return Request
106:      */
107:     public function createInitialRequest()
108:     {
109:         $request = $this->router->match($this->httpRequest);
110: 
111:         if (!$request instanceof Request) {
112:             throw new BadRequestException('No route for HTTP request.');
113:         }
114:         $name = $request->getPresenterName();
115:         if (strcasecmp($name, $this->errorPresenter) === 0
116:             || (Nette\Utils\Strings::startsWith($name, 'Nette:') && $name !== 'Nette:Micro')
117:         ) {
118:             throw new BadRequestException('Invalid request. Presenter is not achievable.');
119:         }
120: 
121:         try {
122:             $this->presenterFactory->getPresenterClass($name);
123:             $request->setPresenterName($name);
124:         } catch (InvalidPresenterException $e) {
125:             throw new BadRequestException($e->getMessage(), 0, $e);
126:         }
127: 
128:         return $request;
129:     }
130: 
131: 
132:     /**
133:      * @return void
134:      */
135:     public function processRequest(Request $request)
136:     {
137:         if (count($this->requests) > self::$maxLoop) {
138:             throw new ApplicationException('Too many loops detected in application life cycle.');
139:         }
140: 
141:         $this->requests[] = $request;
142:         $this->onRequest($this, $request);
143: 
144:         $this->presenter = $this->presenterFactory->createPresenter($request->getPresenterName());
145:         $this->onPresenter($this, $this->presenter);
146:         $response = $this->presenter->run($request);
147: 
148:         if ($response instanceof Responses\ForwardResponse) {
149:             $this->processRequest($response->getRequest());
150: 
151:         } elseif ($response) {
152:             $this->onResponse($this, $response);
153:             $response->send($this->httpRequest, $this->httpResponse);
154:         }
155:     }
156: 
157: 
158:     /**
159:      * @return void
160:      */
161:     public function processException(\Exception $e)
162:     {
163:         if (!$e instanceof BadRequestException && $this->httpResponse instanceof Nette\Http\Response) {
164:             $this->httpResponse->warnOnBuffer = FALSE;
165:         }
166:         if (!$this->httpResponse->isSent()) {
167:             $this->httpResponse->setCode($e instanceof BadRequestException ? ($e->getCode() ?: 404) : 500);
168:         }
169: 
170:         $args = array('exception' => $e, 'request' => end($this->requests) ?: NULL);
171:         if ($this->presenter instanceof UI\Presenter) {
172:             try {
173:                 $this->presenter->forward(":$this->errorPresenter:", $args);
174:             } catch (AbortException $foo) {
175:                 $this->processRequest($this->presenter->getLastCreatedRequest());
176:             }
177:         } else {
178:             $this->processRequest(new Request($this->errorPresenter, Request::FORWARD, $args));
179:         }
180:     }
181: 
182: 
183:     /**
184:      * Returns all processed requests.
185:      * @return Request[]
186:      */
187:     public function getRequests()
188:     {
189:         return $this->requests;
190:     }
191: 
192: 
193:     /**
194:      * Returns current presenter.
195:      * @return IPresenter
196:      */
197:     public function getPresenter()
198:     {
199:         return $this->presenter;
200:     }
201: 
202: 
203:     /********************* services ****************d*g**/
204: 
205: 
206:     /**
207:      * Returns router.
208:      * @return IRouter
209:      */
210:     public function getRouter()
211:     {
212:         return $this->router;
213:     }
214: 
215: 
216:     /**
217:      * Returns presenter factory.
218:      * @return IPresenterFactory
219:      */
220:     public function getPresenterFactory()
221:     {
222:         return $this->presenterFactory;
223:     }
224: 
225: 
226:     /********************* request serialization ****************d*g**/
227: 
228: 
229:     /** @deprecated */
230:     function storeRequest($expiration = '+ 10 minutes')
231:     {
232:         trigger_error(__METHOD__ . '() is deprecated; use $presenter->storeRequest() instead.', E_USER_DEPRECATED);
233:         return $this->presenter->storeRequest($expiration);
234:     }
235: 
236:     /** @deprecated */
237:     function restoreRequest($key)
238:     {
239:         trigger_error(__METHOD__ . '() is deprecated; use $presenter->restoreRequest() instead.', E_USER_DEPRECATED);
240:         return $this->presenter->restoreRequest($key);
241:     }
242: 
243: }
244: 
Nette 2.2.14 API API documentation generated by ApiGen 2.8.0