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),
65: private static $aliases =
array(
66: 'getHttpRequest' =>
'Nette\Web\IHttpRequest',
67: 'getHttpResponse' =>
'Nette\Web\IHttpResponse',
68: 'getApplication' =>
'Nette\Application\Application',
69: 'getUser' =>
'Nette\Web\IUser',
75: * Static class - cannot be instantiated.
79: throw new LogicException("Cannot instantiate static class " .
get_class($this));
85: * Sets "class behind Environment" configurator.
86: * @param Configurator
91: self::$configurator =
$configurator;
97: * Gets "class behind Environment" configurator.
98: * @return Configurator
102: if (self::$configurator ===
NULL) {
103: self::$configurator =
new Configurator;
105: return self::$configurator;
110: /********************* environment name and modes ****************d*g**/
115: * Sets the current environment name.
118: * @throws InvalidStateException
122: if (!isset(self::$vars['environment'])) {
123: self::setVariable('environment', $name, FALSE);
133: * Returns the current environment name.
138: $name =
self::getVariable('environment');
139: if ($name ===
NULL) {
140: $name =
self::getConfigurator()->detect('environment');
141: self::setVariable('environment', $name, FALSE);
151: * @param string mode identifier
152: * @param bool set or unser
155: public static function setMode($mode, $value =
TRUE)
157: self::$mode[$mode] = (bool)
$value;
165: * @param string mode identifier
170: if (isset(self::$mode[$mode])) {
171: return self::$mode[$mode];
174: return self::$mode[$mode] =
self::getConfigurator()->detect($mode);
181: * Detects console (non-HTTP) mode.
186: return self::getMode('console');
192: * Determines whether a server is running in production mode.
197: return self::getMode('production');
212: /********************* environment variables ****************d*g**/
217: * Sets the environment variable.
228: self::$vars[$name] =
array($value, (bool)
$expand);
234: * Returns the value of an environment variable or $default if there is no element set.
236: * @param mixed default value to use if key not found
238: * @throws InvalidStateException
242: if (isset(self::$vars[$name])) {
243: list($var, $exp) =
self::$vars[$name];
245: $var =
self::expand($var);
246: self::$vars[$name] =
array($var, FALSE);
251: // convert from camelCaps (or PascalCaps) to ALL_CAPS
254: if (isset($list['user'][$const])) {
255: self::$vars[$name] =
array($list['user'][$const], FALSE);
256: return $list['user'][$const];
267: * Returns the all environment variables.
273: foreach (self::$vars as $name =>
$foo) {
274: $res[$name] =
self::getVariable($name);
282: * Returns expanded variable.
285: * @throws InvalidStateException
290: return @preg_replace_callback('#%([a-z0-9_-]*)%#i', array(__CLASS__
, 'expandCb'), $var); // intentionally @ due PHP bug #39257
298: * @see Environment::expand()
302: private static function expandCb($m)
305: if ($var ===
'') return '%';
308: if (isset($livelock[$var])) {
314: $livelock[$var] =
TRUE;
315: $val =
self::getVariable($var);
316: unset($livelock[$var]);
317: } catch (Exception $e) {
318: $livelock =
array();
322: if ($val ===
NULL) {
334: /********************* service locator ****************d*g**/
339: * Get initial instance of service locator.
340: * @return IServiceLocator
344: if (self::$serviceLocator ===
NULL) {
345: self::$serviceLocator =
self::getConfigurator()->createServiceLocator();
347: return self::$serviceLocator;
353: * Gets the service object of the specified type.
354: * @param string service name
355: * @param array options in case service is not singleton
366: * Adds new Environment::get<Service>() method.
367: * @param string service name
368: * @param string alias name
371: public static function setServiceAlias($service, $alias)
373: self::$aliases['get' .
ucfirst($alias)] =
$service;
379: * Calling to undefined static method.
380: * @param string method name
381: * @param array arguments
382: * @return object service
386: if (isset(self::$aliases[$name])) {
387: return self::getServiceLocator()->getService(self::$aliases[$name], $args);
389: throw new MemberAccessException("Call to undefined static method Nette\\Environment::$name().");
396: * @return HttpRequest
400: return self::getServiceLocator()->getService(self::$aliases[__FUNCTION__
]);
406: * @return HttpResponse
408: public static function getHttpResponse()
410: return self::getServiceLocator()->getService(self::$aliases[__FUNCTION__
]);
416: * @return Application
418: public static function getApplication()
420: return self::getServiceLocator()->getService(self::$aliases[__FUNCTION__
]);
428: public static function getUser()
430: return self::getServiceLocator()->getService(self::$aliases[__FUNCTION__
]);
435: /********************* service factories ****************d*g**/
443: public static function getCache($namespace =
'')
446: self::getService('Nette\Caching\ICacheStorage'),
454: * Returns instance of session or session namespace.
460: $handler =
self::getService('Nette\Web\Session');
461: return $namespace ===
NULL ?
$handler :
$handler->getNamespace($namespace);
466: /********************* global configuration ****************d*g**/
471: * Loads global configuration from file and process it.
472: * @param string|Nette\Config\Config file name or Config object
473: * @return ArrayObject
477: return self::$config =
self::getConfigurator()->loadConfig($file);
483: * Returns the global configuration.
485: * @param mixed default value
488: public static function getConfig($key =
NULL, $default =
NULL)
491: return isset(self::$config[$key]) ?
self::$config[$key] :
$default;
494: return self::$config;