Packages

  • Nette
    • Application
      • Application\Diagnostics
      • Application\Responses
      • Application\Routers
      • Application\UI
    • Caching
      • Caching\Storages
    • ComponentModel
    • Config
    • Database
      • Database\Diagnostics
      • Database\Drivers
      • Database\Reflection
      • Database\Table
    • DI
    • Diagnostics
    • Forms
      • Forms\Controls
      • Forms\Rendering
    • Http
    • Iterators
    • Latte
      • Latte\Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
    • Templating
    • Utils
  • 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, 2011 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:  * @package Nette\Application
 20:  */
 21: class NApplication extends NObject
 22: {
 23:     /** @var int */
 24:     public static $maxLoop = 20;
 25: 
 26:     /** @var bool enable fault barrier? */
 27:     public $catchExceptions;
 28: 
 29:     /** @var string */
 30:     public $errorPresenter;
 31: 
 32:     /** @var array of function(Application $sender); Occurs before the application loads presenter */
 33:     public $onStartup;
 34: 
 35:     /** @var array of function(Application $sender, Exception $e = NULL); Occurs before the application shuts down */
 36:     public $onShutdown;
 37: 
 38:     /** @var array of function(Application $sender, Request $request); Occurs when a new request is ready for dispatch */
 39:     public $onRequest;
 40: 
 41:     /** @var array of function(Application $sender, IResponse $response); Occurs when a new response is received */
 42:     public $onResponse;
 43: 
 44:     /** @var array of function(Application $sender, Exception $e); Occurs when an unhandled exception occurs in the application */
 45:     public $onError;
 46: 
 47:     /** @var array of string */
 48:     public $allowedMethods = array('GET', 'POST', 'HEAD', 'PUT', 'DELETE');
 49: 
 50:     /** @var array of NPresenterRequest */
 51:     private $requests = array();
 52: 
 53:     /** @var IPresenter */
 54:     private $presenter;
 55: 
 56:     /** @var IDIContainer */
 57:     private $context;
 58: 
 59: 
 60: 
 61:     public function __construct(IDIContainer $context)
 62:     {
 63:         $this->context = $context;
 64:     }
 65: 
 66: 
 67: 
 68:     /**
 69:      * Dispatch a HTTP request to a front controller.
 70:      * @return void
 71:      */
 72:     public function run()
 73:     {
 74:         $httpRequest = $this->context->httpRequest;
 75:         $httpResponse = $this->context->httpResponse;
 76: 
 77:         // check HTTP method
 78:         if ($this->allowedMethods) {
 79:             $method = $httpRequest->getMethod();
 80:             if (!in_array($method, $this->allowedMethods, TRUE)) {
 81:                 $httpResponse->setCode(IHttpResponse::S501_NOT_IMPLEMENTED);
 82:                 $httpResponse->setHeader('Allow', implode(',', $this->allowedMethods));
 83:                 echo '<h1>Method ' . htmlSpecialChars($method) . ' is not implemented</h1>';
 84:                 return;
 85:             }
 86:         }
 87: 
 88:         // dispatching
 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:                     // routing
101:                     $router = $this->getRouter();
102: 
103:                     // enable routing debugger
104:                     NRoutingDebugger::initialize($this, $httpRequest);
105: 
106:                     $request = $router->match($httpRequest);
107:                     if (!$request instanceof NPresenterRequest) {
108:                         $request = NULL;
109:                         throw new NBadRequestException('No route for HTTP request.');
110:                     }
111: 
112:                     if (strcasecmp($request->getPresenterName(), $this->errorPresenter) === 0) {
113:                         throw new NBadRequestException('Invalid request. Presenter is not achievable.');
114:                     }
115:                 }
116: 
117:                 $this->requests[] = $request;
118:                 $this->onRequest($this, $request);
119: 
120:                 // Instantiate presenter
121:                 $presenterName = $request->getPresenterName();
122:                 try {
123:                     $this->presenter = $this->getPresenterFactory()->createPresenter($presenterName);
124:                 } catch (NInvalidPresenterException $e) {
125:                     throw new NBadRequestException($e->getMessage(), 404, $e);
126:                 }
127: 
128:                 $this->getPresenterFactory()->getPresenterClass($presenterName);
129:                 $request->setPresenterName($presenterName);
130:                 $request->freeze();
131: 
132:                 // Execute presenter
133:                 $response = $this->presenter->run($request);
134:                 $this->onResponse($this, $response);
135: 
136:                 // Send response
137:                 if ($response instanceof NForwardResponse) {
138:                     $request = $response->getRequest();
139:                     continue;
140: 
141:                 } elseif ($response instanceof IPresenterResponse) {
142:                     $response->send($httpRequest, $httpResponse);
143:                 }
144:                 break;
145: 
146:             } catch (Exception $e) {
147:                 // fault barrier
148:                 $this->onError($this, $e);
149: 
150:                 if (!$this->catchExceptions) {
151:                     $this->onShutdown($this, $e);
152:                     throw $e;
153:                 }
154: 
155:                 if ($repeatedError) {
156:                     $e = new NApplicationException('An error occurred while executing error-presenter', 0, $e);
157:                 }
158: 
159:                 if (!$httpResponse->isSent()) {
160:                     $httpResponse->setCode($e instanceof NBadRequestException ? $e->getCode() : 500);
161:                 }
162: 
163:                 if (!$repeatedError && $this->errorPresenter) {
164:                     $repeatedError = TRUE;
165:                     if ($this->presenter instanceof NPresenter) {
166:                         try {
167:                             $this->presenter->forward(":$this->errorPresenter:", array('exception' => $e));
168:                         } catch (NAbortException $foo) {
169:                             $request = $this->presenter->getLastCreatedRequest();
170:                         }
171:                     } else {
172:                         $request = new NPresenterRequest(
173:                             $this->errorPresenter,
174:                             NPresenterRequest::FORWARD,
175:                             array('exception' => $e)
176:                         );
177:                     }
178:                     // continue
179: 
180:                 } else { // default error handler
181:                     if ($e instanceof NBadRequestException) {
182:                         $code = $e->getCode();
183:                     } else {
184:                         $code = 500;
185:                         NDebugger::log($e, NDebugger::ERROR);
186:                     }
187:                     require dirname(__FILE__) . '/templates/error.phtml';
188:                     break;
189:                 }
190:             }
191:         } while (1);
192: 
193:         $this->onShutdown($this, isset($e) ? $e : NULL);
194:     }
195: 
196: 
197: 
198:     /**
199:      * Returns all processed requests.
200:      * @return array of Request
201:      */
202:     final public function getRequests()
203:     {
204:         return $this->requests;
205:     }
206: 
207: 
208: 
209:     /**
210:      * Returns current presenter.
211:      * @return IPresenter
212:      */
213:     final public function getPresenter()
214:     {
215:         return $this->presenter;
216:     }
217: 
218: 
219: 
220:     /********************* services ****************d*g**/
221: 
222: 
223: 
224:     /**
225:      * @internal
226:      */
227:     protected function getContext()
228:     {
229:         return $this->context;
230:     }
231: 
232: 
233: 
234:     /**
235:      * Returns router.
236:      * @return IRouter
237:      */
238:     public function getRouter()
239:     {
240:         return $this->context->router;
241:     }
242: 
243: 
244: 
245:     /**
246:      * Returns presenter factory.
247:      * @return IPresenterFactory
248:      */
249:     public function getPresenterFactory()
250:     {
251:         return $this->context->presenterFactory;
252:     }
253: 
254: 
255: 
256:     /********************* request serialization ****************d*g**/
257: 
258: 
259: 
260:     /**
261:      * Stores current request to session.
262:      * @param  mixed  optional expiration time
263:      * @return string key
264:      */
265:     public function storeRequest($expiration = '+ 10 minutes')
266:     {
267:         $session = $this->context->session->getSection('Nette.Application/requests');
268:         do {
269:             $key = NStrings::random(5);
270:         } while (isset($session[$key]));
271: 
272:         $session[$key] = end($this->requests);
273:         $session->setExpiration($expiration, $key);
274:         return $key;
275:     }
276: 
277: 
278: 
279:     /**
280:      * Restores current request to session.
281:      * @param  string key
282:      * @return void
283:      */
284:     public function restoreRequest($key)
285:     {
286:         $session = $this->context->session->getSection('Nette.Application/requests');
287:         if (isset($session[$key])) {
288:             $request = clone $session[$key];
289:             unset($session[$key]);
290:             $request->setFlag(NPresenterRequest::RESTORED, TRUE);
291:             $this->presenter->sendResponse(new NForwardResponse($request));
292:         }
293:     }
294: 
295: }
296: 
Nette Framework 2.0beta1 (for PHP 5.2) API API documentation generated by ApiGen 2.3.0