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