Packages

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

Classes

  • NApplication
  • NPresenterFactory
  • NPresenterRequest

Interfaces

  • IPresenter
  • IPresenterFactory
  • IPresenterResponse
  • IRouter

Exceptions

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