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
  • PHP

Classes

  • NApplication
  • NPresenterFactory
  • NPresenterRequest

Interfaces

  • IPresenter
  • IPresenterFactory
  • IPresenterResponse
  • IRouter

Exceptions

  • NAbortException
  • NApplicationException
  • NBadRequestException
  • NForbiddenRequestException
  • NInvalidPresenterException
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  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 received */
 44:     public $onRequest;
 45: 
 46:     /** @var array of function(Application $sender, IResponse $response); Occurs when a new response is ready for dispatch */
 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 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 IPresenterFactory */
 68:     private $presenterFactory;
 69: 
 70:     /** @var IRouter */
 71:     private $router;
 72: 
 73: 
 74:     public function __construct(IPresenterFactory $presenterFactory, IRouter $router, IHttpRequest $httpRequest, IHttpResponse $httpResponse)
 75:     {
 76:         $this->httpRequest = $httpRequest;
 77:         $this->httpResponse = $httpResponse;
 78:         $this->presenterFactory = $presenterFactory;
 79:         $this->router = $router;
 80:     }
 81: 
 82: 
 83:     /**
 84:      * Dispatch a HTTP request to a front controller.
 85:      * @return void
 86:      */
 87:     public function run()
 88:     {
 89:         $request = NULL;
 90:         $repeatedError = FALSE;
 91:         do {
 92:             try {
 93:                 if (count($this->requests) > self::$maxLoop) {
 94:                     throw new NApplicationException('Too many loops detected in application life cycle.');
 95:                 }
 96: 
 97:                 if (!$request) {
 98:                     $this->onStartup($this);
 99: 
100:                     $request = $this->router->match($this->httpRequest);
101:                     if (!$request instanceof NPresenterRequest) {
102:                         $request = NULL;
103:                         throw new NBadRequestException('No route for HTTP request.');
104:                     }
105: 
106:                     if (strcasecmp($request->getPresenterName(), $this->errorPresenter) === 0) {
107:                         throw new NBadRequestException('Invalid request. Presenter is not achievable.');
108:                     }
109:                 }
110: 
111:                 $this->requests[] = $request;
112:                 $this->onRequest($this, $request);
113: 
114:                 // Instantiate presenter
115:                 $presenterName = $request->getPresenterName();
116:                 try {
117:                     $this->presenter = $this->presenterFactory->createPresenter($presenterName);
118:                 } catch (NInvalidPresenterException $e) {
119:                     throw new NBadRequestException($e->getMessage(), 404, $e);
120:                 }
121: 
122:                 $this->presenterFactory->getPresenterClass($presenterName);
123:                 $request->setPresenterName($presenterName);
124:                 $request->freeze();
125: 
126:                 // Execute presenter
127:                 $response = $this->presenter->run($request);
128:                 if ($response) {
129:                     $this->onResponse($this, $response);
130:                 }
131: 
132:                 // Send response
133:                 if ($response instanceof NForwardResponse) {
134:                     $request = $response->getRequest();
135:                     continue;
136: 
137:                 } elseif ($response instanceof IPresenterResponse) {
138:                     $response->send($this->httpRequest, $this->httpResponse);
139:                 }
140:                 break;
141: 
142:             } catch (Exception $e) {
143:                 // fault barrier
144:                 $this->onError($this, $e);
145: 
146:                 if (!$this->catchExceptions) {
147:                     $this->onShutdown($this, $e);
148:                     throw $e;
149:                 }
150: 
151:                 if ($repeatedError) {
152:                     $e = new NApplicationException('An error occurred while executing error-presenter', 0, $e);
153:                 }
154: 
155:                 if (!$this->httpResponse->isSent()) {
156:                     $this->httpResponse->setCode($e instanceof NBadRequestException ? $e->getCode() : 500);
157:                 }
158: 
159:                 if (!$repeatedError && $this->errorPresenter) {
160:                     $repeatedError = TRUE;
161:                     if ($this->presenter instanceof NPresenter) {
162:                         try {
163:                             $this->presenter->forward(":$this->errorPresenter:", array('exception' => $e));
164:                         } catch (NAbortException $foo) {
165:                             $request = $this->presenter->getLastCreatedRequest();
166:                         }
167:                     } else {
168:                         $request = new NPresenterRequest(
169:                             $this->errorPresenter,
170:                             NPresenterRequest::FORWARD,
171:                             array('exception' => $e)
172:                         );
173:                     }
174:                     // continue
175: 
176:                 } else { // default error handler
177:                     if ($e instanceof NBadRequestException) {
178:                         $code = $e->getCode();
179:                     } else {
180:                         $code = 500;
181:                         NDebugger::log($e, NDebugger::ERROR);
182:                     }
183:                     require dirname(__FILE__) . '/templates/error.phtml';
184:                     break;
185:                 }
186:             }
187:         } while (1);
188: 
189:         $this->onShutdown($this, isset($e) ? $e : NULL);
190:     }
191: 
192: 
193:     /**
194:      * Returns all processed requests.
195:      * @return NPresenterRequest[]
196:      */
197:     final public function getRequests()
198:     {
199:         return $this->requests;
200:     }
201: 
202: 
203:     /**
204:      * Returns current presenter.
205:      * @return IPresenter
206:      */
207:     final public function getPresenter()
208:     {
209:         return $this->presenter;
210:     }
211: 
212: 
213:     /********************* services ****************d*g**/
214: 
215: 
216:     /**
217:      * Returns router.
218:      * @return IRouter
219:      */
220:     public function getRouter()
221:     {
222:         return $this->router;
223:     }
224: 
225: 
226:     /**
227:      * Returns presenter factory.
228:      * @return IPresenterFactory
229:      */
230:     public function getPresenterFactory()
231:     {
232:         return $this->presenterFactory;
233:     }
234: 
235: 
236:     /********************* request serialization ****************d*g**/
237: 
238: 
239:     /** @deprecated */
240:     function storeRequest($expiration = '+ 10 minutes')
241:     {
242:         return $this->presenter->storeRequest($expiration);
243:     }
244: 
245:     /** @deprecated */
246:     function restoreRequest($key)
247:     {
248:         return $this->presenter->restoreRequest($key);
249:     }
250: 
251: }
252: 
Nette Framework 2.0.13 (for PHP 5.2, prefixed) API API documentation generated by ApiGen 2.8.0