PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Nette/Configurator.php

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