Source for file Environment.php
Documentation is available at Environment.php
6: * @copyright Copyright (c) 2004, 2010 David Grudl
7: * @license http://nettephp.com/license Nette license
8: * @link http://nettephp.com
16: * Nette environment and configuration.
18: * @copyright Copyright (c) 2004, 2010 David Grudl
23: /**#@+ environment name */
24: const DEVELOPMENT =
'development';
25: const PRODUCTION =
'production';
26: const CONSOLE =
'console';
31: const DEBUG =
'debug';
32: const PERFORMANCE =
'performance';
35: /** @var Configurator */
36: private static $configurator;
38: /** @var string the mode of current application */
39: private static $mode =
array();
41: /** @var ArrayObject */
42: private static $config;
44: /** @var IServiceLocator */
45: private static $serviceLocator;
48: private static $vars =
array(
49: 'encoding' =>
array('UTF-8', FALSE),
50: 'lang' =>
array('en', FALSE),
51: 'cacheBase' =>
array('%tempDir%', TRUE), // deprecated
52: 'tempDir' =>
array('%appDir%/temp', TRUE),
53: 'logDir' =>
array('%appDir%/log', TRUE),
57: private static $aliases =
array(
58: 'getHttpContext' =>
'Nette\Web\HttpContext',
59: 'getHttpRequest' =>
'Nette\Web\IHttpRequest',
60: 'getHttpResponse' =>
'Nette\Web\IHttpResponse',
61: 'getApplication' =>
'Nette\Application\Application',
62: 'getUser' =>
'Nette\Web\IUser',
68: * Static class - cannot be instantiated.
72: throw new LogicException("Cannot instantiate static class " .
get_class($this));
78: * Sets "class behind Environment" configurator.
79: * @param Configurator
84: self::$configurator =
$configurator;
90: * Gets "class behind Environment" configurator.
91: * @return Configurator
95: if (self::$configurator ===
NULL) {
96: self::$configurator =
new Configurator;
98: return self::$configurator;
103: /********************* environment name and modes ****************d*g**/
108: * Sets the current environment name.
111: * @throws InvalidStateException
115: if (!isset(self::$vars['environment'])) {
116: self::setVariable('environment', $name, FALSE);
126: * Returns the current environment name.
131: $name =
self::getVariable('environment');
132: if ($name ===
NULL) {
133: $name =
self::getConfigurator()->detect('environment');
134: self::setVariable('environment', $name, FALSE);
144: * @param string mode identifier
145: * @param bool set or unser
148: public static function setMode($mode, $value =
TRUE)
150: self::$mode[$mode] = (bool)
$value;
158: * @param string mode identifier
163: if (isset(self::$mode[$mode])) {
164: return self::$mode[$mode];
167: return self::$mode[$mode] =
self::getConfigurator()->detect($mode);
174: * Detects console (non-HTTP) mode.
179: return self::getMode('console');
185: * Determines whether a server is running in production mode.
190: return self::getMode('production');
205: /********************* environment variables ****************d*g**/
210: * Sets the environment variable.
221: self::$vars[$name] =
array($value, (bool)
$expand);
227: * Returns the value of an environment variable or $default if there is no element set.
229: * @param mixed default value to use if key not found
231: * @throws InvalidStateException
235: if (isset(self::$vars[$name])) {
236: list($var, $exp) =
self::$vars[$name];
238: $var =
self::expand($var);
239: self::$vars[$name] =
array($var, FALSE);
244: // convert from camelCaps (or PascalCaps) to ALL_CAPS
247: if (isset($list['user'][$const])) {
248: self::$vars[$name] =
array($list['user'][$const], FALSE);
249: return $list['user'][$const];
260: * Returns the all environment variables.
266: foreach (self::$vars as $name =>
$foo) {
267: $res[$name] =
self::getVariable($name);
275: * Returns expanded variable.
278: * @throws InvalidStateException
283: return @preg_replace_callback('#%([a-z0-9_-]*)%#i', array(__CLASS__
, 'expandCb'), $var); // intentionally @ due PHP bug #39257
291: * @see Environment::expand()
295: private static function expandCb($m)
298: if ($var ===
'') return '%';
301: if (isset($livelock[$var])) {
307: $livelock[$var] =
TRUE;
308: $val =
self::getVariable($var);
309: unset($livelock[$var]);
310: } catch (Exception $e) {
311: $livelock =
array();
315: if ($val ===
NULL) {
327: /********************* service locator ****************d*g**/
332: * Get initial instance of service locator.
333: * @return IServiceLocator
337: if (self::$serviceLocator ===
NULL) {
338: self::$serviceLocator =
self::getConfigurator()->createServiceLocator();
340: return self::$serviceLocator;
346: * Gets the service object of the specified type.
347: * @param string service name
348: * @param array options in case service is not singleton
359: * Adds new Environment::get<Service>() method.
360: * @param string service name
361: * @param string alias name
364: public static function setServiceAlias($service, $alias)
366: self::$aliases['get' .
ucfirst($alias)] =
$service;
372: * Calling to undefined static method.
373: * @param string method name
374: * @param array arguments
375: * @return object service
379: if (isset(self::$aliases[$name])) {
380: return self::getServiceLocator()->getService(self::$aliases[$name], $args);
382: throw new MemberAccessException("Call to undefined static method Nette\\Environment::$name().");
389: * @return HttpRequest
393: return self::getServiceLocator()->getService(self::$aliases[__FUNCTION__
]);
399: * @return HttpContext
401: public static function getHttpContext()
403: return self::getServiceLocator()->getService(self::$aliases[__FUNCTION__
]);
409: * @return HttpResponse
411: public static function getHttpResponse()
413: return self::getServiceLocator()->getService(self::$aliases[__FUNCTION__
]);
419: * @return Application
421: public static function getApplication()
423: return self::getServiceLocator()->getService(self::$aliases[__FUNCTION__
]);
431: public static function getUser()
433: return self::getServiceLocator()->getService(self::$aliases[__FUNCTION__
]);
438: /********************* service factories ****************d*g**/
446: public static function getCache($namespace =
'')
449: self::getService('Nette\Caching\ICacheStorage'),
457: * Returns instance of session or session namespace.
463: $handler =
self::getService('Nette\Web\Session');
464: return $namespace ===
NULL ?
$handler :
$handler->getNamespace($namespace);
469: /********************* global configuration ****************d*g**/
474: * Loads global configuration from file and process it.
475: * @param string|Nette\Config\Config file name or Config object
476: * @return ArrayObject
480: return self::$config =
self::getConfigurator()->loadConfig($file);
486: * Returns the global configuration.
488: * @param mixed default value
491: public static function getConfig($key =
NULL, $default =
NULL)
494: return isset(self::$config[$key]) ?
self::$config[$key] :
$default;
497: return self::$config;