Source for file Environment.php
Documentation is available at Environment.php
6: * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
8: * This source file is subject to the "Nette license" that is bundled
9: * with this package in the file license.txt.
11: * For more information please see http://nettephp.com
13: * @copyright Copyright (c) 2004, 2009 David Grudl
14: * @license http://nettephp.com/license Nette license
15: * @link http://nettephp.com
23: * Nette environment and configuration.
25: * @author David Grudl
26: * @copyright Copyright (c) 2004, 2009 David Grudl
31: /**#@+ environment name */
32: const DEVELOPMENT =
'development';
33: const PRODUCTION =
'production';
34: const CONSOLE =
'console';
39: const DEBUG =
'debug';
40: const PERFORMANCE =
'performance';
43: /** @var NConfigurator */
44: private static $configurator;
46: /** @var string the mode of current application */
47: private static $mode =
array();
49: /** @var ArrayObject */
50: private static $config;
52: /** @var IServiceLocator */
53: private static $serviceLocator;
56: private static $vars =
array(
57: 'encoding' =>
array('UTF-8', FALSE),
58: 'lang' =>
array('en', FALSE),
59: 'cacheBase' =>
array('%tempDir%', TRUE), // deprecated
60: 'tempDir' =>
array('%appDir%/temp', TRUE),
61: 'logDir' =>
array('%appDir%/log', TRUE),
62: 'templatesDir' =>
array('%appDir%/templates', TRUE),
63: 'presentersDir' =>
array('%appDir%/presenters', TRUE),
64: 'componentsDir' =>
array('%appDir%/components', TRUE),
65: 'modelsDir' =>
array('%appDir%/models', TRUE),
69: private static $aliases =
array(
70: 'getHttpRequest' =>
'IHttpRequest',
71: 'getHttpResponse' =>
'IHttpResponse',
72: 'getApplication' =>
'NApplication',
73: 'getUser' =>
'IUser',
79: * Static class - cannot be instantiated.
83: throw new LogicException("Cannot instantiate static class " .
get_class($this));
89: * Sets "class behind NEnvironment" configurator.
90: * @param NConfigurator
95: self::$configurator =
$configurator;
101: * Gets "class behind NEnvironment" configurator.
102: * @return NConfigurator
106: if (self::$configurator ===
NULL) {
107: self::$configurator =
new NConfigurator;
109: return self::$configurator;
114: /********************* environment name and modes ****************d*g**/
119: * Sets the current environment name.
122: * @throws InvalidStateException
126: if (!isset(self::$vars['environment'])) {
127: self::setVariable('environment', $name, FALSE);
137: * Returns the current environment name.
142: $name =
self::getVariable('environment');
143: if ($name ===
NULL) {
144: $name =
self::getConfigurator()->detect('environment');
145: self::setVariable('environment', $name, FALSE);
155: * @param string mode identifier
156: * @param bool set or unser
159: public static function setMode($mode, $value =
TRUE)
161: self::$mode[$mode] = (bool)
$value;
169: * @param string mode identifier
174: if (isset(self::$mode[$mode])) {
175: return self::$mode[$mode];
178: return self::$mode[$mode] =
self::getConfigurator()->detect($mode);
185: * Detects console (non-HTTP) mode.
190: return self::getMode('console');
196: * Determines whether a server is running in production mode.
201: return self::getMode('production');
216: /********************* environment variables ****************d*g**/
221: * Sets the environment variable.
232: self::$vars[$name] =
array($value, (bool)
$expand);
238: * Returns the value of an environment variable or $default if there is no element set.
240: * @param mixed default value to use if key not found
242: * @throws InvalidStateException
246: if (isset(self::$vars[$name])) {
247: list($var, $exp) =
self::$vars[$name];
249: $var =
self::expand($var);
250: self::$vars[$name] =
array($var, FALSE);
255: // convert from camelCaps (or PascalCaps) to ALL_CAPS
258: if (isset($list['user'][$const])) {
259: self::$vars[$name] =
array($list['user'][$const], FALSE);
260: return $list['user'][$const];
271: * Returns the all environment variables.
277: foreach (self::$vars as $name =>
$foo) {
278: $res[$name] =
self::getVariable($name);
286: * Returns expanded variable.
289: * @throws InvalidStateException
294: return @preg_replace_callback('#%([a-z0-9_-]*)%#i', array(__CLASS__
, 'expandCb'), $var); // intentionally @ due PHP bug #39257
302: * @see NEnvironment::expand()
306: private static function expandCb($m)
309: if ($var ===
'') return '%';
312: if (isset($livelock[$var])) {
318: $livelock[$var] =
TRUE;
319: $val =
self::getVariable($var);
320: unset($livelock[$var]);
321: } catch (Exception $e) {
322: $livelock =
array();
326: if ($val ===
NULL) {
338: /********************* service locator ****************d*g**/
343: * Get initial instance of service locator.
344: * @return IServiceLocator
348: if (self::$serviceLocator ===
NULL) {
349: self::$serviceLocator =
self::getConfigurator()->createServiceLocator();
351: return self::$serviceLocator;
357: * Gets the service object of the specified type.
358: * @param string service name
359: * @param array options in case service is not singleton
370: * Adds new NEnvironment::get<Service>() method.
371: * @param string service name
372: * @param string alias name
375: public static function setServiceAlias($service, $alias)
377: self::$aliases['get' .
ucfirst($alias)] =
$service;
383: * Calling to undefined static method.
384: * @param string method name
385: * @param array arguments
386: * @return object service
390: if (isset(self::$aliases[$name])) {
391: return self::getServiceLocator()->getService(self::$aliases[$name], $args);
393: throw new MemberAccessException("Call to undefined static method \NEnvironment::$name().");
400: * @return NHttpRequest
404: return self::getServiceLocator()->getService(self::$aliases[__FUNCTION__
]);
410: * @return NHttpResponse
412: public static function getHttpResponse()
414: return self::getServiceLocator()->getService(self::$aliases[__FUNCTION__
]);
420: * @return NApplication
422: public static function getApplication()
424: return self::getServiceLocator()->getService(self::$aliases[__FUNCTION__
]);
432: public static function getUser()
434: return self::getServiceLocator()->getService(self::$aliases[__FUNCTION__
]);
439: /********************* service factories ****************d*g**/
447: public static function getCache($namespace =
'')
450: self::getService('ICacheStorage'),
458: * Returns instance of session or session namespace.
464: $handler =
self::getService('NSession');
470: /********************* global configuration ****************d*g**/
475: * Loads global configuration from file and process it.
476: * @param string|NConfig file name or NConfig object
477: * @return ArrayObject
481: return self::$config =
self::getConfigurator()->loadConfig($file);
487: * Returns the global configuration.
489: * @param mixed default value
492: public static function getConfig($key =
NULL, $default =
NULL)
495: return isset(self::$config[$key]) ?
self::$config[$key] :
$default;
498: return self::$config;