Source for file Environment.php

Documentation is available at Environment.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * @copyright  Copyright (c) 2004, 2010 David Grudl
  7. 7:  * @license    http://nettephp.com/license  Nette license
  8. 8:  * @link       http://nettephp.com
  9. 9:  * @category   Nette
  10. 10:  * @package    Nette
  11. 11:  */
  12. 12:  
  13. 13:  
  14. 14:  
  15. 15: /**
  16. 16:  * Nette environment and configuration.
  17. 17:  *
  18. 18:  * @copyright  Copyright (c) 2004, 2010 David Grudl
  19. 19:  * @package    Nette
  20. 20:  */
  21. 21: final class Environment
  22. 22: {
  23. 23:     /**#@+ environment name */
  24. 24:     const DEVELOPMENT 'development';
  25. 25:     const PRODUCTION 'production';
  26. 26:     const CONSOLE 'console';
  27. 27:     const LAB 'lab';
  28. 28:     /**#@-*/
  29. 29:  
  30. 30:     /**#@+ mode name */
  31. 31:     const DEBUG 'debug';
  32. 32:     const PERFORMANCE 'performance';
  33. 33:     /**#@-*/
  34. 34:  
  35. 35:     /** @var Configurator */
  36. 36:     private static $configurator;
  37. 37:  
  38. 38:     /** @var string  the mode of current application */
  39. 39:     private static $mode array();
  40. 40:  
  41. 41:     /** @var ArrayObject */
  42. 42:     private static $config;
  43. 43:  
  44. 44:     /** @var IServiceLocator */
  45. 45:     private static $serviceLocator;
  46. 46:  
  47. 47:     /** @var array */
  48. 48:     private static $vars array(
  49. 49:         'encoding' => array('UTF-8'FALSE),
  50. 50:         'lang' => array('en'FALSE),
  51. 51:         'cacheBase' => array('%tempDir%'TRUE)// deprecated
  52. 52:         'tempDir' => array('%appDir%/temp'TRUE),
  53. 53:         'logDir' => array('%appDir%/log'TRUE),
  54. 54:     );
  55. 55:  
  56. 56:     /** @var array */
  57. 57:     private static $aliases array(
  58. 58:         'getHttpContext' => 'Nette\Web\HttpContext',
  59. 59:         'getHttpRequest' => 'Nette\Web\IHttpRequest',
  60. 60:         'getHttpResponse' => 'Nette\Web\IHttpResponse',
  61. 61:         'getApplication' => 'Nette\Application\Application',
  62. 62:         'getUser' => 'Nette\Web\IUser',
  63. 63:     );
  64. 64:  
  65. 65:  
  66. 66:  
  67. 67:     /**
  68. 68:      * Static class - cannot be instantiated.
  69. 69:      */
  70. 70:     final public function __construct()
  71. 71:     {
  72. 72:         throw new LogicException("Cannot instantiate static class " get_class($this));
  73. 73:     }
  74. 74:  
  75. 75:  
  76. 76:  
  77. 77:     /**
  78. 78:      * Sets "class behind Environment" configurator.
  79. 79:      * @param  Configurator 
  80. 80:      * @return void 
  81. 81:      */
  82. 82:     public static function setConfigurator(Configurator $configurator)
  83. 83:     {
  84. 84:         self::$configurator $configurator;
  85. 85:     }
  86. 86:  
  87. 87:  
  88. 88:  
  89. 89:     /**
  90. 90:      * Gets "class behind Environment" configurator.
  91. 91:      * @return Configurator 
  92. 92:      */
  93. 93:     public static function getConfigurator()
  94. 94:     {
  95. 95:         if (self::$configurator === NULL{
  96. 96:             self::$configurator new Configurator;
  97. 97:         }
  98. 98:         return self::$configurator;
  99. 99:     }
  100. 100:  
  101. 101:  
  102. 102:  
  103. 103:     /********************* environment name and modes ****************d*g**/
  104. 104:  
  105. 105:  
  106. 106:  
  107. 107:     /**
  108. 108:      * Sets the current environment name.
  109. 109:      * @param  string 
  110. 110:      * @return void 
  111. 111:      * @throws InvalidStateException
  112. 112:      */
  113. 113:     public static function setName($name)
  114. 114:     {
  115. 115:         if (!isset(self::$vars['environment'])) {
  116. 116:             self::setVariable('environment'$nameFALSE);
  117. 117:  
  118. 118:         else {
  119. 119:             throw new InvalidStateException('Environment name has been already set.');
  120. 120:         }
  121. 121:     }
  122. 122:  
  123. 123:  
  124. 124:  
  125. 125:     /**
  126. 126:      * Returns the current environment name.
  127. 127:      * @return string 
  128. 128:      */
  129. 129:     public static function getName()
  130. 130:     {
  131. 131:         $name self::getVariable('environment');
  132. 132:         if ($name === NULL{
  133. 133:             $name self::getConfigurator()->detect('environment');
  134. 134:             self::setVariable('environment'$nameFALSE);
  135. 135:         }
  136. 136:         return $name;
  137. 137:     }
  138. 138:  
  139. 139:  
  140. 140:  
  141. 141:     /**
  142. 142:      * Sets the mode.
  143. 143:      *
  144. 144:      * @param  string mode identifier
  145. 145:      * @param  bool   set or unser
  146. 146:      * @return void 
  147. 147:      */
  148. 148:     public static function setMode($mode$value TRUE)
  149. 149:     {
  150. 150:         self::$mode[$mode= (bool) $value;
  151. 151:     }
  152. 152:  
  153. 153:  
  154. 154:  
  155. 155:     /**
  156. 156:      * Returns the mode.
  157. 157:      *
  158. 158:      * @param  string mode identifier
  159. 159:      * @return bool 
  160. 160:      */
  161. 161:     public static function getMode($mode)
  162. 162:     {
  163. 163:         if (isset(self::$mode[$mode])) {
  164. 164:             return self::$mode[$mode];
  165. 165:  
  166. 166:         else {
  167. 167:             return self::$mode[$modeself::getConfigurator()->detect($mode);
  168. 168:         }
  169. 169:     }
  170. 170:  
  171. 171:  
  172. 172:  
  173. 173:     /**
  174. 174:      * Detects console (non-HTTP) mode.
  175. 175:      * @return bool 
  176. 176:      */
  177. 177:     public static function isConsole()
  178. 178:     {
  179. 179:         return self::getMode('console');
  180. 180:     }
  181. 181:  
  182. 182:  
  183. 183:  
  184. 184:     /**
  185. 185:      * Determines whether a server is running in production mode.
  186. 186:      * @return bool 
  187. 187:      */
  188. 188:     public static function isProduction()
  189. 189:     {
  190. 190:         return self::getMode('production');
  191. 191:     }
  192. 192:  
  193. 193:  
  194. 194:  
  195. 195:     /**
  196. 196:      * @deprecated
  197. 197:      */
  198. 198:     public static function isDebugging()
  199. 199:     {
  200. 200:         throw new DeprecatedException;
  201. 201:     }
  202. 202:  
  203. 203:  
  204. 204:  
  205. 205:     /********************* environment variables ****************d*g**/
  206. 206:  
  207. 207:  
  208. 208:  
  209. 209:     /**
  210. 210:      * Sets the environment variable.
  211. 211:      * @param  string 
  212. 212:      * @param  mixed 
  213. 213:      * @param  bool 
  214. 214:      * @return void 
  215. 215:      */
  216. 216:     public static function setVariable($name$value$expand TRUE)
  217. 217:     {
  218. 218:         if (!is_string($value)) {
  219. 219:             $expand FALSE;
  220. 220:         }
  221. 221:         self::$vars[$namearray($value(bool) $expand);
  222. 222:     }
  223. 223:  
  224. 224:  
  225. 225:  
  226. 226:     /**
  227. 227:      * Returns the value of an environment variable or $default if there is no element set.
  228. 228:      * @param  string 
  229. 229:      * @param  mixed  default value to use if key not found
  230. 230:      * @return mixed 
  231. 231:      * @throws InvalidStateException
  232. 232:      */
  233. 233:     public static function getVariable($name$default NULL)
  234. 234:     {
  235. 235:         if (isset(self::$vars[$name])) {
  236. 236:             list($var$expself::$vars[$name];
  237. 237:             if ($exp{
  238. 238:                 $var self::expand($var);
  239. 239:                 self::$vars[$namearray($varFALSE);
  240. 240:             }
  241. 241:             return $var;
  242. 242:  
  243. 243:         else {
  244. 244:             // convert from camelCaps (or PascalCaps) to ALL_CAPS
  245. 245:             $const strtoupper(preg_replace('#(.)([A-Z]+)#''$1_$2'$name));
  246. 246:             $list get_defined_constants(TRUE);
  247. 247:             if (isset($list['user'][$const])) {
  248. 248:                 self::$vars[$namearray($list['user'][$const]FALSE);
  249. 249:                 return $list['user'][$const];
  250. 250:  
  251. 251:             else {
  252. 252:                 return $default;
  253. 253:             }
  254. 254:         }
  255. 255:     }
  256. 256:  
  257. 257:  
  258. 258:  
  259. 259:     /**
  260. 260:      * Returns the all environment variables.
  261. 261:      * @return array 
  262. 262:      */
  263. 263:     public static function getVariables()
  264. 264:     {
  265. 265:         $res array();
  266. 266:         foreach (self::$vars as $name => $foo{
  267. 267:             $res[$nameself::getVariable($name);
  268. 268:         }
  269. 269:         return $res;
  270. 270:     }
  271. 271:  
  272. 272:  
  273. 273:  
  274. 274:     /**
  275. 275:      * Returns expanded variable.
  276. 276:      * @param  string 
  277. 277:      * @return string 
  278. 278:      * @throws InvalidStateException
  279. 279:      */
  280. 280:     public static function expand($var)
  281. 281:     {
  282. 282:         if (is_string($var&& strpos($var'%'!== FALSE{
  283. 283:             return @preg_replace_callback('#%([a-z0-9_-]*)%#i'array(__CLASS__'expandCb')$var)// intentionally @ due PHP bug #39257
  284. 284:         }
  285. 285:         return $var;
  286. 286:     }
  287. 287:  
  288. 288:  
  289. 289:  
  290. 290:     /**
  291. 291:      * @see Environment::expand()
  292. 292:      * @param  array 
  293. 293:      * @return string 
  294. 294:      */
  295. 295:     private static function expandCb($m)
  296. 296:     {
  297. 297:         list($var$m;
  298. 298:         if ($var === ''return '%';
  299. 299:  
  300. 300:         static $livelock;
  301. 301:         if (isset($livelock[$var])) {
  302. 302:             throw new InvalidStateException("Circular reference detected for variables: "
  303. 303:                 . implode(', 'array_keys($livelock)) ".");
  304. 304:         }
  305. 305:  
  306. 306:         try {
  307. 307:             $livelock[$varTRUE;
  308. 308:             $val self::getVariable($var);
  309. 309:             unset($livelock[$var]);
  310. 310:         catch (Exception $e{
  311. 311:             $livelock array();
  312. 312:             throw $e;
  313. 313:         }
  314. 314:  
  315. 315:         if ($val === NULL{
  316. 316:             throw new InvalidStateException("Unknown environment variable '$var'.");
  317. 317:  
  318. 318:         elseif (!is_scalar($val)) {
  319. 319:             throw new InvalidStateException("Environment variable '$var' is not scalar.");
  320. 320:         }
  321. 321:  
  322. 322:         return $val;
  323. 323:     }
  324. 324:  
  325. 325:  
  326. 326:  
  327. 327:     /********************* service locator ****************d*g**/
  328. 328:  
  329. 329:  
  330. 330:  
  331. 331:     /**
  332. 332:      * Get initial instance of service locator.
  333. 333:      * @return IServiceLocator 
  334. 334:      */
  335. 335:     public static function getServiceLocator()
  336. 336:     {
  337. 337:         if (self::$serviceLocator === NULL{
  338. 338:             self::$serviceLocator self::getConfigurator()->createServiceLocator();
  339. 339:         }
  340. 340:         return self::$serviceLocator;
  341. 341:     }
  342. 342:  
  343. 343:  
  344. 344:  
  345. 345:     /**
  346. 346:      * Gets the service object of the specified type.
  347. 347:      * @param  string service name
  348. 348:      * @param  array  options in case service is not singleton
  349. 349:      * @return object 
  350. 350:      */
  351. 351:     public static function getService($namearray $options NULL)
  352. 352:     {
  353. 353:         return self::getServiceLocator()->getService($name$options);
  354. 354:     }
  355. 355:  
  356. 356:  
  357. 357:  
  358. 358:     /**
  359. 359:      * Adds new Environment::get<Service>() method.
  360. 360:      * @param  string  service name
  361. 361:      * @param  string  alias name
  362. 362:      * @return void 
  363. 363:      */
  364. 364:     public static function setServiceAlias($service$alias)
  365. 365:     {
  366. 366:         self::$aliases['get' ucfirst($alias)$service;
  367. 367:     }
  368. 368:  
  369. 369:  
  370. 370:  
  371. 371:     /**
  372. 372:      * Calling to undefined static method.
  373. 373:      * @param  string  method name
  374. 374:      * @param  array   arguments
  375. 375:      * @return object  service 
  376. 376:      */
  377. 377:     public static function __callStatic($name$args)
  378. 378:     {
  379. 379:         if (isset(self::$aliases[$name])) {
  380. 380:             return self::getServiceLocator()->getService(self::$aliases[$name]$args);
  381. 381:         else {
  382. 382:             throw new MemberAccessException("Call to undefined static method Nette\\Environment::$name().");
  383. 383:         }
  384. 384:     }
  385. 385:  
  386. 386:  
  387. 387:  
  388. 388:     /**
  389. 389:      * @return HttpRequest 
  390. 390:      */
  391. 391:     public static function getHttpRequest()
  392. 392:     {
  393. 393:         return self::getServiceLocator()->getService(self::$aliases[__FUNCTION__]);
  394. 394:     }
  395. 395:  
  396. 396:  
  397. 397:  
  398. 398:     /**
  399. 399:      * @return HttpContext 
  400. 400:      */
  401. 401:     public static function getHttpContext()
  402. 402:     {
  403. 403:         return self::getServiceLocator()->getService(self::$aliases[__FUNCTION__]);
  404. 404:     }
  405. 405:  
  406. 406:  
  407. 407:  
  408. 408:     /**
  409. 409:      * @return HttpResponse 
  410. 410:      */
  411. 411:     public static function getHttpResponse()
  412. 412:     {
  413. 413:         return self::getServiceLocator()->getService(self::$aliases[__FUNCTION__]);
  414. 414:     }
  415. 415:  
  416. 416:  
  417. 417:  
  418. 418:     /**
  419. 419:      * @return Application 
  420. 420:      */
  421. 421:     public static function getApplication()
  422. 422:     {
  423. 423:         return self::getServiceLocator()->getService(self::$aliases[__FUNCTION__]);
  424. 424:     }
  425. 425:  
  426. 426:  
  427. 427:  
  428. 428:     /**
  429. 429:      * @return User 
  430. 430:      */
  431. 431:     public static function getUser()
  432. 432:     {
  433. 433:         return self::getServiceLocator()->getService(self::$aliases[__FUNCTION__]);
  434. 434:     }
  435. 435:  
  436. 436:  
  437. 437:  
  438. 438:     /********************* service factories ****************d*g**/
  439. 439:  
  440. 440:  
  441. 441:  
  442. 442:     /**
  443. 443:      * @param  string 
  444. 444:      * @return Cache 
  445. 445:      */
  446. 446:     public static function getCache($namespace '')
  447. 447:     {
  448. 448:         return new Cache(
  449. 449:             self::getService('Nette\Caching\ICacheStorage'),
  450. 450:             $namespace
  451. 451:         );
  452. 452:     }
  453. 453:  
  454. 454:  
  455. 455:  
  456. 456:     /**
  457. 457:      * Returns instance of session or session namespace.
  458. 458:      * @param  string 
  459. 459:      * @return Session 
  460. 460:      */
  461. 461:     public static function getSession($namespace NULL)
  462. 462:     {
  463. 463:         $handler self::getService('Nette\Web\Session');
  464. 464:         return $namespace === NULL $handler $handler->getNamespace($namespace);
  465. 465:     }
  466. 466:  
  467. 467:  
  468. 468:  
  469. 469:     /********************* global configuration ****************d*g**/
  470. 470:  
  471. 471:  
  472. 472:  
  473. 473:     /**
  474. 474:      * Loads global configuration from file and process it.
  475. 475:      * @param  string|Nette\Config\Config file name or Config object
  476. 476:      * @return ArrayObject 
  477. 477:      */
  478. 478:     public static function loadConfig($file NULL)
  479. 479:     {
  480. 480:         return self::$config self::getConfigurator()->loadConfig($file);
  481. 481:     }
  482. 482:  
  483. 483:  
  484. 484:  
  485. 485:     /**
  486. 486:      * Returns the global configuration.
  487. 487:      * @param  string key
  488. 488:      * @param  mixed  default value
  489. 489:      * @return mixed 
  490. 490:      */
  491. 491:     public static function getConfig($key NULL$default NULL)
  492. 492:     {
  493. 493:         if (func_num_args()) {
  494. 494:             return isset(self::$config[$key]self::$config[$key$default;
  495. 495:  
  496. 496:         else {
  497. 497:             return self::$config;
  498. 498:         }
  499. 499:     }
  500. 500: