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