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 Configurator */
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),
71: * Static class - cannot be instantiated.
75: throw new LogicException("Cannot instantiate static class " .
get_class($this));
81: * Sets "class behind Environment" configurator.
82: * @param Configurator
87: self::$configurator =
$configurator;
93: * Gets "class behind Environment" configurator.
94: * @return Configurator
98: if (self::$configurator ===
NULL) {
99: self::$configurator =
new Configurator;
101: return self::$configurator;
106: /********************* environment name and modes ****************d*g**/
111: * Sets the current environment name.
114: * @throws InvalidStateException
118: if (!isset(self::$vars['environment'])) {
119: self::setVariable('environment', $name, FALSE);
129: * Returns the current environment name.
134: $name =
self::getVariable('environment');
135: if ($name ===
NULL) {
136: $name =
self::getConfigurator()->detect('environment');
137: self::setVariable('environment', $name, FALSE);
147: * @param string mode identifier
148: * @param bool set or unser
151: public static function setMode($mode, $value =
TRUE)
153: self::$mode[$mode] = (bool)
$value;
161: * @param string mode identifier
166: if (isset(self::$mode[$mode])) {
167: return self::$mode[$mode];
170: return self::$mode[$mode] =
self::getConfigurator()->detect($mode);
177: * Detects console (non-HTTP) mode.
182: return self::getMode('console');
188: * Determines whether a server is running in production mode.
193: return self::getMode('production');
199: * Determines whether the debugger is active.
204: return self::getMode('debug');
209: /********************* environment variables ****************d*g**/
214: * Sets the environment variable.
225: self::$vars[$name] =
array($value, (bool)
$expand);
231: * Returns the value of an environment variable or $default if there is no element set.
233: * @param mixed default value to use if key not found
235: * @throws InvalidStateException
239: if (isset(self::$vars[$name])) {
240: list($var, $exp) =
self::$vars[$name];
242: $var =
self::expand($var);
243: self::$vars[$name] =
array($var, FALSE);
248: // convert from camelCaps (or PascalCaps) to ALL_CAPS
251: if (isset($list['user'][$const])) {
252: self::$vars[$name] =
array($list['user'][$const], FALSE);
253: return $list['user'][$const];
264: * Returns the all environment variables.
270: foreach (self::$vars as $name =>
$foo) {
271: $res[$name] =
self::getVariable($name);
279: * Returns expanded variable.
282: * @throws InvalidStateException
287: return @preg_replace_callback('#%([a-z0-9_-]*)%#i', array(__CLASS__
, 'expandCb'), $var); // intentionally @ due PHP bug #39257
295: * @see Environment::expand()
299: private static function expandCb($m)
302: if ($var ===
'') return '%';
305: if (isset($livelock[$var])) {
311: $livelock[$var] =
TRUE;
312: $val =
self::getVariable($var);
313: unset($livelock[$var]);
314: } catch (Exception $e) {
315: $livelock =
array();
319: if ($val ===
NULL) {
331: /********************* service locator ****************d*g**/
336: * Get initial instance of service locator.
337: * @return IServiceLocator
341: if (self::$serviceLocator ===
NULL) {
342: self::$serviceLocator =
self::getConfigurator()->createServiceLocator();
344: return self::$serviceLocator;
350: * Gets the service object of the specified type.
351: * @param string service name
352: * @param bool throw exception if service doesn't exist?
357: return self::getServiceLocator()->getService($name, $need);
363: * @return IHttpRequest
367: return self::getServiceLocator()->getService('Nette\Web\IHttpRequest');
373: * @return IHttpResponse
377: return self::getServiceLocator()->getService('Nette\Web\IHttpResponse');
383: * @return Application
387: return self::getServiceLocator()->getService('Nette\Application\Application');
397: return self::getServiceLocator()->getService('Nette\Web\IUser');
402: /********************* service factories ****************d*g**/
413: self::getService('Nette\Caching\ICacheStorage'),
421: * Returns instance of session or session namespace.
423: * @return Session|Nette\Web\Session
427: $handler =
self::getService('Nette\Web\Session');
433: /********************* global configuration ****************d*g**/
438: * Loads global configuration from file and process it.
439: * @param string|Nette\Config\Config file name or Config object
440: * @return ArrayObject
444: return self::$config =
self::getConfigurator()->loadConfig($file);
450: * Returns the global configuration.
452: * @param mixed default value
455: public static function getConfig($key =
NULL, $default =
NULL)
458: return isset(self::$config[$key]) ?
self::$config[$key] :
$default;
461: return self::$config;