loader.class.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. load()->classs('table');
  16. load()->table($name);
  17. $service = false;
  18. $class_name = "{$name}Table";
  19. if (class_exists($class_name)) {
  20. $service = new $class_name();
  21. }
  22. return $service;
  23. }
  24. class Loader {
  25. private $cache = array();
  26. private $singletonObject = array();
  27. private $libraryMap = array(
  28. 'agent' => 'agent/agent.class',
  29. 'captcha' => 'captcha/captcha.class',
  30. 'pdo' => 'pdo/PDO.class',
  31. 'qrcode' => 'qrcode/phpqrcode',
  32. 'ftp' => 'ftp/ftp',
  33. 'pinyin' => 'pinyin/pinyin',
  34. 'pkcs7' => 'pkcs7/pkcs7Encoder',
  35. 'json' => 'json/JSON',
  36. 'phpmailer' => 'phpmailer/PHPMailerAutoload',
  37. 'oss' => 'alioss/autoload',
  38. 'qiniu' => 'qiniu/autoload',
  39. 'cos' => 'cosv4.2/include',
  40. 'cosv3' => 'cos/include',
  41. );
  42. private $loadTypeMap = array(
  43. 'func' => '/framework/function/%s.func.php',
  44. 'model' => '/framework/model/%s.mod.php',
  45. 'classs' => '/framework/class/%s.class.php',
  46. 'library' => '/framework/library/%s.php',
  47. 'table' => '/framework/table/%s.table.php',
  48. 'web' => '/web/common/%s.func.php',
  49. 'app' => '/app/common/%s.func.php',
  50. );
  51. public function __call($type, $params) {
  52. global $_W;
  53. $name = $cachekey = array_shift($params);
  54. if (!empty($this->cache[$type]) && isset($this->cache[$type][$cachekey])) {
  55. return true;
  56. }
  57. if (empty($this->loadTypeMap[$type])) {
  58. return true;
  59. }
  60. if ($type == 'library' && !empty($this->libraryMap[$name])) {
  61. $name = $this->libraryMap[$name];
  62. }
  63. $file = sprintf($this->loadTypeMap[$type], $name);
  64. if (file_exists(IA_ROOT . $file)) {
  65. include IA_ROOT . $file;
  66. $this->cache[$type][$cachekey] = true;
  67. return true;
  68. } else {
  69. trigger_error('Invalid ' . ucfirst($type) . $file, E_USER_WARNING);
  70. return false;
  71. }
  72. }
  73. function singleton($name) {
  74. if (isset($this->singletonObject[$name])) {
  75. return $this->singletonObject[$name];
  76. }
  77. $this->singletonObject[$name] = $this->object($name);
  78. return $this->singletonObject[$name];
  79. }
  80. function object($name) {
  81. $this->classs(strtolower($name));
  82. if (class_exists($name)) {
  83. return new $name();
  84. } else {
  85. return false;
  86. }
  87. }
  88. }