1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Nette;
13:
14: use Nette;
15:
16:
17:
18: 19: 20: 21: 22:
23: final class Environment
24: {
25:
26: const DEVELOPMENT = 'development';
27: const PRODUCTION = 'production';
28: const CONSOLE = 'console';
29:
30:
31:
32: private static $configurator;
33:
34:
35: private static $modes = array();
36:
37:
38: private static $config;
39:
40:
41: private static $context;
42:
43:
44: private static $vars = array(
45: );
46:
47:
48: private static $aliases = array(
49: 'getHttpContext' => 'Nette\\Web\\HttpContext',
50: 'getHttpRequest' => 'Nette\\Web\\IHttpRequest',
51: 'getHttpResponse' => 'Nette\\Web\\IHttpResponse',
52: 'getApplication' => 'Nette\\Application\\Application',
53: 'getUser' => 'Nette\\Web\\IUser',
54: 'getRobotLoader' => 'Nette\\Loaders\\RobotLoader',
55: );
56:
57:
58:
59: 60: 61:
62: final public function __construct()
63: {
64: throw new \LogicException("Cannot instantiate static class " . get_class($this));
65: }
66:
67:
68:
69: 70: 71: 72: 73:
74: public static function setConfigurator(Configurator $configurator)
75: {
76: self::$configurator = $configurator;
77: }
78:
79:
80:
81: 82: 83: 84:
85: public static function getConfigurator()
86: {
87: if (self::$configurator === NULL) {
88: self::$configurator = new Configurator;
89: }
90: return self::$configurator;
91: }
92:
93:
94:
95:
96:
97:
98:
99: 100: 101: 102: 103: 104:
105: public static function setName($name)
106: {
107: if (!isset(self::$vars['environment'])) {
108: self::setVariable('environment', $name, FALSE);
109:
110: } else {
111: throw new \InvalidStateException('Environment name has already been set.');
112: }
113: }
114:
115:
116:
117: 118: 119: 120:
121: public static function getName()
122: {
123: $name = self::getVariable('environment', NULL);
124: if ($name === NULL) {
125: $name = self::getConfigurator()->detect('environment');
126: self::setVariable('environment', $name, FALSE);
127: }
128: return $name;
129: }
130:
131:
132:
133: 134: 135: 136: 137: 138:
139: public static function setMode($mode, $value = TRUE)
140: {
141: self::$modes[$mode] = (bool) $value;
142: }
143:
144:
145:
146: 147: 148: 149: 150:
151: public static function getMode($mode)
152: {
153: if (isset(self::$modes[$mode])) {
154: return self::$modes[$mode];
155:
156: } else {
157: return self::$modes[$mode] = self::getConfigurator()->detect($mode);
158: }
159: }
160:
161:
162:
163: 164: 165: 166:
167: public static function isConsole()
168: {
169: return self::getMode('console');
170: }
171:
172:
173:
174: 175: 176: 177:
178: public static function isProduction()
179: {
180: return self::getMode('production');
181: }
182:
183:
184:
185:
186:
187:
188:
189: 190: 191: 192: 193: 194: 195:
196: public static function setVariable($name, $value, $expand = TRUE)
197: {
198: if (!is_string($value)) {
199: $expand = FALSE;
200: }
201: self::$vars[$name] = array($value, (bool) $expand);
202: }
203:
204:
205:
206: 207: 208: 209: 210: 211: 212:
213: public static function getVariable($name, $default = NULL)
214: {
215: if (isset(self::$vars[$name])) {
216: list($var, $exp) = self::$vars[$name];
217: if ($exp) {
218: $var = self::expand($var);
219: self::$vars[$name] = array($var, FALSE);
220: }
221: return $var;
222:
223: } else {
224: 225: $const = strtoupper(preg_replace('#(.)([A-Z]+)#', '$1_$2', $name));
226: $list = get_defined_constants(TRUE);
227: if (isset($list['user'][$const])) {
228: self::$vars[$name] = array($list['user'][$const], FALSE);
229: return $list['user'][$const];
230:
231: } elseif (func_num_args() > 1) {
232: return $default;
233:
234: } else {
235: throw new \InvalidStateException("Unknown environment variable '$name'.");
236: }
237: }
238: }
239:
240:
241:
242: 243: 244: 245:
246: public static function getVariables()
247: {
248: $res = array();
249: foreach (self::$vars as $name => $foo) {
250: $res[$name] = self::getVariable($name);
251: }
252: return $res;
253: }
254:
255:
256:
257: 258: 259: 260: 261: 262:
263: public static function expand($var)
264: {
265: static $livelock;
266: if (is_string($var) && strpos($var, '%') !== FALSE) {
267: return @preg_replace_callback(
268: '#%([a-z0-9_-]*)%#i',
269: function ($m) use (& $livelock) {
270: list(, $var) = $m;
271: if ($var === '') return '%';
272:
273: if (isset($livelock[$var])) {
274: throw new \InvalidStateException("Circular reference detected for variables: "
275: . implode(', ', array_keys($livelock)) . ".");
276: }
277:
278: try {
279: $livelock[$var] = TRUE;
280: $val = Environment::getVariable($var);
281: unset($livelock[$var]);
282: } catch (\Exception $e) {
283: $livelock = array();
284: throw $e;
285: }
286:
287: if (!is_scalar($val)) {
288: throw new \InvalidStateException("Environment variable '$var' is not scalar.");
289: }
290:
291: return $val;
292: },
293: $var
294: ); 295: }
296: return $var;
297: }
298:
299:
300:
301:
302:
303:
304:
305: 306: 307: 308:
309: public static function getContext()
310: {
311: if (self::$context === NULL) {
312: self::$context = self::getConfigurator()->createContext();
313: }
314: return self::$context;
315: }
316:
317:
318:
319: 320: 321: 322: 323: 324:
325: public static function getService($name, array $options = NULL)
326: {
327: return self::getContext()->getService($name, $options);
328: }
329:
330:
331:
332: 333: 334: 335: 336: 337:
338: public static function setServiceAlias($service, $alias)
339: {
340: self::$aliases['get' . ucfirst($alias)] = $service;
341: }
342:
343:
344:
345: 346: 347: 348: 349: 350:
351: public static function __callStatic($name, $args)
352: {
353: if (isset(self::$aliases[$name])) {
354: return self::getContext()->getService(self::$aliases[$name], $args);
355: } else {
356: throw new \MemberAccessException("Call to undefined static method Nette\\Environment::$name().");
357: }
358: }
359:
360:
361:
362: 363: 364:
365: public static function getHttpRequest()
366: {
367: return self::getContext()->getService(self::$aliases[__FUNCTION__]);
368: }
369:
370:
371:
372: 373: 374:
375: public static function getHttpContext()
376: {
377: return self::getContext()->getService(self::$aliases[__FUNCTION__]);
378: }
379:
380:
381:
382: 383: 384:
385: public static function getHttpResponse()
386: {
387: return self::getContext()->getService(self::$aliases[__FUNCTION__]);
388: }
389:
390:
391:
392: 393: 394:
395: public static function getApplication()
396: {
397: return self::getContext()->getService(self::$aliases[__FUNCTION__]);
398: }
399:
400:
401:
402: 403: 404:
405: public static function getUser()
406: {
407: return self::getContext()->getService(self::$aliases[__FUNCTION__]);
408: }
409:
410:
411:
412: 413: 414:
415: public static function getRobotLoader()
416: {
417: return self::getContext()->getService(self::$aliases[__FUNCTION__]);
418: }
419:
420:
421:
422:
423:
424:
425:
426: 427: 428: 429:
430: public static function getCache($namespace = '')
431: {
432: return new Nette\Caching\Cache(
433: self::getService('Nette\\Caching\\ICacheStorage'),
434: $namespace
435: );
436: }
437:
438:
439:
440: 441: 442: 443: 444:
445: public static function getSession($namespace = NULL)
446: {
447: $handler = self::getService('Nette\\Web\\Session');
448: return $namespace === NULL ? $handler : $handler->getNamespace($namespace);
449: }
450:
451:
452:
453:
454:
455:
456:
457: 458: 459: 460: 461:
462: public static function loadConfig($file = NULL)
463: {
464: return self::$config = self::getConfigurator()->loadConfig($file);
465: }
466:
467:
468:
469: 470: 471: 472: 473: 474:
475: public static function getConfig($key = NULL, $default = NULL)
476: {
477: if (func_num_args()) {
478: return isset(self::$config[$key]) ? self::$config[$key] : $default;
479:
480: } else {
481: return self::$config;
482: }
483: }
484:
485: }
486: