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