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:      * Static class - cannot be instantiated.
 41:      */
 42:     final public function __construct()
 43:     {
 44:         throw new NStaticClassException;
 45:     }
 46: 
 47: 
 48:     /********************* environment modes ****************d*g**/
 49: 
 50: 
 51:     /**
 52:      * Detects console (non-HTTP) mode.
 53:      * @return bool
 54:      */
 55:     public static function isConsole()
 56:     {
 57:         return PHP_SAPI === 'cli';
 58:     }
 59: 
 60: 
 61:     /**
 62:      * Determines whether a server is running in production mode.
 63:      * @return bool
 64:      */
 65:     public static function isProduction()
 66:     {
 67:         if (self::$productionMode === NULL) {
 68:             self::$productionMode = !NConfigurator::detectDebugMode();
 69:         }
 70:         return self::$productionMode;
 71:     }
 72: 
 73: 
 74:     /**
 75:      * Enables or disables production mode.
 76:      * @param  bool
 77:      * @return void
 78:      */
 79:     public static function setProductionMode($value = TRUE)
 80:     {
 81:         self::$productionMode = (bool) $value;
 82:     }
 83: 
 84: 
 85:     /********************* environment variables ****************d*g**/
 86: 
 87: 
 88:     /**
 89:      * Sets the environment variable.
 90:      * @param  string
 91:      * @param  mixed
 92:      * @param  bool
 93:      * @return void
 94:      */
 95:     public static function setVariable($name, $value, $expand = TRUE)
 96:     {
 97:         if ($expand && is_string($value)) {
 98:             $value = self::getContext()->expand($value);
 99:         }
100:         self::getContext()->parameters[$name] = $value;
101:     }
102: 
103: 
104:     /**
105:      * Returns the value of an environment variable or $default if there is no element set.
106:      * @param  string
107:      * @param  mixed  default value to use if key not found
108:      * @return mixed
109:      * @throws InvalidStateException
110:      */
111:     public static function getVariable($name, $default = NULL)
112:     {
113:         if (isset(self::getContext()->parameters[$name])) {
114:             return self::getContext()->parameters[$name];
115:         } elseif (func_num_args() > 1) {
116:             return $default;
117:         } else {
118:             throw new InvalidStateException("Unknown environment variable '$name'.");
119:         }
120:     }
121: 
122: 
123:     /**
124:      * Returns the all environment variables.
125:      * @return array
126:      */
127:     public static function getVariables()
128:     {
129:         return self::getContext()->parameters;
130:     }
131: 
132: 
133:     /**
134:      * Returns expanded variable.
135:      * @param  string
136:      * @return string
137:      * @throws InvalidStateException
138:      */
139:     public static function expand($s)
140:     {
141:         return self::getContext()->expand($s);
142:     }
143: 
144: 
145:     /********************* context ****************d*g**/
146: 
147: 
148:     /**
149:      * Sets initial instance of context.
150:      * @return void
151:      */
152:     public static function setContext(NDIContainer $context)
153:     {
154:         if (self::$createdAt) {
155:             throw new InvalidStateException('Configurator & SystemContainer has already been created automatically by NEnvironment at ' . self::$createdAt);
156:         }
157:         self::$context = $context;
158:     }
159: 
160: 
161:     /**
162:      * Get initial instance of context.
163:      * @return SystemContainer|NDIContainer
164:      */
165:     public static function getContext()
166:     {
167:         if (self::$context === NULL) {
168:             self::loadConfig();
169:         }
170:         return self::$context;
171:     }
172: 
173: 
174:     /**
175:      * Gets the service object of the specified type.
176:      * @param  string service name
177:      * @return object
178:      */
179:     public static function getService($name)
180:     {
181:         return self::getContext()->getService($name);
182:     }
183: 
184: 
185:     /**
186:      * Calling to undefined static method.
187:      * @param  string  method name
188:      * @param  array   arguments
189:      * @return object  service
190:      */
191:     public static function __callStatic($name, $args)
192:     {
193:         if (!$args && strncasecmp($name, 'get', 3) === 0) {
194:             return self::getService(lcfirst(substr($name, 3)));
195:         } else {
196:             throw new MemberAccessException("Call to undefined static method NEnvironment::$name().");
197:         }
198:     }
199: 
200: 
201:     /**
202:      * @return NHttpRequest
203:      */
204:     public static function getHttpRequest()
205:     {
206:         return self::getContext()->getByType('IHttpRequest');
207:     }
208: 
209: 
210:     /**
211:      * @return NHttpContext
212:      */
213:     public static function getHttpContext()
214:     {
215:         return self::getContext()->getByType('NHttpContext');
216:     }
217: 
218: 
219:     /**
220:      * @return NHttpResponse
221:      */
222:     public static function getHttpResponse()
223:     {
224:         return self::getContext()->getByType('IHttpResponse');
225:     }
226: 
227: 
228:     /**
229:      * @return NApplication
230:      */
231:     public static function getApplication()
232:     {
233:         return self::getContext()->getByType('NApplication');
234:     }
235: 
236: 
237:     /**
238:      * @return NUser
239:      */
240:     public static function getUser()
241:     {
242:         return self::getContext()->getByType('NUser');
243:     }
244: 
245: 
246:     /**
247:      * @return NRobotLoader
248:      */
249:     public static function getRobotLoader()
250:     {
251:         return self::getContext()->getByType('NRobotLoader');
252:     }
253: 
254: 
255:     /********************* service factories ****************d*g**/
256: 
257: 
258:     /**
259:      * @param  string
260:      * @return NCache
261:      */
262:     public static function getCache($namespace = '')
263:     {
264:         return new NCache(self::getService('cacheStorage'), $namespace);
265:     }
266: 
267: 
268:     /**
269:      * Returns instance of session or session namespace.
270:      * @param  string
271:      * @return NSession
272:      */
273:     public static function getSession($namespace = NULL)
274:     {
275:         return $namespace === NULL
276:             ? self::getService('session')
277:             : self::getService('session')->getSection($namespace);
278:     }
279: 
280: 
281:     /********************* global configuration ****************d*g**/
282: 
283: 
284:     /**
285:      * Loads global configuration from file and process it.
286:      * @param  string
287:      * @param  string
288:      * @return NArrayHash
289:      */
290:     public static function loadConfig($file = NULL, $section = NULL)
291:     {
292:         if (self::$createdAt) {
293:             throw new InvalidStateException('NConfigurator has already been created automatically by NEnvironment at ' . self::$createdAt);
294:         }
295:         $configurator = new NConfigurator;
296:         $configurator
297:             ->setDebugMode(!self::isProduction())
298:             ->setTempDirectory(defined('TEMP_DIR') ? TEMP_DIR : '')
299:             ->addParameters(array('container' => array('class' => 'EnvironmentContainer')));
300:         if ($file) {
301:             $configurator->addConfig($file, $section);
302:         }
303:         self::$context = $configurator->createContainer();
304: 
305:         self::$createdAt = '?';
306:         foreach (PHP_VERSION_ID < 50205 ? debug_backtrace() : 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 = NArrayHash::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.13 (for PHP 5.2, prefixed) API API documentation generated by ApiGen 2.8.0