loader.class.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. defined('IN_IA') or exit('Access Denied');
  7. function load() {
  8. static $loader;
  9. if(empty($loader)) {
  10. $loader = new Loader();
  11. }
  12. return $loader;
  13. }
  14. function table($name) {
  15. $table_classname = "\\We7\\Table\\";
  16. $subsection_name = explode('_', $name);
  17. if (count($subsection_name) == 1) {
  18. $table_classname .= ucfirst($subsection_name[0]) . "\\" . ucfirst($subsection_name[0]);
  19. } else {
  20. foreach ($subsection_name as $key => $val) {
  21. if ($key == 0) {
  22. $table_classname .= ucfirst($val) . '\\';
  23. } else {
  24. $table_classname .= ucfirst($val);
  25. }
  26. }
  27. }
  28. if (in_array($name, array(
  29. 'account_xzapp',
  30. 'account_aliapp',
  31. 'account_baiduapp',
  32. 'account_toutiaoapp',
  33. 'account_wxapp',
  34. 'account_phoneapp',
  35. 'article_category',
  36. 'article_news',
  37. 'article_notice',
  38. 'article_comment',
  39. 'core_profile_fields',
  40. 'core_sendsms_log',
  41. 'core_attachment',
  42. 'core_attachment_group',
  43. 'core_settings',
  44. 'core_cover_reply',
  45. 'core_message_notice_log',
  46. 'core_menu_shortcut',
  47. 'mc_credits_record',
  48. 'mc_mapping_fans',
  49. 'mc_members',
  50. 'mc_member_fields',
  51. 'mc_fans_groups',
  52. 'mc_oauth_fans',
  53. 'mc_fans_tag',
  54. 'modules_rank',
  55. 'modules_bindings',
  56. 'modules_plugin',
  57. 'modules_plugin_rank',
  58. 'modules_cloud',
  59. 'modules_recycle',
  60. 'modules',
  61. 'modules_ignore',
  62. 'phoneapp_versions',
  63. 'qrcode_stat',
  64. 'rule',
  65. 'rule_keyword',
  66. 'site_article_comment',
  67. 'site_templates',
  68. 'site_multi',
  69. 'site_nav',
  70. 'site_styles',
  71. 'site_styles_vars',
  72. 'stat_visit',
  73. 'stat_visit_ip',
  74. 'system_stat_visit',
  75. 'uni_account_modules',
  76. 'uni_account_users',
  77. 'uni_account_modules_shortcut',
  78. 'uni_verifycode',
  79. 'uni_group',
  80. 'uni_modules',
  81. 'userapi_reply',
  82. 'userapi_cache',
  83. 'users',
  84. 'users_group',
  85. 'users_profile',
  86. 'users_bind',
  87. 'users_create_group',
  88. 'users_extra_group',
  89. 'users_extra_limit',
  90. 'users_extra_modules',
  91. 'users_extra_templates',
  92. 'users_lastuse',
  93. 'users_founder_group',
  94. 'users_founder_own_users',
  95. 'users_founder_own_users_groups',
  96. 'users_founder_own_uni_groups',
  97. 'users_founder_own_create_groups',
  98. 'users_permission',
  99. 'wechat_news',
  100. 'wechat_attachment',
  101. 'wxapp_versions',
  102. 'uni_link_uniacid',
  103. 'store_goods',
  104. 'store_order',
  105. 'store_goods_cloud',
  106. ))) {
  107. return new $table_classname;
  108. }
  109. load()->classs('table');
  110. load()->table($name);
  111. $service = false;
  112. $class_name = "{$name}Table";
  113. if (class_exists($class_name)) {
  114. $service = new $class_name();
  115. }
  116. return $service;
  117. }
  118. class Loader {
  119. private $cache = array();
  120. private $singletonObject = array();
  121. private $libraryMap = array(
  122. 'agent' => 'agent/agent.class',
  123. 'captcha' => 'captcha/captcha.class',
  124. 'pdo' => 'pdo/PDO.class',
  125. 'qrcode' => 'qrcode/phpqrcode',
  126. 'ftp' => 'ftp/ftp',
  127. 'pinyin' => 'pinyin/pinyin',
  128. 'pkcs7' => 'pkcs7/pkcs7Encoder',
  129. 'json' => 'json/JSON',
  130. 'phpmailer' => 'phpmailer/PHPMailerAutoload',
  131. 'oss' => 'alioss/autoload',
  132. 'qiniu' => 'qiniu/autoload',
  133. 'cos' => 'cosv4.2/include',
  134. 'cosv3' => 'cos/include',
  135. 'sentry' => 'sentry/Raven/Autoloader',
  136. );
  137. private $loadTypeMap = array(
  138. 'func' => '/framework/function/%s.func.php',
  139. 'model' => '/framework/model/%s.mod.php',
  140. 'classs' => '/framework/class/%s.class.php',
  141. 'library' => '/framework/library/%s.php',
  142. 'table' => '/framework/table/%s.table.php',
  143. 'web' => '/web/common/%s.func.php',
  144. 'app' => '/app/common/%s.func.php',
  145. );
  146. private $accountMap = array(
  147. 'pay' => 'pay/pay',
  148. 'account' => 'account/account',
  149. 'weixin.account' => 'account/weixin.account',
  150. 'weixin.platform' => 'account/weixin.platform',
  151. 'aliapp.account' => 'account/aliapp.account',
  152. 'baiduapp.account' => 'account/baiduapp.account',
  153. 'toutiaoapp.account' => 'account/toutiaoapp.account',
  154. 'phoneapp.account' => 'account/phoneapp.account',
  155. 'webapp.account' => 'account/webapp.account',
  156. 'wxapp.account' => 'account/wxapp.account',
  157. 'wxapp.platform' => 'account/wxapp.platform',
  158. 'wxapp.work' => 'account/wxapp.work',
  159. 'xzapp.account' => 'account/xzapp.account',
  160. 'xzapp.platform' => 'account/xzapp.platform',
  161. );
  162. public function __construct() {
  163. $this->registerAutoload();
  164. }
  165. public function registerAutoload() {
  166. spl_autoload_register(array($this, 'autoload'));
  167. }
  168. public function autoload($class) {
  169. $section = array(
  170. 'Table' => '/framework/table/',
  171. );
  172. $classmap = array(
  173. 'We7Table' => 'table',
  174. );
  175. if (isset($classmap[$class])) {
  176. load()->classs($classmap[$class]);
  177. } elseif (preg_match('/^[0-9a-zA-Z\-\\\\_]+$/', $class)
  178. && (stripos($class, 'We7') === 0 || stripos($class, '\We7') === 0)
  179. && stripos($class, "\\") !== false) {
  180. $group = explode("\\", $class);
  181. $path = IA_ROOT . $section[$group[1]];
  182. unset($group[0]);
  183. unset($group[1]);
  184. $file_path = $path . implode('/', $group) . '.php';
  185. if(is_file($file_path)) {
  186. include $file_path;
  187. }
  188. $file_path = $path . 'Core/' . implode('', $group) . '.php';
  189. if(is_file($file_path)) {
  190. include $file_path;
  191. }
  192. }
  193. }
  194. public function __call($type, $params) {
  195. global $_W;
  196. $name = $cachekey = array_shift($params);
  197. if (!empty($this->cache[$type]) && isset($this->cache[$type][$cachekey])) {
  198. return true;
  199. }
  200. if (empty($this->loadTypeMap[$type])) {
  201. return true;
  202. }
  203. if ($type == 'library' && !empty($this->libraryMap[$name])) {
  204. $name = $this->libraryMap[$name];
  205. }
  206. if ($type == 'classs' && !empty($this->accountMap[$name])) {
  207. $filename = sprintf($this->loadTypeMap[$type], $this->accountMap[$name]);
  208. if (file_exists(IA_ROOT . $filename)) {
  209. $name = $this->accountMap[$name];
  210. }
  211. }
  212. $file = sprintf($this->loadTypeMap[$type], $name);
  213. if (file_exists(IA_ROOT . $file)) {
  214. include IA_ROOT . $file;
  215. $this->cache[$type][$cachekey] = true;
  216. }
  217. return true;
  218. }
  219. function singleton($name) {
  220. if (isset($this->singletonObject[$name])) {
  221. return $this->singletonObject[$name];
  222. }
  223. $this->singletonObject[$name] = $this->object($name);
  224. return $this->singletonObject[$name];
  225. }
  226. function object($name) {
  227. $this->classs(strtolower($name));
  228. if (class_exists($name)) {
  229. return new $name();
  230. } else {
  231. return false;
  232. }
  233. }
  234. }