PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Nette/Environment.php

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