Source for file Application.php

Documentation is available at Application.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  7. 7:  *
  8. 8:  * This source file is subject to the "Nette license" that is bundled
  9. 9:  * with this package in the file license.txt.
  10. 10:  *
  11. 11:  * For more information please see https://nette.org
  12. 12:  *
  13. 13:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  14. 14:  * @license    https://nette.org/license  Nette license
  15. 15:  * @link       https://nette.org
  16. 16:  * @category   Nette
  17. 17:  * @package    Nette\Application
  18. 18:  * @version    $Id$
  19. 19:  */
  20. 20:  
  21. 21:  
  22. 22:  
  23. 23: require_once dirname(__FILE__'/../Object.php';
  24. 24:  
  25. 25: require_once dirname(__FILE__'/../Application/ApplicationException.php';
  26. 26:  
  27. 27:  
  28. 28:  
  29. 29: /**
  30. 30:  * Front Controller.
  31. 31:  *
  32. 32:  * @author     David Grudl
  33. 33:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  34. 34:  * @package    Nette\Application
  35. 35:  */
  36. 36: class Application extends Object
  37. 37: {
  38. 38:     /** @var int */
  39. 39:     public static $maxLoop 20;
  40. 40:  
  41. 41:     /** @var array */
  42. 42:     public $defaultServices = array(
  43. 43:         'Nette\Application\IRouter' => 'Nette\Application\MultiRouter',
  44. 44:         'Nette\Application\IPresenterLoader' => 'Nette\Application\PresenterLoader',
  45. 45:     );
  46. 46:  
  47. 47:     /** @var bool enable fault barrier? */
  48. 48:     public $catchExceptions;
  49. 49:  
  50. 50:     /** @var string */
  51. 51:     public $errorPresenter;
  52. 52:  
  53. 53:     /** @var array of function(Application $sender) */
  54. 54:     public $onStartup;
  55. 55:  
  56. 56:     /** @var array of function(Application $sender, \Exception $e = NULL) */
  57. 57:     public $onShutdown;
  58. 58:  
  59. 59:     /** @var array of function(Application $sender, PresenterRequest $request) */
  60. 60:     public $onRequest;
  61. 61:  
  62. 62:     /** @var array of function(Application $sender, \Exception $e) */
  63. 63:     public $onError;
  64. 64:  
  65. 65:     /** @var array of string */
  66. 66:     public $allowedMethods = array('GET''POST''HEAD');
  67. 67:  
  68. 68:     /** @var array of PresenterRequest */
  69. 69:     private $requests array();
  70. 70:  
  71. 71:     /** @var Presenter */
  72. 72:     private $presenter;
  73. 73:  
  74. 74:     /** @var ServiceLocator */
  75. 75:     private $serviceLocator;
  76. 76:  
  77. 77:  
  78. 78:  
  79. 79:     /**
  80. 80:      * Dispatch a HTTP request to a front controller.
  81. 81:      */
  82. 82:     public function run()
  83. 83:     {
  84. 84:         $httpRequest $this->getHttpRequest();
  85. 85:         $httpResponse $this->getHttpResponse();
  86. 86:  
  87. 87:         $httpRequest->setEncoding('UTF-8');
  88. 88:         $httpResponse->setHeader('X-Powered-By''Nette Framework');
  89. 89:  
  90. 90:         if (Environment::getVariable('baseUri'=== NULL{
  91. 91:             Environment::setVariable('baseUri'$httpRequest->getUri()->basePath);
  92. 92:         }
  93. 93:  
  94. 94:         // check HTTP method
  95. 95:         $method $httpRequest->getMethod();
  96. 96:         if ($this->allowedMethods{
  97. 97:             if (!in_array($method$this->allowedMethodsTRUE)) {
  98. 98:                 $httpResponse->setCode(IHttpResponse::S501_NOT_IMPLEMENTED);
  99. 99:                 $httpResponse->setHeader('Allow'implode(','$this->allowedMethods));
  100. 100:                 $method htmlSpecialChars($method);
  101. 101:                 die("<h1>Method $method is not implemented</h1>");
  102. 102:             }
  103. 103:         }
  104. 104:  
  105. 105:         // dispatching
  106. 106:         $request NULL;
  107. 107:         $hasError FALSE;
  108. 108:         do {
  109. 109:             try {
  110. 110:                 if (count($this->requestsself::$maxLoop{
  111. 111:                     throw new ApplicationException('Too many loops detected in application life cycle.');
  112. 112:                 }
  113. 113:  
  114. 114:                 if (!$request{
  115. 115:                     $this->onStartup($this);
  116. 116:  
  117. 117:                     // default router
  118. 118:                     $router $this->getRouter();
  119. 119:                     if ($router instanceof MultiRouter && !count($router)) {
  120. 120:                         $router[new SimpleRouter(array(
  121. 121:                             'presenter' => 'Default',
  122. 122:                             'action' => 'default',
  123. 123:                         ));
  124. 124:                     }
  125. 125:  
  126. 126:                     // routing
  127. 127:                     $request $router->match($httpRequest);
  128. 128:                     if (!($request instanceof PresenterRequest)) {
  129. 129:                         $request NULL;
  130. 130:                         throw new BadRequestException('No route for HTTP request.');
  131. 131:                     }
  132. 132:  
  133. 133:                     if (strcasecmp($request->getPresenterName()$this->errorPresenter=== 0{
  134. 134:                         throw new BadRequestException('Invalid request.');
  135. 135:                     }
  136. 136:                 }
  137. 137:  
  138. 138:                 $this->requests[$request;
  139. 139:                 $this->onRequest($this$request);
  140. 140:  
  141. 141:                 // Instantiate presenter
  142. 142:                 $presenter $request->getPresenterName();
  143. 143:                 try {
  144. 144:                     $class $this->getPresenterLoader()->getPresenterClass($presenter);
  145. 145:                     $request->modify('name'$presenter);
  146. 146:                 catch (InvalidPresenterException $e{
  147. 147:                     throw new BadRequestException($e->getMessage()404$e);
  148. 148:                 }
  149. 149:                 $this->presenter new $class($request);
  150. 150:  
  151. 151:                 // Instantiate topmost service locator
  152. 152:                 $this->presenter->setServiceLocator(new ServiceLocator($this->serviceLocator));
  153. 153:  
  154. 154:                 // Execute presenter
  155. 155:                 $this->presenter->run();
  156. 156:                 break;
  157. 157:  
  158. 158:             catch (RedirectingException $e{
  159. 159:                 // not error, presenter redirects to new URL
  160. 160:                 $httpResponse->redirect($e->getUri()$e->getCode());
  161. 161:                 break;
  162. 162:  
  163. 163:             catch (ForwardingException $e{
  164. 164:                 // not error, presenter forwards to new request
  165. 165:                 $request $e->getRequest();
  166. 166:  
  167. 167:             catch (AbortException $e{
  168. 168:                 // not error, application is correctly terminated
  169. 169:                 unset($e);
  170. 170:                 break;
  171. 171:  
  172. 172:             catch (Exception $e{
  173. 173:                 // fault barrier
  174. 174:                 if ($this->catchExceptions === NULL{
  175. 175:                     $this->catchExceptions = Environment::isProduction();
  176. 176:                 }
  177. 177:  
  178. 178:                 if (!$this->catchExceptions{
  179. 179:                     throw $e;
  180. 180:                 }
  181. 181:  
  182. 182:                 $this->onError($this$e);
  183. 183:  
  184. 184:                 if ($hasError{
  185. 185:                     $e new ApplicationException('An error occured while executing error-presenter'0$e);
  186. 186:  
  187. 187:                 elseif ($this->errorPresenter{
  188. 188:                     $hasError TRUE;
  189. 189:                     $request new PresenterRequest(
  190. 190:                         $this->errorPresenter,
  191. 191:                         PresenterRequest::FORWARD,
  192. 192:                         array('exception' => $e)
  193. 193:                     );
  194. 194:                     continue;
  195. 195:                 }
  196. 196:  
  197. 197:                 if ($e instanceof BadRequestException{
  198. 198:                     if (!$httpResponse->isSent()) {
  199. 199:                         $httpResponse->setCode($e->getCode());
  200. 200:                     }
  201. 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>";
  202. 202:                     break;
  203. 203:  
  204. 204:                 else {
  205. 205:                     if (!$httpResponse->isSent()) {
  206. 206:                         $httpResponse->setCode(500);
  207. 207:                     }
  208. 208:                     Debug::processException($eFALSE);
  209. 209:                     echo "<title>500 Internal Server Error</title>\n\n<h1>Server Error</h1>\n\n",
  210. 210:                         "<p>The server encountered an internal error and was unable to complete your request. Please try again later.</p>";
  211. 211:                     break;
  212. 212:                 }
  213. 213:             }
  214. 214:         while (1);
  215. 215:  
  216. 216:         $this->onShutdown($thisisset($e$e NULL);
  217. 217:     }
  218. 218:  
  219. 219:  
  220. 220:  
  221. 221:     /**
  222. 222:      * Returns all processed requests.
  223. 223:      * @return array of PresenterRequest
  224. 224:      */
  225. 225:     final public function getRequests()
  226. 226:     {
  227. 227:         return $this->requests;
  228. 228:     }
  229. 229:  
  230. 230:  
  231. 231:  
  232. 232:     /**
  233. 233:      * Returns current presenter.
  234. 234:      * @return Presenter 
  235. 235:      */
  236. 236:     final public function getPresenter()
  237. 237:     {
  238. 238:         return $this->presenter;
  239. 239:     }
  240. 240:  
  241. 241:  
  242. 242:  
  243. 243:     /********************* services ****************d*g**/
  244. 244:  
  245. 245:  
  246. 246:  
  247. 247:     /**
  248. 248:      * Gets the service locator (experimental).
  249. 249:      * @return IServiceLocator 
  250. 250:      */
  251. 251:     final public function getServiceLocator()
  252. 252:     {
  253. 253:         if ($this->serviceLocator === NULL{
  254. 254:             $this->serviceLocator new ServiceLocator(Environment::getServiceLocator());
  255. 255:  
  256. 256:             foreach ($this->defaultServices as $name => $service{
  257. 257:                 if ($this->serviceLocator->getService($nameFALSE=== NULL{
  258. 258:                     $this->serviceLocator->addService($service$name);
  259. 259:                 }
  260. 260:             }
  261. 261:         }
  262. 262:         return $this->serviceLocator;
  263. 263:     }
  264. 264:  
  265. 265:  
  266. 266:  
  267. 267:     /**
  268. 268:      * Gets the service object of the specified type.
  269. 269:      * @param  string service name
  270. 270:      * @param  bool   throw exception if service doesn't exist?
  271. 271:      * @return mixed 
  272. 272:      */
  273. 273:     final public function getService($name$need TRUE)
  274. 274:     {
  275. 275:         return $this->getServiceLocator()->getService($name$need);
  276. 276:     }
  277. 277:  
  278. 278:  
  279. 279:  
  280. 280:     /**
  281. 281:      * Returns router.
  282. 282:      * @return IRouter 
  283. 283:      */
  284. 284:     public function getRouter()
  285. 285:     {
  286. 286:         return $this->getServiceLocator()->getService('Nette\Application\IRouter');
  287. 287:     }
  288. 288:  
  289. 289:  
  290. 290:  
  291. 291:     /**
  292. 292:      * Changes router.
  293. 293:      * @param  IRouter 
  294. 294:      * @return void 
  295. 295:      */
  296. 296:     public function setRouter(IRouter $router)
  297. 297:     {
  298. 298:         $this->getServiceLocator()->addService($router'Nette\Application\IRouter');
  299. 299:     }
  300. 300:  
  301. 301:  
  302. 302:  
  303. 303:     /**
  304. 304:      * Returns presenter loader.
  305. 305:      * @return IPresenterLoader 
  306. 306:      */
  307. 307:     public function getPresenterLoader()
  308. 308:     {
  309. 309:         return $this->getServiceLocator()->getService('Nette\Application\IPresenterLoader');
  310. 310:     }
  311. 311:  
  312. 312:  
  313. 313:  
  314. 314:     /********************* request serialization ****************d*g**/
  315. 315:  
  316. 316:  
  317. 317:  
  318. 318:     /**
  319. 319:      * @return string 
  320. 320:      */
  321. 321:     public function storeRequest()
  322. 322:     {
  323. 323:         $session $this->getSession()->getNamespace('Nette.Application/requests');
  324. 324:         do {
  325. 325:             $key substr(md5(lcg_value())04);
  326. 326:         while (isset($session->$key));
  327. 327:  
  328. 328:         $session->$key end($this->requests);
  329. 329:         $session->setExpiration(10 60'requests');
  330. 330:         return $key;
  331. 331:     }
  332. 332:  
  333. 333:  
  334. 334:  
  335. 335:     /**
  336. 336:      * @param  string 
  337. 337:      * @return void 
  338. 338:      */
  339. 339:     public function restoreRequest($key)
  340. 340:     {
  341. 341:         $session $this->getSession()->getNamespace('Nette.Application/requests');
  342. 342:         if (isset($session->$key)) {
  343. 343:             $request $session->$key;
  344. 344:             unset($session->$key);
  345. 345:             throw new ForwardingException($request);
  346. 346:         }
  347. 347:     }
  348. 348:  
  349. 349:  
  350. 350:  
  351. 351:     /********************* backend ****************d*g**/
  352. 352:  
  353. 353:  
  354. 354:  
  355. 355:     /**
  356. 356:      * @return IHttpRequest 
  357. 357:      */
  358. 358:     protected function getHttpRequest()
  359. 359:     {
  360. 360:         return Environment::getHttpRequest();
  361. 361:     }
  362. 362:  
  363. 363:  
  364. 364:  
  365. 365:     /**
  366. 366:      * @return IHttpResponse 
  367. 367:      */
  368. 368:     protected function getHttpResponse()
  369. 369:     {
  370. 370:         return Environment::getHttpResponse();
  371. 371:     }
  372. 372:  
  373. 373:  
  374. 374:  
  375. 375:     /**
  376. 376:      * @return Session 
  377. 377:      */
  378. 378:     protected function getSession()
  379. 379:     {
  380. 380:         return Environment::getSession();
  381. 381:     }
  382. 382: