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