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 $productionMode;
32:
33:
34: private static $createdAt;
35:
36:
37: private static $context;
38:
39:
40: 41: 42:
43: final public function __construct()
44: {
45: throw new StaticClassException;
46: }
47:
48:
49:
50:
51:
52: 53: 54: 55:
56: public static function isConsole()
57: {
58: return PHP_SAPI === 'cli';
59: }
60:
61:
62: 63: 64: 65:
66: public static function isProduction()
67: {
68: if (self::$productionMode === NULL) {
69: self::$productionMode = !Nette\Config\Configurator::detectDebugMode();
70: }
71: return self::$productionMode;
72: }
73:
74:
75: 76: 77: 78: 79:
80: public static function setProductionMode($value = TRUE)
81: {
82: self::$productionMode = (bool) $value;
83: }
84:
85:
86:
87:
88:
89: 90: 91: 92: 93: 94: 95:
96: public static function setVariable($name, $value, $expand = TRUE)
97: {
98: if ($expand && is_string($value)) {
99: $value = self::getContext()->expand($value);
100: }
101: self::getContext()->parameters[$name] = $value;
102: }
103:
104:
105: 106: 107: 108: 109: 110: 111:
112: public static function getVariable($name, $default = NULL)
113: {
114: if (isset(self::getContext()->parameters[$name])) {
115: return self::getContext()->parameters[$name];
116: } elseif (func_num_args() > 1) {
117: return $default;
118: } else {
119: throw new InvalidStateException("Unknown environment variable '$name'.");
120: }
121: }
122:
123:
124: 125: 126: 127:
128: public static function getVariables()
129: {
130: return self::getContext()->parameters;
131: }
132:
133:
134: 135: 136: 137: 138: 139:
140: public static function expand($s)
141: {
142: return self::getContext()->expand($s);
143: }
144:
145:
146:
147:
148:
149: 150: 151: 152:
153: public static function setContext(DI\Container $context)
154: {
155: if (self::$createdAt) {
156: throw new Nette\InvalidStateException('Configurator & SystemContainer has already been created automatically by Nette\Environment at ' . self::$createdAt);
157: }
158: self::$context = $context;
159: }
160:
161:
162: 163: 164: 165:
166: public static function getContext()
167: {
168: if (self::$context === NULL) {
169: self::loadConfig();
170: }
171: return self::$context;
172: }
173:
174:
175: 176: 177: 178: 179:
180: public static function getService($name)
181: {
182: return self::getContext()->getService($name);
183: }
184:
185:
186: 187: 188: 189: 190: 191:
192: public static function __callStatic($name, $args)
193: {
194: if (!$args && strncasecmp($name, 'get', 3) === 0) {
195: return self::getService(lcfirst(substr($name, 3)));
196: } else {
197: throw new MemberAccessException("Call to undefined static method Nette\\Environment::$name().");
198: }
199: }
200:
201:
202: 203: 204:
205: public static function getHttpRequest()
206: {
207: return self::getContext()->getByType('Nette\Http\IRequest');
208: }
209:
210:
211: 212: 213:
214: public static function getHttpContext()
215: {
216: return self::getContext()->getByType('Nette\Http\Context');
217: }
218:
219:
220: 221: 222:
223: public static function getHttpResponse()
224: {
225: return self::getContext()->getByType('Nette\Http\IResponse');
226: }
227:
228:
229: 230: 231:
232: public static function getApplication()
233: {
234: return self::getContext()->getByType('Nette\Application\Application');
235: }
236:
237:
238: 239: 240:
241: public static function getUser()
242: {
243: return self::getContext()->getByType('Nette\Security\User');
244: }
245:
246:
247: 248: 249:
250: public static function getRobotLoader()
251: {
252: return self::getContext()->getByType('Nette\Loaders\RobotLoader');
253: }
254:
255:
256:
257:
258:
259: 260: 261: 262:
263: public static function getCache($namespace = '')
264: {
265: return new Caching\Cache(self::getService('cacheStorage'), $namespace);
266: }
267:
268:
269: 270: 271: 272: 273:
274: public static function getSession($namespace = NULL)
275: {
276: return $namespace === NULL
277: ? self::getService('session')
278: : self::getService('session')->getSection($namespace);
279: }
280:
281:
282:
283:
284:
285: 286: 287: 288: 289: 290:
291: public static function loadConfig($file = NULL, $section = NULL)
292: {
293: if (self::$createdAt) {
294: throw new Nette\InvalidStateException('Nette\Config\Configurator has already been created automatically by Nette\Environment at ' . self::$createdAt);
295: }
296: $configurator = new Nette\Config\Configurator;
297: $configurator
298: ->setDebugMode(!self::isProduction())
299: ->setTempDirectory(defined('TEMP_DIR') ? TEMP_DIR : '');
300: if ($file) {
301: $configurator->addConfig($file, $section);
302: }
303: self::$context = $configurator->createContainer();
304:
305: self::$createdAt = '?';
306: foreach (debug_backtrace(FALSE) as $row) {
307: if (isset($row['file']) && is_file($row['file']) && strpos($row['file'], NETTE_DIR . DIRECTORY_SEPARATOR) !== 0) {
308: self::$createdAt = "$row[file]:$row[line]";
309: break;
310: }
311: }
312: return self::getConfig();
313: }
314:
315:
316: 317: 318: 319: 320: 321:
322: public static function getConfig($key = NULL, $default = NULL)
323: {
324: $params = Nette\ArrayHash::from(self::getContext()->parameters);
325: if (func_num_args()) {
326: return isset($params[$key]) ? $params[$key] : $default;
327: } else {
328: return $params;
329: }
330: }
331:
332: }
333: