Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Diagnostics
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
      • Diagnostics
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • PhpGenerator
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
  • NetteModule
  • none

Classes

  • ArrayHash
  • ArrayList
  • Callback
  • Configurator
  • DateTime
  • Environment
  • Framework
  • FreezableObject
  • Image
  • Object
  • ObjectMixin

Interfaces

  • IFreezable

Exceptions

  • ArgumentOutOfRangeException
  • DeprecatedException
  • DirectoryNotFoundException
  • FatalErrorException
  • FileNotFoundException
  • InvalidArgumentException
  • InvalidStateException
  • IOException
  • MemberAccessException
  • NotImplementedException
  • NotSupportedException
  • OutOfRangeException
  • StaticClassException
  • UnexpectedValueException
  • UnknownImageFileException
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Other releases
  • Nette homepage
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://nette.org)
  5:  * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette;
  9: 
 10: use Nette;
 11: 
 12: 
 13: /**
 14:  * Nette environment and configuration.
 15:  *
 16:  * @author     David Grudl
 17:  * @deprecated
 18:  */
 19: class Environment
 20: {
 21:     /** environment name */
 22:     const DEVELOPMENT = 'development',
 23:         PRODUCTION = 'production',
 24:         CONSOLE = 'console';
 25: 
 26:     /** @var bool */
 27:     private static $productionMode;
 28: 
 29:     /** @var string */
 30:     private static $createdAt;
 31: 
 32:     /** @var Nette\DI\Container */
 33:     private static $context;
 34: 
 35: 
 36:     /**
 37:      * Static class - cannot be instantiated.
 38:      */
 39:     final public function __construct()
 40:     {
 41:         throw new StaticClassException;
 42:     }
 43: 
 44: 
 45:     /********************* environment modes ****************d*g**/
 46: 
 47: 
 48:     /**
 49:      * Detects console (non-HTTP) mode.
 50:      * @return bool
 51:      */
 52:     public static function isConsole()
 53:     {
 54:         return PHP_SAPI === 'cli';
 55:     }
 56: 
 57: 
 58:     /**
 59:      * Determines whether a server is running in production mode.
 60:      * @return bool
 61:      */
 62:     public static function isProduction()
 63:     {
 64:         if (self::$productionMode === NULL) {
 65:             self::$productionMode = !Nette\Configurator::detectDebugMode();
 66:         }
 67:         return self::$productionMode;
 68:     }
 69: 
 70: 
 71:     /**
 72:      * Enables or disables production mode.
 73:      * @param  bool
 74:      * @return void
 75:      */
 76:     public static function setProductionMode($value = TRUE)
 77:     {
 78:         self::$productionMode = (bool) $value;
 79:     }
 80: 
 81: 
 82:     /********************* environment variables ****************d*g**/
 83: 
 84: 
 85:     /**
 86:      * Sets the environment variable.
 87:      * @param  string
 88:      * @param  mixed
 89:      * @param  bool
 90:      * @return void
 91:      */
 92:     public static function setVariable($name, $value, $expand = TRUE)
 93:     {
 94:         if ($expand && is_string($value)) {
 95:             $value = self::getContext()->expand($value);
 96:         }
 97:         self::getContext()->parameters[$name] = $value;
 98:     }
 99: 
100: 
101:     /**
102:      * Returns the value of an environment variable or $default if there is no element set.
103:      * @param  string
104:      * @param  mixed  default value to use if key not found
105:      * @return mixed
106:      * @throws InvalidStateException
107:      */
108:     public static function getVariable($name, $default = NULL)
109:     {
110:         if (isset(self::getContext()->parameters[$name])) {
111:             return self::getContext()->parameters[$name];
112:         } elseif (func_num_args() > 1) {
113:             return $default;
114:         } else {
115:             throw new InvalidStateException("Unknown environment variable '$name'.");
116:         }
117:     }
118: 
119: 
120:     /**
121:      * Returns the all environment variables.
122:      * @return array
123:      */
124:     public static function getVariables()
125:     {
126:         return self::getContext()->parameters;
127:     }
128: 
129: 
130:     /**
131:      * Returns expanded variable.
132:      * @param  string
133:      * @return string
134:      * @throws InvalidStateException
135:      */
136:     public static function expand($s)
137:     {
138:         return self::getContext()->expand($s);
139:     }
140: 
141: 
142:     /********************* context ****************d*g**/
143: 
144: 
145:     /**
146:      * Sets initial instance of context.
147:      * @return void
148:      */
149:     public static function setContext(DI\Container $context)
150:     {
151:         if (self::$createdAt) {
152:             throw new Nette\InvalidStateException('Configurator & SystemContainer has already been created automatically by Nette\Environment at ' . self::$createdAt);
153:         }
154:         self::$context = $context;
155:     }
156: 
157: 
158:     /**
159:      * Get initial instance of context.
160:      * @return \SystemContainer|Nette\DI\Container
161:      */
162:     public static function getContext()
163:     {
164:         if (self::$context === NULL) {
165:             self::loadConfig();
166:         }
167:         return self::$context;
168:     }
169: 
170: 
171:     /**
172:      * Gets the service object of the specified type.
173:      * @param  string service name
174:      * @return object
175:      */
176:     public static function getService($name)
177:     {
178:         return self::getContext()->getService($name);
179:     }
180: 
181: 
182:     /**
183:      * Calling to undefined static method.
184:      * @param  string  method name
185:      * @param  array   arguments
186:      * @return object  service
187:      */
188:     public static function __callStatic($name, $args)
189:     {
190:         if (!$args && strncasecmp($name, 'get', 3) === 0) {
191:             return self::getService(lcfirst(substr($name, 3)));
192:         } else {
193:             throw new MemberAccessException("Call to undefined static method Nette\\Environment::$name().");
194:         }
195:     }
196: 
197: 
198:     /**
199:      * @return Nette\Http\Request
200:      */
201:     public static function getHttpRequest()
202:     {
203:         return self::getContext()->getByType('Nette\Http\IRequest');
204:     }
205: 
206: 
207:     /**
208:      * @return Nette\Http\Context
209:      */
210:     public static function getHttpContext()
211:     {
212:         return self::getContext()->getByType('Nette\Http\Context');
213:     }
214: 
215: 
216:     /**
217:      * @return Nette\Http\Response
218:      */
219:     public static function getHttpResponse()
220:     {
221:         return self::getContext()->getByType('Nette\Http\IResponse');
222:     }
223: 
224: 
225:     /**
226:      * @return Nette\Application\Application
227:      */
228:     public static function getApplication()
229:     {
230:         return self::getContext()->getByType('Nette\Application\Application');
231:     }
232: 
233: 
234:     /**
235:      * @return Nette\Security\User
236:      */
237:     public static function getUser()
238:     {
239:         return self::getContext()->getByType('Nette\Security\User');
240:     }
241: 
242: 
243:     /**
244:      * @return Nette\Loaders\RobotLoader
245:      */
246:     public static function getRobotLoader()
247:     {
248:         return self::getContext()->getByType('Nette\Loaders\RobotLoader');
249:     }
250: 
251: 
252:     /********************* service factories ****************d*g**/
253: 
254: 
255:     /**
256:      * @param  string
257:      * @return Nette\Caching\Cache
258:      */
259:     public static function getCache($namespace = '')
260:     {
261:         return new Caching\Cache(self::getContext()->getByType('Nette\Caching\IStorage'), $namespace);
262:     }
263: 
264: 
265:     /**
266:      * Returns instance of session or session namespace.
267:      * @param  string
268:      * @return Nette\Http\Session
269:      */
270:     public static function getSession($namespace = NULL)
271:     {
272:         return $namespace === NULL
273:             ? self::getContext()->getByType('Nette\Http\Session')
274:             : self::getContext()->getByType('Nette\Http\Session')->getSection($namespace);
275:     }
276: 
277: 
278:     /********************* global configuration ****************d*g**/
279: 
280: 
281:     /**
282:      * Loads global configuration from file and process it.
283:      * @param  string
284:      * @param  string
285:      * @return Nette\ArrayHash
286:      */
287:     public static function loadConfig($file = NULL, $section = NULL)
288:     {
289:         if (self::$createdAt) {
290:             throw new Nette\InvalidStateException('Nette\Configurator has already been created automatically by Nette\Environment at ' . self::$createdAt);
291:         } elseif (!defined('TEMP_DIR')) {
292:             throw new Nette\InvalidStateException('Nette\Environment requires constant TEMP_DIR with path to temporary directory.');
293:         }
294:         $configurator = new Nette\Configurator;
295:         $configurator
296:             ->setDebugMode(!self::isProduction())
297:             ->setTempDirectory(TEMP_DIR)
298:             ->addParameters(array('container' => array('class' => 'EnvironmentContainer')));
299:         if ($file) {
300:             $configurator->addConfig($file, $section);
301:         }
302:         self::$context = $configurator->createContainer();
303: 
304:         self::$createdAt = '?';
305:         foreach (debug_backtrace(FALSE) as $row) {
306:             if (isset($row['file']) && $row['file'] !== __FILE__ && is_file($row['file'])) {
307:                 self::$createdAt = "$row[file]:$row[line]";
308:                 break;
309:             }
310:         }
311:         return self::getConfig();
312:     }
313: 
314: 
315:     /**
316:      * Returns the global configuration.
317:      * @param  string key
318:      * @param  mixed  default value
319:      * @return mixed
320:      */
321:     public static function getConfig($key = NULL, $default = NULL)
322:     {
323:         $params = Nette\ArrayHash::from(self::getContext()->parameters);
324:         if (func_num_args()) {
325:             return isset($params[$key]) ? $params[$key] : $default;
326:         } else {
327:             return $params;
328:         }
329:     }
330: 
331: }
332: 
Nette 2.1 API documentation generated by ApiGen 2.8.0