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 https://nette.org
13: * @copyright Copyright (c) 2004, 2009 David Grudl
14: * @license https://nette.org/license Nette license
15: * @link https://nette.org
24: * Nette environment and configuration.
26: * @author David Grudl
27: * @copyright Copyright (c) 2004, 2009 David Grudl
32: /**#@+ environment name */
33: const DEVELOPMENT =
'development';
34: const PRODUCTION =
'production';
35: const CONSOLE =
'console';
40: const DEBUG =
'debug';
41: const PERFORMANCE =
'performance';
44: /** @var Configurator */
45: private static $configurator;
47: /** @var string the mode of current application */
48: private static $mode =
array();
50: /** @var ArrayObject */
51: private static $config;
53: /** @var IServiceLocator */
54: private static $serviceLocator;
57: private static $vars =
array(
58: 'encoding' =>
array('UTF-8', FALSE),
59: 'lang' =>
array('en', FALSE),
60: 'cacheBase' =>
array('%tempDir%/cache-', TRUE),
61: 'tempDir' =>
array('%appDir%/temp', TRUE),
62: 'logDir' =>
array('%appDir%/log', TRUE),
63: 'templatesDir' =>
array('%appDir%/templates', TRUE),
64: 'presentersDir' =>
array('%appDir%/presenters', TRUE),
65: 'componentsDir' =>
array('%appDir%/components', TRUE),
66: 'modelsDir' =>
array('%appDir%/models', TRUE),
72: * Static class - cannot be instantiated.
76: throw new LogicException("Cannot instantiate static class " .
get_class($this));
82: * Sets "class behind Environment" configurator.
83: * @param Configurator
88: self::$configurator =
$configurator;
94: * Gets "class behind Environment" configurator.
95: * @return Configurator
99: if (self::$configurator ===
NULL) {
100: self::$configurator =
new Configurator;
102: return self::$configurator;
107: /********************* environment name and modes ****************d*g**/
112: * Sets the current environment name.
115: * @throws InvalidStateException
119: if (!isset(self::$vars['environment'])) {
120: self::setVariable('environment', $name, FALSE);
130: * Returns the current environment name.
135: $name =
self::getVariable('environment');
136: if ($name ===
NULL) {
137: $name =
self::getConfigurator()->detect('environment');
138: self::setVariable('environment', $name, FALSE);
148: * @param string mode identifier
149: * @param bool set or unser
152: public static function setMode($mode, $flag =
TRUE)
154: if ($mode ===
'live') $mode =
'production'; // back compatibility
155: self::$mode[$mode] = (bool)
$flag;
163: * @param string mode identifier
168: if ($mode ===
'live') $mode =
'production'; // back compatibility
169: if (isset(self::$mode[$mode])) {
170: return self::$mode[$mode];
173: return self::$mode[$mode] =
self::getConfigurator()->detect($mode);
180: * Detects console (non-HTTP) mode.
185: return self::getMode('console');
191: * Determines whether a server is running in production mode.
196: return self::getMode('production');
202: * @deprecated {@link Environment::isProduction()}
206: trigger_error('Environment::isLive() is deprecated; use Environment::isProduction() instead.', E_USER_WARNING);
207: return self::getMode('production');
213: * Determines whether the debugger is active.
218: return self::getMode('debug');
223: /********************* environment variables ****************d*g**/
228: * Sets the environment variable.
239: self::$vars[$name] =
array($value, (bool)
$expand);
245: * Returns the value of an environment variable or $default if there is no element set.
247: * @param mixed default value to use if key not found
249: * @throws InvalidStateException
253: if (isset(self::$vars[$name])) {
254: list($var, $exp) =
self::$vars[$name];
256: $var =
self::expand($var);
257: self::$vars[$name] =
array($var, FALSE);
262: // convert from camelCaps (or PascalCaps) to ALL_CAPS
265: if (isset($list['user'][$const])) {
266: self::$vars[$name] =
array($list['user'][$const], FALSE);
267: return $list['user'][$const];
278: * Define one or more variables as constants.
279: * @param string|array
288: foreach ($names as $name) {
297: * Returns expanded variable.
300: * @throws InvalidStateException
305: return @preg_replace_callback('#%([a-z0-9_-]*)%#i', array(__CLASS__
, 'expandCb'), $var); // intentionally @ due PHP bug #39257
313: * @see Environment::expand()
317: private static function expandCb($m)
320: if ($var ===
'') return '%';
323: if (isset($livelock[$var])) {
329: $livelock[$var] =
TRUE;
330: $val =
self::getVariable($var);
331: unset($livelock[$var]);
332: } catch (Exception $e) {
333: $livelock =
array();
337: if ($val ===
NULL) {
349: /********************* service locator ****************d*g**/
354: * Get initial instance of service locator.
355: * @return IServiceLocator
359: if (self::$serviceLocator ===
NULL) {
360: self::$serviceLocator =
self::getConfigurator()->createServiceLocator();
362: return self::$serviceLocator;
368: * Gets the service object of the specified type.
369: * @param string service name
370: * @param bool throw exception if service doesn't exist?
375: return self::getServiceLocator()->getService($name, $need);
381: * @return IHttpRequest
385: return self::getServiceLocator()->getService('Nette\Web\IHttpRequest');
391: * @return IHttpResponse
395: return self::getServiceLocator()->getService('Nette\Web\IHttpResponse');
401: * @return Application
405: return self::getServiceLocator()->getService('Nette\Application\Application');
415: return self::getServiceLocator()->getService('Nette\Web\IUser');
420: /********************* service factories ****************d*g**/
431: self::getService('Nette\Caching\ICacheStorage'),
439: * Returns instance of session or session namespace.
441: * @return Session|Nette\Web\Session
445: $handler =
self::getService('Nette\Web\Session');
451: /********************* global configuration ****************d*g**/
456: * Loads global configuration from file and process it.
457: * @param string|Nette\Config\Config file name or Config object
458: * @return ArrayObject
462: return self::$config =
self::getConfigurator()->loadConfig($file);
468: * Returns the global configuration.
470: * @param mixed default value
473: public static function getConfig($key =
NULL, $default =
NULL)
476: return isset(self::$config[$key]) ?
self::$config[$key] :
$default;
479: return self::$config;