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