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