人人商城

loader.class.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. 'modules_rank',
  30. 'modules_bindings',
  31. 'modules_plugin',
  32. 'modules_cloud',
  33. 'modules_recycle',
  34. 'modules',
  35. 'modules_ignore',
  36. 'account_xzapp',
  37. 'account_aliapp',
  38. 'account_wxapp',
  39. 'uni_account_modules',
  40. 'system_stat_visit',
  41. 'core_profile_fields',
  42. 'article_comment',
  43. 'wxapp_versions',
  44. 'users_permission',
  45. 'uni_verifycode',
  46. ))) {
  47. return new $table_classname;
  48. }
  49. load()->classs('table');
  50. load()->table($name);
  51. $service = false;
  52. $class_name = "{$name}Table";
  53. if (class_exists($class_name)) {
  54. $service = new $class_name();
  55. }
  56. return $service;
  57. }
  58. class Loader {
  59. private $cache = array();
  60. private $singletonObject = array();
  61. private $libraryMap = array(
  62. 'agent' => 'agent/agent.class',
  63. 'captcha' => 'captcha/captcha.class',
  64. 'pdo' => 'pdo/PDO.class',
  65. 'qrcode' => 'qrcode/phpqrcode',
  66. 'ftp' => 'ftp/ftp',
  67. 'pinyin' => 'pinyin/pinyin',
  68. 'pkcs7' => 'pkcs7/pkcs7Encoder',
  69. 'json' => 'json/JSON',
  70. 'phpmailer' => 'phpmailer/PHPMailerAutoload',
  71. 'oss' => 'alioss/autoload',
  72. 'qiniu' => 'qiniu/autoload',
  73. 'cos' => 'cosv4.2/include',
  74. 'cosv3' => 'cos/include',
  75. 'sentry' => 'sentry/Raven/Autoloader',
  76. );
  77. private $loadTypeMap = array(
  78. 'func' => '/framework/function/%s.func.php',
  79. 'model' => '/framework/model/%s.mod.php',
  80. 'classs' => '/framework/class/%s.class.php',
  81. 'library' => '/framework/library/%s.php',
  82. 'table' => '/framework/table/%s.table.php',
  83. 'web' => '/web/common/%s.func.php',
  84. 'app' => '/app/common/%s.func.php',
  85. );
  86. private $accountMap = array(
  87. 'account' => 'account/account',
  88. 'weixin.account' => 'account/weixin.account',
  89. 'weixin.platform' => 'account/weixin.platform',
  90. 'aliapp.account' => 'account/aliapp.account',
  91. 'phoneapp.account' => 'account/phoneapp.account',
  92. 'webapp.account' => 'account/webapp.account',
  93. 'wxapp.account' => 'account/wxapp.account',
  94. 'wxapp.platform' => 'account/wxapp.platform',
  95. 'wxapp.work' => 'account/wxapp.work',
  96. 'xzapp.account' => 'account/xzapp.account',
  97. 'xzapp.platform' => 'account/xzapp.platform',
  98. );
  99. public function __construct() {
  100. $this->registerAutoload();
  101. }
  102. public function registerAutoload() {
  103. spl_autoload_register(array($this, 'autoload'));
  104. }
  105. public function autoload($class) {
  106. $section = array(
  107. 'Table' => '/framework/table/',
  108. );
  109. $classmap = array(
  110. 'We7Table' => 'table',
  111. );
  112. if (isset($classmap[$class])) {
  113. load()->classs($classmap[$class]);
  114. } elseif (preg_match('/^[0-9a-zA-Z\-\\\\_]+$/', $class)
  115. && (stripos($class, 'We7') === 0 || stripos($class, '\We7') === 0)
  116. && stripos($class, "\\") !== false) {
  117. $group = explode("\\", $class);
  118. $path = IA_ROOT . $section[$group[1]];
  119. unset($group[0]);
  120. unset($group[1]);
  121. $file_path = $path . implode('/', $group) . '.php';
  122. if(is_file($file_path)) {
  123. include $file_path;
  124. }
  125. $file_path = $path . 'Core/' . implode('', $group) . '.php';
  126. if(is_file($file_path)) {
  127. include $file_path;
  128. }
  129. }
  130. }
  131. public function __call($type, $params) {
  132. global $_W;
  133. $name = $cachekey = array_shift($params);
  134. if (!empty($this->cache[$type]) && isset($this->cache[$type][$cachekey])) {
  135. return true;
  136. }
  137. if (empty($this->loadTypeMap[$type])) {
  138. return true;
  139. }
  140. if ($type == 'library' && !empty($this->libraryMap[$name])) {
  141. $name = $this->libraryMap[$name];
  142. }
  143. if ($type == 'classs' && !empty($this->accountMap[$name])) {
  144. $filename = sprintf($this->loadTypeMap[$type], $this->accountMap[$name]);
  145. if (file_exists(IA_ROOT . $filename)) {
  146. $name = $this->accountMap[$name];
  147. }
  148. }
  149. $file = sprintf($this->loadTypeMap[$type], $name);
  150. if (file_exists(IA_ROOT . $file)) {
  151. include IA_ROOT . $file;
  152. $this->cache[$type][$cachekey] = true;
  153. return true;
  154. } else {
  155. trigger_error('Invalid ' . ucfirst($type) . $file, E_USER_WARNING);
  156. return false;
  157. }
  158. }
  159. function singleton($name) {
  160. if (isset($this->singletonObject[$name])) {
  161. return $this->singletonObject[$name];
  162. }
  163. $this->singletonObject[$name] = $this->object($name);
  164. return $this->singletonObject[$name];
  165. }
  166. function object($name) {
  167. $this->classs(strtolower($name));
  168. if (class_exists($name)) {
  169. return new $name();
  170. } else {
  171. return false;
  172. }
  173. }
  174. }