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 https://nette.org
  12. 12:  *
  13. 13:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  14. 14:  * @license    https://nette.org/license  Nette license
  15. 15:  * @link       https://nette.org
  16. 16:  * @category   Nette
  17. 17:  * @package    Nette
  18. 18:  * @version    $Id$
  19. 19:  */
  20. 20:  
  21. 21:  
  22. 22:  
  23. 23: require_once dirname(__FILE__'/Object.php';
  24. 24:  
  25. 25:  
  26. 26:  
  27. 27: /**
  28. 28:  * Nette\Environment helper.
  29. 29:  *
  30. 30:  * @author     David Grudl
  31. 31:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  32. 32:  * @package    Nette
  33. 33:  */
  34. 34: class Configurator extends Object
  35. 35: {
  36. 36:     /** @var string */
  37. 37:     public $defaultConfigFile = '%appDir%/config.ini';
  38. 38:  
  39. 39:     /** @var array */
  40. 40:     public $defaultServices = array(
  41. 41:         'Nette\Application\Application' => 'Nette\Application\Application',
  42. 42:         'Nette\Web\IHttpRequest' => 'Nette\Web\HttpRequest',
  43. 43:         'Nette\Web\IHttpResponse' => 'Nette\Web\HttpResponse',
  44. 44:         'Nette\Web\IUser' => 'Nette\Web\User',
  45. 45:         'Nette\Caching\ICacheStorage' => array(__CLASS__'createCacheStorage'),
  46. 46:         'Nette\Web\Session' => 'Nette\Web\Session',
  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 'debug':
  89. 89:             // Determines whether the debugger is active
  90. 90:             if (defined('DEBUG_MODE')) {
  91. 91:                 return (bool) DEBUG_MODE;
  92. 92:  
  93. 93:             else {
  94. 94:                 return !Environment::getMode('production'&& isset($_REQUEST['DBGSESSID']);
  95. 95:                 // function_exists('DebugBreak');
  96. 96:             }
  97. 97:  
  98. 98:         case 'console':
  99. 99:             return PHP_SAPI === 'cli';
  100. 100:  
  101. 101:         default:
  102. 102:             // unknown mode
  103. 103:             return NULL;
  104. 104:         }
  105. 105:     }
  106. 106:  
  107. 107:  
  108. 108:  
  109. 109:     /**
  110. 110:      * Loads global configuration from file and process it.
  111. 111:      * @param  string|Nette\Config\Config file name or Config object
  112. 112:      * @return Config 
  113. 113:      */
  114. 114:     public function loadConfig($file)
  115. 115:     {
  116. 116:         $name Environment::getName();
  117. 117:  
  118. 118:         if ($file instanceof Config{
  119. 119:             $config $file;
  120. 120:             $file NULL;
  121. 121:  
  122. 122:         else {
  123. 123:             if ($file === NULL{
  124. 124:                 $file $this->defaultConfigFile;
  125. 125:             }
  126. 126:             $file Environment::expand($file);
  127. 127:             $config Config::fromFile($file$name0);
  128. 128:         }
  129. 129:  
  130. 130:         // process environment variables
  131. 131:         if ($config->variable instanceof Config{
  132. 132:             foreach ($config->variable as $key => $value{
  133. 133:                 Environment::setVariable($key$value);
  134. 134:             }
  135. 135:         }
  136. 136:  
  137. 137:         $config->expand();
  138. 138:  
  139. 139:         // process services
  140. 140:         $locator Environment::getServiceLocator();
  141. 141:         if ($config->service instanceof Config{
  142. 142:             foreach ($config->service as $key => $value{
  143. 143:                 $locator->addService($valuestrtr($key'-''\\'));
  144. 144:             }
  145. 145:         }
  146. 146:  
  147. 147:         // check temporary directory - TODO: discuss
  148. 148:         /*
  149. 149:         $dir = Environment::getVariable('tempDir');
  150. 150:         if ($dir && !(is_dir($dir) && is_writable($dir))) {
  151. 151:             trigger_error("Temporary directory '$dir' is not writable", E_USER_NOTICE);
  152. 152:         }
  153. 153:         */
  154. 154:  
  155. 155:         // process ini settings
  156. 156:         if ($config->set instanceof Config{
  157. 157:             if (PATH_SEPARATOR !== ';' && isset($config->set->include_path)) {
  158. 158:                 $config->set->include_path str_replace(';'PATH_SEPARATOR$config->set->include_path);
  159. 159:             }
  160. 160:  
  161. 161:             foreach ($config->set as $key => $value{
  162. 162:                 $key strtr($key'-''.')// old INI compatibility
  163. 163:  
  164. 164:                 if (!is_scalar($value)) {
  165. 165:                     throw new InvalidStateException("Configuration value for directive '$key' is not scalar.");
  166. 166:                 }
  167. 167:  
  168. 168:                 if (function_exists('ini_set')) {
  169. 169:                     ini_set($key$value);
  170. 170:                 else {
  171. 171:                     switch ($key{
  172. 172:                     case 'include_path':
  173. 173:                         set_include_path($value);
  174. 174:                         break;
  175. 175:                     case 'iconv.internal_encoding':
  176. 176:                         iconv_set_encoding('internal_encoding'$value);
  177. 177:                         break;
  178. 178:                     case 'mbstring.internal_encoding':
  179. 179:                         mb_internal_encoding($value);
  180. 180:                         break;
  181. 181:                     case 'date.timezone':
  182. 182:                         date_default_timezone_set($value);
  183. 183:                         break;
  184. 184:                     case 'error_reporting':
  185. 185:                         error_reporting($value);
  186. 186:                         break;
  187. 187:                     case 'ignore_user_abort':
  188. 188:                         ignore_user_abort($value);
  189. 189:                         break;
  190. 190:                     case 'max_execution_time':
  191. 191:                         set_time_limit($value);
  192. 192:                         break;
  193. 193:                     default:
  194. 194:                         if (ini_get($key!= $value// intentionally ==
  195. 195:                             throw new NotSupportedException('Required function ini_set() is disabled.');
  196. 196:                         }
  197. 197:                     }
  198. 198:                 }
  199. 199:             }
  200. 200:         }
  201. 201:  
  202. 202:         // define constants
  203. 203:         if ($config->const instanceof Config{
  204. 204:             foreach ($config->const as $key => $value{
  205. 205:                 define($key$value);
  206. 206:             }
  207. 207:         }
  208. 208:  
  209. 209:         // set modes
  210. 210:         if (isset($config->mode)) {
  211. 211:             foreach($config->mode as $mode => $state{
  212. 212:                 Environment::setMode($mode$state);
  213. 213:             }
  214. 214:         }
  215. 215:  
  216. 216:         $config->setReadOnly();
  217. 217:         return $config;
  218. 218:     }
  219. 219:  
  220. 220:  
  221. 221:  
  222. 222:     /********************* service factories ****************d*g**/
  223. 223:  
  224. 224:  
  225. 225:  
  226. 226:     /**
  227. 227:      * Get initial instance of service locator.
  228. 228:      * @return IServiceLocator 
  229. 229:      */
  230. 230:     public function createServiceLocator()
  231. 231:     {
  232. 232:         $locator new ServiceLocator;
  233. 233:         foreach ($this->defaultServices as $name => $service{
  234. 234:             $locator->addService($service$name);
  235. 235:         }
  236. 236:         return $locator;
  237. 237:     }
  238. 238:  
  239. 239:  
  240. 240:  
  241. 241:     /**
  242. 242:      * @return ICacheStorage 
  243. 243:      */
  244. 244:     public static function createCacheStorage()
  245. 245:     {
  246. 246:         return new FileStorage(Environment::getVariable('cacheBase'));
  247. 247:     }
  248. 248: