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