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