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