Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Diagnostics
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
      • Diagnostics
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • PhpGenerator
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
  • NetteModule
  • none

Classes

  • Application
  • PresenterFactory
  • Request

Interfaces

  • IPresenter
  • IPresenterFactory
  • IResponse
  • IRouter

Exceptions

  • AbortException
  • ApplicationException
  • BadRequestException
  • ForbiddenRequestException
  • InvalidPresenterException
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Other releases
  • Nette homepage
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://nette.org)
  5:  * Copyright (c) 2004 David Grudl (https://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 array of function (Application $sender); Occurs before the application loads presenter */
 35:     public $onStartup;
 36: 
 37:     /** @var array of function (Application $sender, \Exception $e = NULL); Occurs before the application shuts down */
 38:     public $onShutdown;
 39: 
 40:     /** @var array of function (Application $sender, Request $request); Occurs when a new request is received */
 41:     public $onRequest;
 42: 
 43:     /** @var array of function (Application $sender, Presenter $presenter); Occurs when a presenter is created */
 44:     public $onPresenter;
 45: 
 46:     /** @var array of function (Application $sender, IResponse $response); Occurs when a new response is ready for dispatch */
 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:     /** @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:             $request->setPresenterName($name);
127:         } catch (InvalidPresenterException $e) {
128:             throw new BadRequestException($e->getMessage(), 0, $e);
129:         }
130: 
131:         return $request;
132:     }
133: 
134: 
135:     /**
136:      * @return void
137:      */
138:     public function processRequest(Request $request)
139:     {
140:         if (count($this->requests) > self::$maxLoop) {
141:             throw new ApplicationException('Too many loops detected in application life cycle.');
142:         }
143: 
144:         $this->requests[] = $request;
145:         $this->onRequest($this, $request);
146: 
147:         $this->presenter = $this->presenterFactory->createPresenter($request->getPresenterName());
148:         $this->onPresenter($this, $this->presenter);
149:         $response = $this->presenter->run($request);
150: 
151:         if ($response instanceof Responses\ForwardResponse) {
152:             $this->processRequest($response->getRequest());
153: 
154:         } elseif ($response) {
155:             $this->onResponse($this, $response);
156:             $response->send($this->httpRequest, $this->httpResponse);
157:         }
158:     }
159: 
160: 
161:     /**
162:      * @return void
163:      */
164:     public function processException(\Exception $e)
165:     {
166:         if (!$e instanceof BadRequestException && $this->httpResponse instanceof Nette\Http\Response) {
167:             $this->httpResponse->warnOnBuffer = FALSE;
168:         }
169:         if (!$this->httpResponse->isSent()) {
170:             $this->httpResponse->setCode($e instanceof BadRequestException ? ($e->getCode() ?: 404) : 500);
171:         }
172: 
173:         $args = array('exception' => $e, 'request' => end($this->requests) ?: NULL);
174:         if ($this->presenter instanceof UI\Presenter) {
175:             try {
176:                 $this->presenter->forward(":$this->errorPresenter:", $args);
177:             } catch (AbortException $foo) {
178:                 $this->processRequest($this->presenter->getLastCreatedRequest());
179:             }
180:         } else {
181:             $this->processRequest(new Request($this->errorPresenter, Request::FORWARD, $args));
182:         }
183:     }
184: 
185: 
186:     /**
187:      * Returns all processed requests.
188:      * @return Request[]
189:      */
190:     public function getRequests()
191:     {
192:         return $this->requests;
193:     }
194: 
195: 
196:     /**
197:      * Returns current presenter.
198:      * @return IPresenter
199:      */
200:     public function getPresenter()
201:     {
202:         return $this->presenter;
203:     }
204: 
205: 
206:     /********************* services ****************d*g**/
207: 
208: 
209:     /**
210:      * Returns router.
211:      * @return IRouter
212:      */
213:     public function getRouter()
214:     {
215:         return $this->router;
216:     }
217: 
218: 
219:     /**
220:      * Returns presenter factory.
221:      * @return IPresenterFactory
222:      */
223:     public function getPresenterFactory()
224:     {
225:         return $this->presenterFactory;
226:     }
227: 
228: 
229:     /********************* request serialization ****************d*g**/
230: 
231: 
232:     /** @deprecated */
233:     function storeRequest($expiration = '+ 10 minutes')
234:     {
235:         trigger_error(__METHOD__ . '() is deprecated; use $presenter->storeRequest() instead.', E_USER_DEPRECATED);
236:         return $this->presenter->storeRequest($expiration);
237:     }
238: 
239:     /** @deprecated */
240:     function restoreRequest($key)
241:     {
242:         trigger_error(__METHOD__ . '() is deprecated; use $presenter->restoreRequest() instead.', E_USER_DEPRECATED);
243:         return $this->presenter->restoreRequest($key);
244:     }
245: 
246: }
247: 
Nette 2.1 API documentation generated by ApiGen 2.8.0