Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationLatte
      • ApplicationTracy
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsLatte
      • Framework
      • HttpTracy
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • none
  • Tracy

Classes

  • Callback
  • Configurator
  • Environment
  • Framework
  • FreezableObject
  • Object

Interfaces

  • IFreezable

Exceptions

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