Packages

  • 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

  • NArrayHash
  • NArrayList
  • NCallback
  • NDateTime53
  • NEnvironment
  • NFramework
  • NFreezableObject
  • NImage
  • NObject
  • NObjectMixin

Interfaces

  • IFreezable

Exceptions

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