PageRenderTime 37ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/ThinkPHP/Lib/Core/Think.class.php

http://thinkphp.googlecode.com/
PHP | 307 lines | 153 code | 22 blank | 132 comment | 41 complexity | 142ac9fa2076b1719d867657d421a032 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. // $Id: Think.class.php 2929 2012-05-02 06:45:47Z liu21st@gmail.com $
  12. /**
  13. +------------------------------------------------------------------------------
  14. * ThinkPHP Portal?
  15. +------------------------------------------------------------------------------
  16. * @category Think
  17. * @package Think
  18. * @subpackage Core
  19. * @author liu21st <liu21st@gmail.com>
  20. * @version $Id: Think.class.php 2929 2012-05-02 06:45:47Z liu21st@gmail.com $
  21. +------------------------------------------------------------------------------
  22. */
  23. class Think {
  24. private static $_instance = array();
  25. /**
  26. +----------------------------------------------------------
  27. * ???????
  28. +----------------------------------------------------------
  29. * @access public
  30. +----------------------------------------------------------
  31. * @return void
  32. +----------------------------------------------------------
  33. */
  34. static public function start() {
  35. // ?????????
  36. set_error_handler(array('Think','appError'));
  37. set_exception_handler(array('Think','appException'));
  38. // ??AUTOLOAD??
  39. spl_autoload_register(array('Think', 'autoload'));
  40. //[RUNTIME]
  41. Think::buildApp(); // ?????
  42. //[/RUNTIME]
  43. // ????
  44. App::run();
  45. return ;
  46. }
  47. //[RUNTIME]
  48. /**
  49. +----------------------------------------------------------
  50. * ?????? ????
  51. +----------------------------------------------------------
  52. * @access private
  53. +----------------------------------------------------------
  54. * @return string
  55. +----------------------------------------------------------
  56. */
  57. static private function buildApp() {
  58. // ??????????
  59. C(include THINK_PATH.'Conf/convention.php');
  60. // ??????
  61. if(defined('MODE_NAME')) { // ???????????
  62. $mode = include MODE_PATH.strtolower(MODE_NAME).'.php';
  63. }else{
  64. $mode = array();
  65. }
  66. // ????????
  67. if(isset($mode['config'])) {
  68. C( is_array($mode['config'])?$mode['config']:include $mode['config'] );
  69. }
  70. // ????????
  71. if(is_file(CONF_PATH.'config.php'))
  72. C(include CONF_PATH.'config.php');
  73. // ?????????
  74. L(include THINK_PATH.'Lang/'.strtolower(C('DEFAULT_LANG')).'.php');
  75. // ??????????
  76. if(C('APP_TAGS_ON')) {
  77. if(isset($mode['extends'])) {
  78. C('extends',is_array($mode['extends'])?$mode['extends']:include $mode['extends']);
  79. }else{ // ????????????
  80. C('extends', include THINK_PATH.'Conf/tags.php');
  81. }
  82. }
  83. // ????????
  84. if(isset($mode['tags'])) {
  85. C('tags', is_array($mode['tags'])?$mode['tags']:include $mode['tags']);
  86. }elseif(is_file(CONF_PATH.'tags.php')){
  87. // ???????????tags????
  88. C('tags', include CONF_PATH.'tags.php');
  89. }
  90. $compile = '';
  91. // ??????????
  92. if(isset($mode['core'])) {
  93. $list = $mode['core'];
  94. }else{
  95. $list = array(
  96. THINK_PATH.'Common/functions.php', // ???????
  97. CORE_PATH.'Core/Log.class.php', // ?????
  98. CORE_PATH.'Core/Dispatcher.class.php', // URL???
  99. CORE_PATH.'Core/App.class.php', // ?????
  100. CORE_PATH.'Core/Action.class.php', // ????
  101. CORE_PATH.'Core/View.class.php', // ???
  102. );
  103. }
  104. // ????????????
  105. if(is_file(CONF_PATH.'core.php')) {
  106. $list = array_merge($list,include CONF_PATH.'core.php');
  107. }
  108. foreach ($list as $file){
  109. if(is_file($file)) {
  110. require_cache($file);
  111. if(!APP_DEBUG) $compile .= compile($file);
  112. }
  113. }
  114. // ????????
  115. if(is_file(COMMON_PATH.'common.php')) {
  116. include COMMON_PATH.'common.php';
  117. // ????
  118. if(!APP_DEBUG) $compile .= compile(COMMON_PATH.'common.php');
  119. }
  120. // ????????
  121. if(isset($mode['alias'])) {
  122. $alias = is_array($mode['alias'])?$mode['alias']:include $mode['alias'];
  123. alias_import($alias);
  124. if(!APP_DEBUG) $compile .= 'alias_import('.var_export($alias,true).');';
  125. }
  126. // ????????
  127. if(is_file(CONF_PATH.'alias.php')){
  128. $alias = include CONF_PATH.'alias.php';
  129. alias_import($alias);
  130. if(!APP_DEBUG) $compile .= 'alias_import('.var_export($alias,true).');';
  131. }
  132. if(APP_DEBUG) {
  133. // ???????????????
  134. C(include THINK_PATH.'Conf/debug.php');
  135. // ???????????
  136. $status = C('APP_STATUS');
  137. // ???????????
  138. if(is_file(CONF_PATH.$status.'.php'))
  139. // ??????????????
  140. C(include CONF_PATH.$status.'.php');
  141. }else{
  142. // ????????????
  143. build_runtime_cache($compile);
  144. }
  145. return ;
  146. }
  147. //[/RUNTIME]
  148. /**
  149. +----------------------------------------------------------
  150. * ??????ThinkPHP??
  151. * ????????????
  152. +----------------------------------------------------------
  153. * @param string $class ????
  154. +----------------------------------------------------------
  155. * @return void
  156. +----------------------------------------------------------
  157. */
  158. public static function autoload($class) {
  159. // ??????????
  160. if(alias_import($class)) return ;
  161. if(substr($class,-8)=='Behavior') { // ????
  162. if(require_cache(CORE_PATH.'Behavior/'.$class.'.class.php')
  163. || require_cache(EXTEND_PATH.'Behavior/'.$class.'.class.php')
  164. || require_cache(LIB_PATH.'Behavior/'.$class.'.class.php')
  165. || (defined('MODE_NAME') && require_cache(MODE_PATH.ucwords(MODE_NAME).'/Behavior/'.$class.'.class.php'))) {
  166. return ;
  167. }
  168. }elseif(substr($class,-5)=='Model'){ // ????
  169. if((defined('GROUP_NAME') && require_cache(LIB_PATH.'Model/'.GROUP_NAME.'/'.$class.'.class.php'))
  170. || require_cache(LIB_PATH.'Model/'.$class.'.class.php')
  171. || require_cache(EXTEND_PATH.'Model/'.$class.'.class.php') ) {
  172. return ;
  173. }
  174. }elseif(substr($class,-6)=='Action'){ // ?????
  175. if((defined('GROUP_NAME') && require_cache(LIB_PATH.'Action/'.GROUP_NAME.'/'.$class.'.class.php'))
  176. || require_cache(LIB_PATH.'Action/'.$class.'.class.php')
  177. || require_cache(EXTEND_PATH.'Action/'.$class.'.class.php') ) {
  178. return ;
  179. }
  180. }
  181. // ????????????????
  182. $paths = explode(',',C('APP_AUTOLOAD_PATH'));
  183. foreach ($paths as $path){
  184. if(import($path.'.'.$class))
  185. // ??????????
  186. return ;
  187. }
  188. }
  189. /**
  190. +----------------------------------------------------------
  191. * ?????? ??????????
  192. +----------------------------------------------------------
  193. * @param string $class ????
  194. * @param string $method ???????
  195. +----------------------------------------------------------
  196. * @return object
  197. +----------------------------------------------------------
  198. */
  199. static public function instance($class,$method='') {
  200. $identify = $class.$method;
  201. if(!isset(self::$_instance[$identify])) {
  202. if(class_exists($class)){
  203. $o = new $class();
  204. if(!empty($method) && method_exists($o,$method))
  205. self::$_instance[$identify] = call_user_func_array(array(&$o, $method));
  206. else
  207. self::$_instance[$identify] = $o;
  208. }
  209. else
  210. halt(L('_CLASS_NOT_EXIST_').':'.$class);
  211. }
  212. return self::$_instance[$identify];
  213. }
  214. /**
  215. +----------------------------------------------------------
  216. * ???????
  217. +----------------------------------------------------------
  218. * @access public
  219. +----------------------------------------------------------
  220. * @param mixed $e ????
  221. +----------------------------------------------------------
  222. */
  223. static public function appException($e) {
  224. halt($e->__toString());
  225. }
  226. /**
  227. +----------------------------------------------------------
  228. * ???????
  229. +----------------------------------------------------------
  230. * @access public
  231. +----------------------------------------------------------
  232. * @param int $errno ????
  233. * @param string $errstr ????
  234. * @param string $errfile ????
  235. * @param int $errline ????
  236. +----------------------------------------------------------
  237. * @return void
  238. +----------------------------------------------------------
  239. */
  240. static public function appError($errno, $errstr, $errfile, $errline) {
  241. switch ($errno) {
  242. case E_ERROR:
  243. case E_USER_ERROR:
  244. $errorStr = "[$errno] $errstr ".basename($errfile)." ? $errline ?.";
  245. if(C('LOG_RECORD')) Log::write($errorStr,Log::ERR);
  246. halt($errorStr);
  247. break;
  248. case E_STRICT:
  249. case E_USER_WARNING:
  250. case E_USER_NOTICE:
  251. default:
  252. $errorStr = "[$errno] $errstr ".basename($errfile)." ? $errline ?.";
  253. Log::record($errorStr,Log::NOTICE);
  254. break;
  255. }
  256. }
  257. /**
  258. +----------------------------------------------------------
  259. * ??????
  260. +----------------------------------------------------------
  261. * @access public
  262. +----------------------------------------------------------
  263. * @param $name ????
  264. * @param $value ???
  265. +----------------------------------------------------------
  266. */
  267. public function __set($name ,$value) {
  268. if(property_exists($this,$name))
  269. $this->$name = $value;
  270. }
  271. /**
  272. +----------------------------------------------------------
  273. * ??????
  274. +----------------------------------------------------------
  275. * @access public
  276. +----------------------------------------------------------
  277. * @param $name ????
  278. +----------------------------------------------------------
  279. * @return mixed
  280. +----------------------------------------------------------
  281. */
  282. public function __get($name) {
  283. return isset($this->$name)?$this->$name:null;
  284. }
  285. }