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