Namespaces

  • Nette
    • Application
    • Caching
    • Collections
    • Config
    • Forms
    • IO
    • Loaders
    • Mail
    • Reflection
    • Security
    • Templates
    • Web
  • None
  • PHP

Classes

  • ArrayTools
  • Callback
  • Component
  • ComponentContainer
  • Configurator
  • DateTime
  • Debug
  • Environment
  • Framework
  • FreezableObject
  • GenericRecursiveIterator
  • Image
  • ImageMagick
  • InstanceFilterIterator
  • Object
  • ObjectMixin
  • Paginator
  • RecursiveComponentIterator
  • ServiceLocator
  • SmartCachingIterator
  • String
  • Tools

Interfaces

  • IComponent
  • IComponentContainer
  • IDebuggable
  • IServiceLocator
  • ITranslator

Exceptions

  • AmbiguousServiceException
  • Overview
  • Namespace
  • Class
  • Tree
  • Other releases
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://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:     Nette\Config\Config;
 16: 
 17: 
 18: 
 19: /**
 20:  * Nette\Environment helper.
 21:  *
 22:  * @author     David Grudl
 23:  */
 24: class Configurator extends Object
 25: {
 26:     /** @var string */
 27:     public $defaultConfigFile = '%appDir%/config.ini';
 28: 
 29:     /** @var array */
 30:     public $defaultServices = array(
 31:         'Nette\\Application\\Application' => 'Nette\Application\Application',
 32:         'Nette\\Web\\HttpContext' => 'Nette\Web\HttpContext',
 33:         'Nette\\Web\\IHttpRequest' => 'Nette\Web\HttpRequest',
 34:         'Nette\\Web\\IHttpResponse' => 'Nette\Web\HttpResponse',
 35:         'Nette\\Web\\IUser' => 'Nette\Web\User',
 36:         'Nette\\Caching\\ICacheStorage' => array(__CLASS__, 'createCacheStorage'),
 37:         'Nette\\Web\\Session' => 'Nette\Web\Session',
 38:         'Nette\\Loaders\\RobotLoader' => array(__CLASS__, 'createRobotLoader'),
 39:     );
 40: 
 41: 
 42: 
 43:     /**
 44:      * Detect environment mode.
 45:      * @param  string mode name
 46:      * @return bool
 47:      */
 48:     public function detect($name)
 49:     {
 50:         switch ($name) {
 51:         case 'environment':
 52:             // environment name autodetection
 53:             if ($this->detect('console')) {
 54:                 return Environment::CONSOLE;
 55: 
 56:             } else {
 57:                 return Environment::getMode('production') ? Environment::PRODUCTION : Environment::DEVELOPMENT;
 58:             }
 59: 
 60:         case 'production':
 61:             // detects production mode by server IP address
 62:             if (PHP_SAPI === 'cli') {
 63:                 return FALSE;
 64: 
 65:             } elseif (isset($_SERVER['SERVER_ADDR']) || isset($_SERVER['LOCAL_ADDR'])) {
 66:                 $addr = isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : $_SERVER['LOCAL_ADDR'];
 67:                 $oct = explode('.', $addr);
 68:                 // 10.0.0.0/8   Private network
 69:                 // 127.0.0.0/8  Loopback
 70:                 // 169.254.0.0/16 & ::1  Link-Local
 71:                 // 172.16.0.0/12  Private network
 72:                 // 192.168.0.0/16  Private network
 73:                 return $addr !== '::1' && (count($oct) !== 4 || ($oct[0] !== '10' && $oct[0] !== '127' && ($oct[0] !== '172' || $oct[1] < 16 || $oct[1] > 31)
 74:                     && ($oct[0] !== '169' || $oct[1] !== '254') && ($oct[0] !== '192' || $oct[1] !== '168')));
 75: 
 76:             } else {
 77:                 return TRUE;
 78:             }
 79: 
 80:         case 'console':
 81:             return PHP_SAPI === 'cli';
 82: 
 83:         default:
 84:             // unknown mode
 85:             return NULL;
 86:         }
 87:     }
 88: 
 89: 
 90: 
 91:     /**
 92:      * Loads global configuration from file and process it.
 93:      * @param  string|Nette\Config\Config  file name or Config object
 94:      * @return Nette\Config\Config
 95:      */
 96:     public function loadConfig($file)
 97:     {
 98:         $name = Environment::getName();
 99: 
100:         if ($file instanceof Config) {
101:             $config = $file;
102:             $file = NULL;
103: 
104:         } else {
105:             if ($file === NULL) {
106:                 $file = $this->defaultConfigFile;
107:             }
108:             $file = Environment::expand($file);
109:             $config = Config::fromFile($file, $name, 0);
110:         }
111: 
112:         // process environment variables
113:         if ($config->variable instanceof Config) {
114:             foreach ($config->variable as $key => $value) {
115:                 Environment::setVariable($key, $value);
116:             }
117:         }
118: 
119:         $config->expand();
120: 
121:         // process services
122:         $runServices = array();
123:         $locator = Environment::getServiceLocator();
124:         if ($config->service instanceof Config) {
125:             foreach ($config->service as $key => $value) {
126:                 $key = strtr($key, '-', '\\'); // limited INI chars
127:                 if (is_string($value)) {
128:                     $locator->removeService($key);
129:                     $locator->addService($key, $value);
130:                 } else {
131:                     if ($value->factory) {
132:                         $locator->removeService($key);
133:                         $locator->addService($key, $value->factory, isset($value->singleton) ? $value->singleton : TRUE, (array) $value->option);
134:                     }
135:                     if ($value->run) {
136:                         $runServices[] = $key;
137:                     }
138:                 }
139:             }
140:         }
141: 
142:         // process ini settings
143:         if (!$config->php) { // backcompatibility
144:             $config->php = $config->set;
145:             unset($config->set);
146:         }
147: 
148:         if ($config->php instanceof Config) {
149:             if (PATH_SEPARATOR !== ';' && isset($config->php->include_path)) {
150:                 $config->php->include_path = str_replace(';', PATH_SEPARATOR, $config->php->include_path);
151:             }
152: 
153:             foreach ($config->php as $key => $value) { // flatten INI dots
154:                 if ($value instanceof Config) {
155:                     unset($config->php->$key);
156:                     foreach ($value as $k => $v) {
157:                         $config->php->{"$key.$k"} = $v;
158:                     }
159:                 }
160:             }
161: 
162:             foreach ($config->php as $key => $value) {
163:                 $key = strtr($key, '-', '.'); // backcompatibility
164: 
165:                 if (!is_scalar($value)) {
166:                     throw new \InvalidStateException("Configuration value for directive '$key' is not scalar.");
167:                 }
168: 
169:                 if ($key === 'date.timezone') { // PHP bug #47466
170:                     date_default_timezone_set($value);
171:                 }
172: 
173:                 if (function_exists('ini_set')) {
174:                     ini_set($key, $value);
175:                 } else {
176:                     switch ($key) {
177:                     case 'include_path':
178:                         set_include_path($value);
179:                         break;
180:                     case 'iconv.internal_encoding':
181:                         iconv_set_encoding('internal_encoding', $value);
182:                         break;
183:                     case 'mbstring.internal_encoding':
184:                         mb_internal_encoding($value);
185:                         break;
186:                     case 'date.timezone':
187:                         date_default_timezone_set($value);
188:                         break;
189:                     case 'error_reporting':
190:                         error_reporting($value);
191:                         break;
192:                     case 'ignore_user_abort':
193:                         ignore_user_abort($value);
194:                         break;
195:                     case 'max_execution_time':
196:                         set_time_limit($value);
197:                         break;
198:                     default:
199:                         if (ini_get($key) != $value) { // intentionally ==
200:                             throw new \NotSupportedException('Required function ini_set() is disabled.');
201:                         }
202:                     }
203:                 }
204:             }
205:         }
206: 
207:         // define constants
208:         if ($config->const instanceof Config) {
209:             foreach ($config->const as $key => $value) {
210:                 define($key, $value);
211:             }
212:         }
213: 
214:         // set modes
215:         if (isset($config->mode)) {
216:             foreach($config->mode as $mode => $state) {
217:                 Environment::setMode($mode, $state);
218:             }
219:         }
220: 
221:         // auto-start services
222:         foreach ($runServices as $name) {
223:             $locator->getService($name);
224:         }
225: 
226:         $config->freeze();
227:         return $config;
228:     }
229: 
230: 
231: 
232:     /********************* service factories ****************d*g**/
233: 
234: 
235: 
236:     /**
237:      * Get initial instance of service locator.
238:      * @return IServiceLocator
239:      */
240:     public function createServiceLocator()
241:     {
242:         $locator = new ServiceLocator;
243:         foreach ($this->defaultServices as $name => $service) {
244:             $locator->addService($name, $service);
245:         }
246:         return $locator;
247:     }
248: 
249: 
250: 
251:     /**
252:      * @return Nette\Caching\ICacheStorage
253:      */
254:     public static function createCacheStorage()
255:     {
256:         return new Nette\Caching\FileStorage(Environment::getVariable('tempDir'));
257:     }
258: 
259: 
260: 
261:     /**
262:      * @return Nette\Loaders\RobotLoader
263:      */
264:     public static function createRobotLoader($options)
265:     {
266:         $loader = new Nette\Loaders\RobotLoader;
267:         $loader->autoRebuild = !Environment::isProduction();
268:         //$loader->setCache(Environment::getCache('Nette.RobotLoader'));
269:         $dirs = isset($options['directory']) ? $options['directory'] : array(Environment::getVariable('appDir'), Environment::getVariable('libsDir'));
270:         $loader->addDirectory($dirs);
271:         $loader->register();
272:         return $loader;
273:     }
274: 
275: }
276: 
Nette Framework 0.9.7 API documentation generated by ApiGen 2.3.0