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