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:         // process ini settings
  140. 140:         if (!$config->php// backcompatibility
  141. 141:             $config->php $config->set;
  142. 142:             unset($config->set);
  143. 143:         }
  144. 144:  
  145. 145:         if ($config->php instanceof Config{
  146. 146:             if (PATH_SEPARATOR !== ';' && isset($config->php->include_path)) {
  147. 147:                 $config->php->include_path str_replace(';'PATH_SEPARATOR$config->php->include_path);
  148. 148:             }
  149. 149:  
  150. 150:             foreach ($config->php as $key => $value// flatten INI dots
  151. 151:                 if ($value instanceof Config{
  152. 152:                     unset($config->php->$key);
  153. 153:                     foreach ($value as $k => $v{
  154. 154:                         $config->php->{"$key.$k"$v;
  155. 155:                     }
  156. 156:                 }
  157. 157:             }
  158. 158:  
  159. 159:             foreach ($config->php as $key => $value{
  160. 160:                 $key strtr($key'-''.')// backcompatibility
  161. 161:  
  162. 162:                 if (!is_scalar($value)) {
  163. 163:                     throw new InvalidStateException("Configuration value for directive '$key' is not scalar.");
  164. 164:                 }
  165. 165:  
  166. 166:                 if (function_exists('ini_set')) {
  167. 167:                     ini_set($key$value);
  168. 168:                 else {
  169. 169:                     switch ($key{
  170. 170:                     case 'include_path':
  171. 171:                         set_include_path($value);
  172. 172:                         break;
  173. 173:                     case 'iconv.internal_encoding':
  174. 174:                         iconv_set_encoding('internal_encoding'$value);
  175. 175:                         break;
  176. 176:                     case 'mbstring.internal_encoding':
  177. 177:                         mb_internal_encoding($value);
  178. 178:                         break;
  179. 179:                     case 'date.timezone':
  180. 180:                         date_default_timezone_set($value);
  181. 181:                         break;
  182. 182:                     case 'error_reporting':
  183. 183:                         error_reporting($value);
  184. 184:                         break;
  185. 185:                     case 'ignore_user_abort':
  186. 186:                         ignore_user_abort($value);
  187. 187:                         break;
  188. 188:                     case 'max_execution_time':
  189. 189:                         set_time_limit($value);
  190. 190:                         break;
  191. 191:                     default:
  192. 192:                         if (ini_get($key!= $value// intentionally ==
  193. 193:                             throw new NotSupportedException('Required function ini_set() is disabled.');
  194. 194:                         }
  195. 195:                     }
  196. 196:                 }
  197. 197:             }
  198. 198:         }
  199. 199:  
  200. 200:         // define constants
  201. 201:         if ($config->const instanceof Config{
  202. 202:             foreach ($config->const as $key => $value{
  203. 203:                 define($key$value);
  204. 204:             }
  205. 205:         }
  206. 206:  
  207. 207:         // set modes
  208. 208:         if (isset($config->mode)) {
  209. 209:             foreach($config->mode as $mode => $state{
  210. 210:                 Environment::setMode($mode$state);
  211. 211:             }
  212. 212:         }
  213. 213:  
  214. 214:         // auto-start services
  215. 215:         foreach ($runServices as $name{
  216. 216:             $locator->getService($name);
  217. 217:         }
  218. 218:  
  219. 219:         $config->freeze();
  220. 220:         return $config;
  221. 221:     }
  222. 222:  
  223. 223:  
  224. 224:  
  225. 225:     /********************* service factories ****************d*g**/
  226. 226:  
  227. 227:  
  228. 228:  
  229. 229:     /**
  230. 230:      * Get initial instance of service locator.
  231. 231:      * @return IServiceLocator 
  232. 232:      */
  233. 233:     public function createServiceLocator()
  234. 234:     {
  235. 235:         $locator new ServiceLocator;
  236. 236:         foreach ($this->defaultServices as $name => $service{
  237. 237:             $locator->addService($name$service);
  238. 238:         }
  239. 239:         return $locator;
  240. 240:     }
  241. 241:  
  242. 242:  
  243. 243:  
  244. 244:     /**
  245. 245:      * @return ICacheStorage 
  246. 246:      */
  247. 247:     public static function createCacheStorage()
  248. 248:     {
  249. 249:         return new FileStorage(Environment::getVariable('tempDir'));
  250. 250:     }
  251. 251:  
  252. 252:  
  253. 253:  
  254. 254:     /**
  255. 255:      * @return RobotLoader 
  256. 256:      */
  257. 257:     public static function createRobotLoader($options)
  258. 258:     {
  259. 259:         $loader new RobotLoader;
  260. 260:         $loader->autoRebuild !Environment::isProduction();
  261. 261:         //$loader->setCache(Environment::getCache('Nette.RobotLoader'));
  262. 262:         $dirs isset($options['directory']$options['directory'array(Environment::getVariable('appDir')Environment::getVariable('libsDir'));
  263. 263:         $loader->addDirectory($dirs);
  264. 264:         $loader->register();
  265. 265:         return $loader;
  266. 266:     }
  267. 267: