Source for file Configurator.php

Documentation is available at Configurator.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  7. 7:  *
  8. 8:  * This source file is subject to the "Nette license" that is bundled
  9. 9:  * with this package in the file license.txt.
  10. 10:  *
  11. 11:  * For more information please see http://nettephp.com
  12. 12:  *
  13. 13:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  14. 14:  * @license    http://nettephp.com/license  Nette license
  15. 15:  * @link       http://nettephp.com
  16. 16:  * @category   Nette
  17. 17:  * @package    Nette
  18. 18:  */
  19. 19:  
  20. 20:  
  21. 21:  
  22. 22: require_once dirname(__FILE__'/Object.php';
  23. 23:  
  24. 24:  
  25. 25:  
  26. 26: /**
  27. 27:  * Nette\Environment helper.
  28. 28:  *
  29. 29:  * @author     David Grudl
  30. 30:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  31. 31:  * @package    Nette
  32. 32:  */
  33. 33: class Configurator extends Object
  34. 34: {
  35. 35:     /** @var string */
  36. 36:     public $defaultConfigFile = '%appDir%/config.ini';
  37. 37:  
  38. 38:     /** @var array */
  39. 39:     public $defaultServices = array(
  40. 40:         'Nette\Application\Application' => 'Nette\Application\Application',
  41. 41:         'Nette\Web\IHttpRequest' => 'Nette\Web\HttpRequest',
  42. 42:         'Nette\Web\IHttpResponse' => 'Nette\Web\HttpResponse',
  43. 43:         'Nette\Web\IUser' => 'Nette\Web\User',
  44. 44:         'Nette\Caching\ICacheStorage' => array(__CLASS__'createCacheStorage'),
  45. 45:         'Nette\Web\Session' => 'Nette\Web\Session',
  46. 46:     );
  47. 47:  
  48. 48:  
  49. 49:  
  50. 50:     /**
  51. 51:      * Detect environment mode.
  52. 52:      * @param  string mode name
  53. 53:      * @return bool 
  54. 54:      */
  55. 55:     public function detect($name)
  56. 56:     {
  57. 57:         switch ($name{
  58. 58:         case 'environment':
  59. 59:             // environment name autodetection
  60. 60:             if ($this->detect('console')) {
  61. 61:                 return Environment::CONSOLE;
  62. 62:  
  63. 63:             else {
  64. 64:                 return Environment::getMode('production'Environment::PRODUCTION Environment::DEVELOPMENT;
  65. 65:             }
  66. 66:  
  67. 67:         case 'production':
  68. 68:             // detects production mode by server IP address
  69. 69:             if (PHP_SAPI === 'cli'{
  70. 70:                 return FALSE;
  71. 71:  
  72. 72:             elseif (isset($_SERVER['SERVER_ADDR']|| isset($_SERVER['LOCAL_ADDR'])) {
  73. 73:                 $addr isset($_SERVER['SERVER_ADDR']$_SERVER['SERVER_ADDR'$_SERVER['LOCAL_ADDR'];
  74. 74:                 $oct explode('.'$addr);
  75. 75:                 // 10.0.0.0/8   Private network
  76. 76:                 // 127.0.0.0/8  Loopback
  77. 77:                 // 169.254.0.0/16 & ::1  Link-Local
  78. 78:                 // 172.16.0.0/12  Private network
  79. 79:                 // 192.168.0.0/16  Private network
  80. 80:                 return $addr !== '::1' && (count($oct!== || ($oct[0!== '10' && $oct[0!== '127' && ($oct[0!== '172' || $oct[116 || $oct[131)
  81. 81:                     && ($oct[0!== '169' || $oct[1!== '254'&& ($oct[0!== '192' || $oct[1!== '168')));
  82. 82:  
  83. 83:             else {
  84. 84:                 return TRUE;
  85. 85:             }
  86. 86:  
  87. 87:         case 'debug':
  88. 88:             // Determines whether the debugger is active
  89. 89:             if (defined('DEBUG_MODE')) {
  90. 90:                 return (bool) DEBUG_MODE;
  91. 91:  
  92. 92:             else {
  93. 93:                 return !Environment::getMode('production'&& isset($_REQUEST['DBGSESSID']);
  94. 94:                 // function_exists('DebugBreak');
  95. 95:             }
  96. 96:  
  97. 97:         case 'console':
  98. 98:             return PHP_SAPI === 'cli';
  99. 99:  
  100. 100:         default:
  101. 101:             // unknown mode
  102. 102:             return NULL;
  103. 103:         }
  104. 104:     }
  105. 105:  
  106. 106:  
  107. 107:  
  108. 108:     /**
  109. 109:      * Loads global configuration from file and process it.
  110. 110:      * @param  string|Nette\Config\Config file name or Config object
  111. 111:      * @return Config 
  112. 112:      */
  113. 113:     public function loadConfig($file)
  114. 114:     {
  115. 115:         $name Environment::getName();
  116. 116:  
  117. 117:         if ($file instanceof Config{
  118. 118:             $config $file;
  119. 119:             $file NULL;
  120. 120:  
  121. 121:         else {
  122. 122:             if ($file === NULL{
  123. 123:                 $file $this->defaultConfigFile;
  124. 124:             }
  125. 125:             $file Environment::expand($file);
  126. 126:             $config Config::fromFile($file$name0);
  127. 127:         }
  128. 128:  
  129. 129:         // process environment variables
  130. 130:         if ($config->variable instanceof Config{
  131. 131:             foreach ($config->variable as $key => $value{
  132. 132:                 Environment::setVariable($key$value);
  133. 133:             }
  134. 134:         }
  135. 135:  
  136. 136:         $config->expand();
  137. 137:  
  138. 138:         // process services
  139. 139:         $locator Environment::getServiceLocator();
  140. 140:         if ($config->service instanceof Config{
  141. 141:             foreach ($config->service as $key => $value{
  142. 142:                 $locator->addService($valuestrtr($key'-''\\'));
  143. 143:             }
  144. 144:         }
  145. 145:  
  146. 146:         // check temporary directory - TODO: discuss
  147. 147:         /*
  148. 148:         $dir = Environment::getVariable('tempDir');
  149. 149:         if ($dir && !(is_dir($dir) && is_writable($dir))) {
  150. 150:             trigger_error("Temporary directory '$dir' is not writable", E_USER_NOTICE);
  151. 151:         }
  152. 152:         */
  153. 153:  
  154. 154:         // process ini settings
  155. 155:         if (!$config->php// backcompatibility
  156. 156:             $config->php $config->set;
  157. 157:             unset($config->set);
  158. 158:         }
  159. 159:  
  160. 160:         if ($config->php instanceof Config{
  161. 161:             if (PATH_SEPARATOR !== ';' && isset($config->php->include_path)) {
  162. 162:                 $config->php->include_path str_replace(';'PATH_SEPARATOR$config->php->include_path);
  163. 163:             }
  164. 164:  
  165. 165:             foreach ($config->php as $key => $value// flatten INI dots
  166. 166:                 if ($value instanceof Config{
  167. 167:                     unset($config->php->$key);
  168. 168:                     foreach ($value as $k => $v{
  169. 169:                         $config->php->{"$key.$k"$v;
  170. 170:                     }
  171. 171:                 }
  172. 172:             }
  173. 173:  
  174. 174:             foreach ($config->php as $key => $value{
  175. 175:                 $key strtr($key'-''.')// backcompatibility
  176. 176:  
  177. 177:                 if (!is_scalar($value)) {
  178. 178:                     throw new InvalidStateException("Configuration value for directive '$key' is not scalar.");
  179. 179:                 }
  180. 180:  
  181. 181:                 if (function_exists('ini_set')) {
  182. 182:                     ini_set($key$value);
  183. 183:                 else {
  184. 184:                     switch ($key{
  185. 185:                     case 'include_path':
  186. 186:                         set_include_path($value);
  187. 187:                         break;
  188. 188:                     case 'iconv.internal_encoding':
  189. 189:                         iconv_set_encoding('internal_encoding'$value);
  190. 190:                         break;
  191. 191:                     case 'mbstring.internal_encoding':
  192. 192:                         mb_internal_encoding($value);
  193. 193:                         break;
  194. 194:                     case 'date.timezone':
  195. 195:                         date_default_timezone_set($value);
  196. 196:                         break;
  197. 197:                     case 'error_reporting':
  198. 198:                         error_reporting($value);
  199. 199:                         break;
  200. 200:                     case 'ignore_user_abort':
  201. 201:                         ignore_user_abort($value);
  202. 202:                         break;
  203. 203:                     case 'max_execution_time':
  204. 204:                         set_time_limit($value);
  205. 205:                         break;
  206. 206:                     default:
  207. 207:                         if (ini_get($key!= $value// intentionally ==
  208. 208:                             throw new NotSupportedException('Required function ini_set() is disabled.');
  209. 209:                         }
  210. 210:                     }
  211. 211:                 }
  212. 212:             }
  213. 213:         }
  214. 214:  
  215. 215:         // define constants
  216. 216:         if ($config->const instanceof Config{
  217. 217:             foreach ($config->const as $key => $value{
  218. 218:                 define($key$value);
  219. 219:             }
  220. 220:         }
  221. 221:  
  222. 222:         // set modes
  223. 223:         if (isset($config->mode)) {
  224. 224:             foreach($config->mode as $mode => $state{
  225. 225:                 Environment::setMode($mode$state);
  226. 226:             }
  227. 227:         }
  228. 228:  
  229. 229:         $config->freeze();
  230. 230:         return $config;
  231. 231:     }
  232. 232:  
  233. 233:  
  234. 234:  
  235. 235:     /********************* service factories ****************d*g**/
  236. 236:  
  237. 237:  
  238. 238:  
  239. 239:     /**
  240. 240:      * Get initial instance of service locator.
  241. 241:      * @return IServiceLocator 
  242. 242:      */
  243. 243:     public function createServiceLocator()
  244. 244:     {
  245. 245:         $locator new ServiceLocator;
  246. 246:         foreach ($this->defaultServices as $name => $service{
  247. 247:             $locator->addService($service$name);
  248. 248:         }
  249. 249:         return $locator;
  250. 250:     }
  251. 251:  
  252. 252:  
  253. 253:  
  254. 254:     /**
  255. 255:      * @return ICacheStorage 
  256. 256:      */
  257. 257:     public static function createCacheStorage()
  258. 258:     {
  259. 259:         return new FileStorage(Environment::getVariable('tempDir'));
  260. 260:     }
  261. 261: