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