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