PageRenderTime 58ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/Nette/Environment/Configurator.php

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