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