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