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