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