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