Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationDI
      • ApplicationLatte
      • ApplicationTracy
      • CacheDI
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsDI
      • FormsLatte
      • Framework
      • HttpDI
      • HttpTracy
      • MailDI
      • ReflectionDI
      • SecurityDI
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Conventions
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • none
  • Tracy
    • Bridges
      • Nette

Classes

  • Application
  • LinkGenerator
  • PresenterFactory
  • Request

Interfaces

  • IPresenter
  • IPresenterFactory
  • IResponse
  • IRouter

Exceptions

  • AbortException
  • ApplicationException
  • BadRequestException
  • ForbiddenRequestException
  • InvalidPresenterException
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://nette.org)
  5:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette\Application;
  9: 
 10: use Nette;
 11: 
 12: 
 13: /**
 14:  * Front Controller.
 15:  *
 16:  * @author     David Grudl
 17:  *
 18:  * @property-read array $requests
 19:  * @property-read IPresenter $presenter
 20:  * @property-read IRouter $router
 21:  * @property-read IPresenterFactory $presenterFactory
 22:  */
 23: class Application extends Nette\Object
 24: {
 25:     /** @var int */
 26:     public static $maxLoop = 20;
 27: 
 28:     /** @var bool enable fault barrier? */
 29:     public $catchExceptions;
 30: 
 31:     /** @var string */
 32:     public $errorPresenter;
 33: 
 34:     /** @var callable[]  function(Application $sender); Occurs before the application loads presenter */
 35:     public $onStartup;
 36: 
 37:     /** @var callable[]  function(Application $sender, \Exception $e = NULL); Occurs before the application shuts down */
 38:     public $onShutdown;
 39: 
 40:     /** @var callable[]  function(Application $sender, Request $request); Occurs when a new request is received */
 41:     public $onRequest;
 42: 
 43:     /** @var callable[]  function(Application $sender, Presenter $presenter); Occurs when a presenter is created */
 44:     public $onPresenter;
 45: 
 46:     /** @var callable[]  function(Application $sender, IResponse $response); Occurs when a new response is ready for dispatch */
 47:     public $onResponse;
 48: 
 49:     /** @var callable[]  function(Application $sender, \Exception $e); Occurs when an unhandled exception occurs in the application */
 50:     public $onError;
 51: 
 52:     /** @var Request[] */
 53:     private $requests = array();
 54: 
 55:     /** @var IPresenter */
 56:     private $presenter;
 57: 
 58:     /** @var Nette\Http\IRequest */
 59:     private $httpRequest;
 60: 
 61:     /** @var Nette\Http\IResponse */
 62:     private $httpResponse;
 63: 
 64:     /** @var IPresenterFactory */
 65:     private $presenterFactory;
 66: 
 67:     /** @var IRouter */
 68:     private $router;
 69: 
 70: 
 71:     public function __construct(IPresenterFactory $presenterFactory, IRouter $router, Nette\Http\IRequest $httpRequest, Nette\Http\IResponse $httpResponse)
 72:     {
 73:         $this->httpRequest = $httpRequest;
 74:         $this->httpResponse = $httpResponse;
 75:         $this->presenterFactory = $presenterFactory;
 76:         $this->router = $router;
 77:     }
 78: 
 79: 
 80:     /**
 81:      * Dispatch a HTTP request to a front controller.
 82:      * @return void
 83:      */
 84:     public function run()
 85:     {
 86:         try {
 87:             $this->onStartup($this);
 88:             $this->processRequest($this->createInitialRequest());
 89:             $this->onShutdown($this);
 90: 
 91:         } catch (\Exception $e) {
 92:             $this->onError($this, $e);
 93:             if ($this->catchExceptions && $this->errorPresenter) {
 94:                 try {
 95:                     $this->processException($e);
 96:                     $this->onShutdown($this, $e);
 97:                     return;
 98: 
 99:                 } catch (\Exception $e) {
100:                     $this->onError($this, $e);
101:                 }
102:             }
103:             $this->onShutdown($this, $e);
104:             throw $e;
105:         }
106:     }
107: 
108: 
109:     /**
110:      * @return Request
111:      */
112:     public function createInitialRequest()
113:     {
114:         $request = $this->router->match($this->httpRequest);
115: 
116:         if (!$request instanceof Request) {
117:             throw new BadRequestException('No route for HTTP request.');
118: 
119:         } elseif (strcasecmp($request->getPresenterName(), $this->errorPresenter) === 0) {
120:             throw new BadRequestException('Invalid request. Presenter is not achievable.');
121:         }
122: 
123:         try {
124:             $name = $request->getPresenterName();
125:             $this->presenterFactory->getPresenterClass($name);
126:         } catch (InvalidPresenterException $e) {
127:             throw new BadRequestException($e->getMessage(), 0, $e);
128:         }
129: 
130:         return $request;
131:     }
132: 
133: 
134:     /**
135:      * @return void
136:      */
137:     public function processRequest(Request $request)
138:     {
139:         if (count($this->requests) > self::$maxLoop) {
140:             throw new ApplicationException('Too many loops detected in application life cycle.');
141:         }
142: 
143:         $this->requests[] = $request;
144:         $this->onRequest($this, $request);
145: 
146:         $this->presenter = $this->presenterFactory->createPresenter($request->getPresenterName());
147:         $this->onPresenter($this, $this->presenter);
148:         $response = $this->presenter->run($request);
149: 
150:         if ($response instanceof Responses\ForwardResponse) {
151:             $this->processRequest($response->getRequest());
152: 
153:         } elseif ($response) {
154:             $this->onResponse($this, $response);
155:             $response->send($this->httpRequest, $this->httpResponse);
156:         }
157:     }
158: 
159: 
160:     /**
161:      * @return void
162:      */
163:     public function processException(\Exception $e)
164:     {
165:         if (!$e instanceof BadRequestException && $this->httpResponse instanceof Nette\Http\Response) {
166:             $this->httpResponse->warnOnBuffer = FALSE;
167:         }
168:         if (!$this->httpResponse->isSent()) {
169:             $this->httpResponse->setCode($e instanceof BadRequestException ? ($e->getCode() ?: 404) : 500);
170:         }
171: 
172:         $args = array('exception' => $e, 'request' => end($this->requests) ?: NULL);
173:         if ($this->presenter instanceof UI\Presenter) {
174:             try {
175:                 $this->presenter->forward(":$this->errorPresenter:", $args);
176:             } catch (AbortException $foo) {
177:                 $this->processRequest($this->presenter->getLastCreatedRequest());
178:             }
179:         } else {
180:             $this->processRequest(new Request($this->errorPresenter, Request::FORWARD, $args));
181:         }
182:     }
183: 
184: 
185:     /**
186:      * Returns all processed requests.
187:      * @return Request[]
188:      */
189:     public function getRequests()
190:     {
191:         return $this->requests;
192:     }
193: 
194: 
195:     /**
196:      * Returns current presenter.
197:      * @return IPresenter
198:      */
199:     public function getPresenter()
200:     {
201:         return $this->presenter;
202:     }
203: 
204: 
205:     /********************* services ****************d*g**/
206: 
207: 
208:     /**
209:      * Returns router.
210:      * @return IRouter
211:      */
212:     public function getRouter()
213:     {
214:         return $this->router;
215:     }
216: 
217: 
218:     /**
219:      * Returns presenter factory.
220:      * @return IPresenterFactory
221:      */
222:     public function getPresenterFactory()
223:     {
224:         return $this->presenterFactory;
225:     }
226: 
227: }
228: 
Nette 2.3.1 API API documentation generated by ApiGen 2.8.0