PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/www/libs/nette-dev/Environment.php

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