/
Environment.php
481 lines (364 loc) · 9.28 KB
/
Environment.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
<?php
/**
* This file is part of the Nette Framework (http://nette.org)
*
* Copyright (c) 2004, 2011 David Grudl (http://davidgrudl.com)
*
* For the full copyright and license information, please view
* the file license.txt that was distributed with this source code.
*/
namespace Nette;
use Nette;
/**
* Nette environment and configuration.
*
* @author David Grudl
*/
final class Environment
{
/** environment name */
const DEVELOPMENT = 'development',
PRODUCTION = 'production',
CONSOLE = 'console';
/** @var Nette\DI\Configurator */
private static $configurator;
/** @var Nette\DI\IContainer */
private static $context;
/** @var array */
private static $vars = array(
);
/** @var array */
private static $aliases = array(
'getHttpContext' => 'Nette\\Web\\HttpContext',
'getHttpRequest' => 'Nette\\Web\\IHttpRequest',
'getHttpResponse' => 'Nette\\Web\\IHttpResponse',
'getApplication' => 'Nette\\Application\\Application',
'getUser' => 'Nette\\Web\\IUser',
'getRobotLoader' => 'Nette\\Loaders\\RobotLoader',
);
/**
* Static class - cannot be instantiated.
*/
final public function __construct()
{
throw new StaticClassException;
}
/**
* Sets "class behind Environment" configurator.
* @param Nette\DI\Configurator
* @return void
*/
public static function setConfigurator(DI\Configurator $configurator)
{
self::$configurator = $configurator;
}
/**
* Gets "class behind Environment" configurator.
* @return Nette\DI\Configurator
*/
public static function getConfigurator()
{
if (self::$configurator === NULL) {
self::$configurator = new DI\Configurator;
}
return self::$configurator;
}
/********************* environment name and modes ****************d*g**/
/**
* Sets the current environment name.
* @param string
* @return void
* @throws InvalidStateException
*/
public static function setName($name)
{
if (!isset(self::$vars['environment'])) {
self::setVariable('environment', $name, FALSE);
} else {
throw new InvalidStateException('Environment name has already been set.');
}
}
/**
* Returns the current environment name.
* @return string
*/
public static function getName()
{
$name = self::getVariable('environment', NULL);
if ($name === NULL) {
$name = self::getConfigurator()->detect('environment');
self::setVariable('environment', $name, FALSE);
}
return $name;
}
/**
* Sets the mode.
* @param string mode identifier
* @param bool set or unset
* @return void
*/
public static function setMode($mode, $value = TRUE)
{
self::getContext()->setParam($mode . 'Mode', (bool) $value);
}
/**
* Returns the mode.
* @param string mode identifier
* @return bool
*/
public static function getMode($mode)
{
if (isset(self::getContext()->params[$mode . 'Mode'])) {
return self::getContext()->params[$mode . 'Mode'];
} else {
return self::getContext()->params[$mode . 'Mode'] = self::getConfigurator()->detect($mode);
}
}
/**
* Detects console (non-HTTP) mode.
* @return bool
*/
public static function isConsole()
{
return self::getMode('console');
}
/**
* Determines whether a server is running in production mode.
* @return bool
*/
public static function isProduction()
{
return self::getMode('production');
}
/********************* environment variables ****************d*g**/
/**
* Sets the environment variable.
* @param string
* @param mixed
* @param bool
* @return void
*/
public static function setVariable($name, $value, $expand = TRUE)
{
$expand = $expand && is_string($value) && strpos($value, '%') !== FALSE;
if (!$expand) {
self::getContext()->setParam($name, $value);
}
self::$vars[$name] = array($value, $expand);
}
/**
* Returns the value of an environment variable or $default if there is no element set.
* @param string
* @param mixed default value to use if key not found
* @return mixed
* @throws InvalidStateException
*/
public static function getVariable($name, $default = NULL)
{
if (isset(self::$vars[$name])) {
list($var, $exp) = self::$vars[$name];
if ($exp) {
$var = self::expand($var);
self::$vars[$name] = array($var, FALSE);
}
return $var;
} else {
// convert from camelCaps (or PascalCaps) to ALL_CAPS
$const = strtoupper(preg_replace('#(.)([A-Z]+)#', '$1_$2', $name));
$list = get_defined_constants(TRUE);
if (isset($list['user'][$const])) {
self::$vars[$name] = array($list['user'][$const], FALSE);
return $list['user'][$const];
} elseif (func_num_args() > 1) {
return $default;
} else {
throw new InvalidStateException("Unknown environment variable '$name'.");
}
}
}
/**
* Returns the all environment variables.
* @return array
*/
public static function getVariables()
{
$res = array();
foreach (self::$vars as $name => $foo) {
$res[$name] = self::getVariable($name);
}
return $res;
}
/**
* Returns expanded variable.
* @param string
* @return string
* @throws InvalidStateException
*/
public static function expand($var)
{
static $livelock;
if (is_string($var) && strpos($var, '%') !== FALSE) {
return @preg_replace_callback( // intentionally @
'#%([a-z0-9_-]*)%#i',
function ($m) use (& $livelock) {
list(, $var) = $m;
if ($var === '') {
return '%';
}
if (isset($livelock[$var])) {
throw new InvalidStateException("Circular reference detected for variables: "
. implode(', ', array_keys($livelock)) . ".");
}
try {
$livelock[$var] = TRUE;
$val = Environment::getVariable($var);
unset($livelock[$var]);
} catch (\Exception $e) {
$livelock = array();
throw $e;
}
if (!is_scalar($val)) {
throw new InvalidStateException("Environment variable '$var' is not scalar.");
}
return $val;
},
$var
); // intentionally @ due PHP bug #39257
}
return $var;
}
/********************* context ****************d*g**/
/**
* Get initial instance of context.
* @return Nette\DI\IContainer
*/
public static function getContext()
{
if (self::$context === NULL) {
self::$context = self::getConfigurator()->createContainer();
}
return self::$context;
}
/**
* Gets the service object of the specified type.
* @param string service name
* @return object
*/
public static function getService($name)
{
return self::getContext()->getService($name);
}
/**
* Adds new Environment::get<Service>() method.
* @param string service name
* @param string alias name
* @return void
*/
public static function setServiceAlias($service, $alias)
{
self::$aliases['get' . ucfirst($alias)] = $service;
}
/**
* Calling to undefined static method.
* @param string method name
* @param array arguments
* @return object service
*/
public static function __callStatic($name, $args)
{
if (isset(self::$aliases[$name])) {
return self::getContext()->getService(self::$aliases[$name], $args);
} else {
throw new MemberAccessException("Call to undefined static method Nette\\Environment::$name().");
}
}
/**
* @return Nette\Http\Request
*/
public static function getHttpRequest()
{
return self::getContext()->getService(self::$aliases[__FUNCTION__]);
}
/**
* @return Nette\Http\Context
*/
public static function getHttpContext()
{
return self::getContext()->getService(self::$aliases[__FUNCTION__]);
}
/**
* @return Nette\Http\Response
*/
public static function getHttpResponse()
{
return self::getContext()->getService(self::$aliases[__FUNCTION__]);
}
/**
* @return Nette\Application\Application
*/
public static function getApplication()
{
return self::getContext()->getService(self::$aliases[__FUNCTION__]);
}
/**
* @return Nette\Http\User
*/
public static function getUser()
{
return self::getContext()->getService(self::$aliases[__FUNCTION__]);
}
/**
* @return Nette\Loaders\RobotLoader
*/
public static function getRobotLoader()
{
return self::getContext()->getService(self::$aliases[__FUNCTION__]);
}
/********************* service factories ****************d*g**/
/**
* @param string
* @return Nette\Caching\Cache
*/
public static function getCache($namespace = '')
{
return new Caching\Cache(
self::getService('Nette\\Caching\\ICacheStorage'),
$namespace
);
}
/**
* Returns instance of session or session namespace.
* @param string
* @return Nette\Http\Session
*/
public static function getSession($namespace = NULL)
{
$handler = self::getService('Nette\\Web\\Session');
return $namespace === NULL ? $handler : $handler->getNamespace($namespace);
}
/********************* global configuration ****************d*g**/
/**
* Loads global configuration from file and process it.
* @param string file name
* @return \ArrayObject
*/
public static function loadConfig($file = NULL)
{
return self::getConfigurator()->loadConfig(self::getContext(), $file);
}
/**
* Returns the global configuration.
* @param string key
* @param mixed default value
* @return mixed
*/
public static function getConfig($key = NULL, $default = NULL)
{
$config = self::getContext()->config;
if (func_num_args()) {
return isset($config[$key]) ? $config[$key] : $default;
} else {
return $config;
}
}
}