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