Packages

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

Classes

  • NArrayTools
  • NCallback
  • NComponent
  • NComponentContainer
  • NConfigurator
  • NDateTime53
  • NDebug
  • NEnvironment
  • NFramework
  • NFreezableObject
  • NGenericRecursiveIterator
  • NImage
  • NImageMagick
  • NInstanceFilterIterator
  • NObject
  • NObjectMixin
  • NPaginator
  • NRecursiveComponentIterator
  • NServiceLocator
  • NSmartCachingIterator
  • NString
  • NTools

Interfaces

  • IComponent
  • IComponentContainer
  • IDebuggable
  • IServiceLocator
  • ITranslator

Exceptions

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