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

Classes

Interfaces

Exceptions

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