Namespaces

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

Classes

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