Source for file Application.php
Documentation is available at Application.php
6: * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
8: * This source file is subject to the "Nette license" that is bundled
9: * with this package in the file license.txt.
11: * For more information please see https://nette.org
13: * @copyright Copyright (c) 2004, 2009 David Grudl
14: * @license https://nette.org/license Nette license
15: * @link https://nette.org
17: * @package Nette\Application
23: require_once dirname(__FILE__) .
'/../Object.php';
25: require_once dirname(__FILE__) .
'/../Application/ApplicationException.php';
32: * @author David Grudl
33: * @copyright Copyright (c) 2004, 2009 David Grudl
34: * @package Nette\Application
39: public static $maxLoop =
20;
43: 'Nette\Application\IRouter' =>
'Nette\Application\MultiRouter',
44: 'Nette\Application\IPresenterLoader' =>
'Nette\Application\PresenterLoader',
47: /** @var bool enable fault barrier? */
53: /** @var array of function(Application $sender) */
56: /** @var array of function(Application $sender, \Exception $e = NULL) */
59: /** @var array of function(Application $sender, PresenterRequest $request) */
62: /** @var array of function(Application $sender, \Exception $e) */
65: /** @var array of string */
68: /** @var array of PresenterRequest */
69: private $requests =
array();
71: /** @var Presenter */
74: /** @var ServiceLocator */
75: private $serviceLocator;
80: * Dispatch a HTTP request to a front controller.
87: $httpRequest->setEncoding('UTF-8');
88: $httpResponse->setHeader('X-Powered-By', 'Nette Framework');
95: $method =
$httpRequest->getMethod();
100: $method =
htmlSpecialChars($method);
101: die("<h1>Method $method is not implemented</h1>");
110: if (count($this->requests) >
self::$maxLoop) {
111: throw new ApplicationException('Too many loops detected in application life cycle.');
115: $this->onStartup($this);
121: 'presenter' =>
'Default',
122: 'action' =>
'default',
127: $request =
$router->match($httpRequest);
138: $this->requests[] =
$request;
139: $this->onRequest($this, $request);
141: // Instantiate presenter
142: $presenter =
$request->getPresenterName();
145: $request->modify('name', $presenter);
149: $this->presenter =
new $class($request);
151: // Instantiate topmost service locator
154: // Execute presenter
159: // not error, presenter redirects to new URL
160: $httpResponse->redirect($e->getUri(), $e->getCode());
164: // not error, presenter forwards to new request
165: $request =
$e->getRequest();
168: // not error, application is correctly terminated
172: } catch (Exception $e) {
182: $this->onError($this, $e);
192: array('exception' =>
$e)
198: if (!$httpResponse->isSent()) {
199: $httpResponse->setCode($e->getCode());
201: echo "<title>404 Not Found</title>\n\n<h1>Not Found</h1>\n\n<p>The requested URL was not found on this server.</p>";
205: if (!$httpResponse->isSent()) {
206: $httpResponse->setCode(500);
209: echo "<title>500 Internal Server Error</title>\n\n<h1>Server Error</h1>\n\n",
210: "<p>The server encountered an internal error and was unable to complete your request. Please try again later.</p>";
216: $this->onShutdown($this, isset($e) ?
$e :
NULL);
222: * Returns all processed requests.
223: * @return array of PresenterRequest
227: return $this->requests;
233: * Returns current presenter.
238: return $this->presenter;
243: /********************* services ****************d*g**/
248: * Gets the service locator (experimental).
249: * @return IServiceLocator
253: if ($this->serviceLocator ===
NULL) {
257: if ($this->serviceLocator->getService($name, FALSE) ===
NULL) {
258: $this->serviceLocator->addService($service, $name);
262: return $this->serviceLocator;
268: * Gets the service object of the specified type.
269: * @param string service name
270: * @param bool throw exception if service doesn't exist?
304: * Returns presenter loader.
305: * @return IPresenterLoader
314: /********************* request serialization ****************d*g**/
323: $session =
$this->getSession()->getNamespace('Nette.Application/requests');
326: } while (isset($session->$key));
328: $session->$key =
end($this->requests);
329: $session->setExpiration(10 *
60, 'requests');
341: $session =
$this->getSession()->getNamespace('Nette.Application/requests');
342: if (isset($session->$key)) {
343: $request =
$session->$key;
344: unset($session->$key);
351: /********************* backend ****************d*g**/
356: * @return IHttpRequest
366: * @return IHttpResponse