class.boyaa.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php !defined('IN_WEB') AND exit('Access Denied!');
  2. /**
  3. * 实现类的按需加载
  4. *
  5. * @package Lib
  6. * @author dulu
  7. *
  8. * @version 1.0.1
  9. */
  10. final class Boyaa {
  11. //定义使用自动加载目录文件,之后需要加目录遍历直接加数组元素
  12. protected static $_paths = array( PATH_ACT );
  13. protected static $_files = array();
  14. public static $ext = '.php';
  15. protected static $_init = FALSE;
  16. /**
  17. * 环境变量初始化
  18. *
  19. * @author dulu
  20. *
  21. * @return void
  22. */
  23. public static function init() {
  24. if(self::$_init)
  25. {
  26. return;
  27. }
  28. self::$_init = TRUE;
  29. spl_autoload_register(array('Boyaa', 'load'));
  30. }
  31. /**
  32. * 类自动加载器
  33. *
  34. * @author dulu
  35. *
  36. * @param {string} $class 类名
  37. *
  38. * @return boolean
  39. */
  40. public static function load($class)
  41. {
  42. /**
  43. * You only need this file if you are not using composer.
  44. * Why are you not using composer?
  45. * https://getcomposer.org/
  46. */
  47. if (version_compare(PHP_VERSION, '5.4.0', '<')) {
  48. throw new Exception('The Facebook SDK v4 requires PHP version 5.4 or higher.');
  49. }
  50. //判断加载FB模块
  51. $prefix = 'Facebook\\';
  52. $len = strlen($prefix);
  53. if (strncmp($prefix, $class, $len) === 0) {
  54. $base_dir = defined('FACEBOOK_SDK_V4_SRC_DIR') ? FACEBOOK_SDK_V4_SRC_DIR : PATH_LIB.'Facebook/'; //初始化文件路径
  55. $relative_class = substr($class, $len);
  56. $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php'; //拼接文件路径
  57. if (file_exists($file)) { //文件存在就加载文件
  58. require($file);
  59. return true;
  60. }
  61. } else {
  62. $pos = strrpos($class, '_');
  63. $class = strtolower($class);
  64. $dir = "";
  65. if($pos > 0) {
  66. $classData = explode("_", $class);
  67. $dir = $classData[0] . "/" . $classData[1] . "/";
  68. } else {
  69. $dir = "{$class}/";
  70. }
  71. $file = str_replace('_', '.', $class);
  72. if($file = Boyaa::find_file($dir, $file)) {
  73. require($file);
  74. return true;
  75. }
  76. }
  77. return false;
  78. }
  79. /**
  80. * 查找文件
  81. *
  82. * @author dulu
  83. *
  84. * @param mix $dir 目录名
  85. * @param string $file 文件名
  86. * @param string $ext 扩展名
  87. * @param bool $array 是否返回匹配的文件列表
  88. *
  89. * @return string|false 成功返回文件路径,失败返回FALSE
  90. */
  91. public static function find_file($dir, $file, $ext=NULL, $array=FALSE) {
  92. $ext = $ext ? ".{$ext}" : Boyaa::$ext;
  93. $found = FALSE;
  94. foreach(Boyaa::$_paths as $path) {
  95. $path = $path . $dir;
  96. $filePath = $path . $file . $ext;
  97. $key = md5($filePath);
  98. if(isset(Boyaa::$_files[$key])) {
  99. $found = Boyaa::$_files[$key];
  100. break;
  101. }
  102. if(is_file($filePath)) {
  103. Boyaa::$_files[$key] = $filePath;
  104. } else {
  105. continue;
  106. }
  107. if(isset(Boyaa::$_files[$key]) && $array==FALSE) {
  108. $found = Boyaa::$_files[$key];
  109. break;
  110. } else {
  111. $found[] = $filePath;
  112. }
  113. }
  114. return $found;
  115. }
  116. }