Packages

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
      • Adapters
      • Extensions
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Diagnostics
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
      • PhpGenerator
  • NetteModule
  • none

Classes

Interfaces

Exceptions

  • Overview
  • Package
  • 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 (http://davidgrudl.com)
  6:  * @package Nette\Application
  7:  */
  8: 
  9: 
 10: 
 11: /**
 12:  * Front Controller.
 13:  *
 14:  * @author     David Grudl
 15:  *
 16:  * @property-read array $requests
 17:  * @property-read IPresenter $presenter
 18:  * @property-read IRouter $router
 19:  * @property-read IPresenterFactory $presenterFactory
 20:  * @package Nette\Application
 21:  */
 22: class NApplication extends NObject
 23: {
 24:     /** @var int */
 25:     public static $maxLoop = 20;
 26: 
 27:     /** @var bool enable fault barrier? */
 28:     public $catchExceptions;
 29: 
 30:     /** @var string */
 31:     public $errorPresenter;
 32: 
 33:     /** @var array of function(Application $sender); Occurs before the application loads presenter */
 34:     public $onStartup;
 35: 
 36:     /** @var array of function(Application $sender, Exception $e = NULL); Occurs before the application shuts down */
 37:     public $onShutdown;
 38: 
 39:     /** @var array of function(Application $sender, Request $request); Occurs when a new request is received */
 40:     public $onRequest;
 41: 
 42:     /** @var array of function(Application $sender, IResponse $response); Occurs when a new response is ready for dispatch */
 43:     public $onResponse;
 44: 
 45:     /** @var array of function(Application $sender, Exception $e); Occurs when an unhandled exception occurs in the application */
 46:     public $onError;
 47: 
 48:     /** @deprecated */
 49:     public $allowedMethods;
 50: 
 51:     /** @var NPresenterRequest[] */
 52:     private $requests = array();
 53: 
 54:     /** @var IPresenter */
 55:     private $presenter;
 56: 
 57:     /** @var IHttpRequest */
 58:     private $httpRequest;
 59: 
 60:     /** @var IHttpResponse */
 61:     private $httpResponse;
 62: 
 63:     /** @var IPresenterFactory */
 64:     private $presenterFactory;
 65: 
 66:     /** @var IRouter */
 67:     private $router;
 68: 
 69: 
 70:     public function __construct(IPresenterFactory $presenterFactory, IRouter $router, IHttpRequest $httpRequest, IHttpResponse $httpResponse)
 71:     {
 72:         $this->httpRequest = $httpRequest;
 73:         $this->httpResponse = $httpResponse;
 74:         $this->presenterFactory = $presenterFactory;
 75:         $this->router = $router;
 76:     }
 77: 
 78: 
 79:     /**
 80:      * Dispatch a HTTP request to a front controller.
 81:      * @return void
 82:      */
 83:     public function run()
 84:     {
 85:         $request = NULL;
 86:         $repeatedError = FALSE;
 87:         do {
 88:             try {
 89:                 if (count($this->requests) > self::$maxLoop) {
 90:                     throw new NApplicationException('Too many loops detected in application life cycle.');
 91:                 }
 92: 
 93:                 if (!$request) {
 94:                     $this->onStartup($this);
 95: 
 96:                     $request = $this->router->match($this->httpRequest);
 97:                     if (!$request instanceof NPresenterRequest) {
 98:                         $request = NULL;
 99:                         throw new NBadRequestException('No route for HTTP request.');
100:                     }
101: 
102:                     if (strcasecmp($request->getPresenterName(), $this->errorPresenter) === 0) {
103:                         throw new NBadRequestException('Invalid request. Presenter is not achievable.');
104:                     }
105:                 }
106: 
107:                 $this->requests[] = $request;
108:                 $this->onRequest($this, $request);
109: 
110:                 // Instantiate presenter
111:                 $presenterName = $request->getPresenterName();
112:                 try {
113:                     $this->presenter = $this->presenterFactory->createPresenter($presenterName);
114:                 } catch (NInvalidPresenterException $e) {
115:                     throw new NBadRequestException($e->getMessage(), 404, $e);
116:                 }
117: 
118:                 $this->presenterFactory->getPresenterClass($presenterName);
119:                 $request->setPresenterName($presenterName);
120:                 $request->freeze();
121: 
122:                 // Execute presenter
123:                 $response = $this->presenter->run($request);
124:                 if ($response) {
125:                     $this->onResponse($this, $response);
126:                 }
127: 
128:                 // Send response
129:                 if ($response instanceof NForwardResponse) {
130:                     $request = $response->getRequest();
131:                     continue;
132: 
133:                 } elseif ($response instanceof IPresenterResponse) {
134:                     $response->send($this->httpRequest, $this->httpResponse);
135:                 }
136:                 break;
137: 
138:             } catch (Exception $e) {
139:                 // fault barrier
140:                 $this->onError($this, $e);
141: 
142:                 if (!$this->catchExceptions) {
143:                     $this->onShutdown($this, $e);
144:                     throw $e;
145:                 }
146: 
147:                 if ($repeatedError) {
148:                     $e = new NApplicationException('An error occurred while executing error-presenter', 0, $e);
149:                 }
150: 
151:                 if (!$this->httpResponse->isSent()) {
152:                     $this->httpResponse->setCode($e instanceof NBadRequestException ? $e->getCode() : 500);
153:                 }
154: 
155:                 if (!$repeatedError && $this->errorPresenter) {
156:                     $repeatedError = TRUE;
157:                     if ($this->presenter instanceof NPresenter) {
158:                         try {
159:                             $this->presenter->forward(":$this->errorPresenter:", array('exception' => $e));
160:                         } catch (NAbortException $foo) {
161:                             $request = $this->presenter->getLastCreatedRequest();
162:                         }
163:                     } else {
164:                         $request = new NPresenterRequest(
165:                             $this->errorPresenter,
166:                             NPresenterRequest::FORWARD,
167:                             array('exception' => $e)
168:                         );
169:                     }
170:                     // continue
171: 
172:                 } else { // default error handler
173:                     if ($e instanceof NBadRequestException) {
174:                         $code = $e->getCode();
175:                     } else {
176:                         $code = 500;
177:                         NDebugger::log($e, NDebugger::ERROR);
178:                     }
179:                     require dirname(__FILE__) . '/templates/error.phtml';
180:                     break;
181:                 }
182:             }
183:         } while (1);
184: 
185:         $this->onShutdown($this, isset($e) ? $e : NULL);
186:     }
187: 
188: 
189:     /**
190:      * Returns all processed requests.
191:      * @return NPresenterRequest[]
192:      */
193:     public function getRequests()
194:     {
195:         return $this->requests;
196:     }
197: 
198: 
199:     /**
200:      * Returns current presenter.
201:      * @return IPresenter
202:      */
203:     public function getPresenter()
204:     {
205:         return $this->presenter;
206:     }
207: 
208: 
209:     /********************* services ****************d*g**/
210: 
211: 
212:     /**
213:      * Returns router.
214:      * @return IRouter
215:      */
216:     public function getRouter()
217:     {
218:         return $this->router;
219:     }
220: 
221: 
222:     /**
223:      * Returns presenter factory.
224:      * @return IPresenterFactory
225:      */
226:     public function getPresenterFactory()
227:     {
228:         return $this->presenterFactory;
229:     }
230: 
231: 
232:     /********************* request serialization ****************d*g**/
233: 
234: 
235:     /** @deprecated */
236:     function storeRequest($expiration = '+ 10 minutes')
237:     {
238:         return $this->presenter->storeRequest($expiration);
239:     }
240: 
241:     /** @deprecated */
242:     function restoreRequest($key)
243:     {
244:         return $this->presenter->restoreRequest($key);
245:     }
246: 
247: }
248: 
Nette Framework 2.0.18 (for PHP 5.2, prefixed) API documentation generated by ApiGen 2.8.0