PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/www/libs/nette-dev/Configurator.php

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