<?php !defined('IN_WEB') AND exit('Access Denied!');

/**
 * 实现类的按需加载
 *
 * @package Lib
 * @author  dulu
 * 
 * @version 1.0.1
 */
final class Boyaa {
    //定义使用自动加载目录文件,之后需要加目录遍历直接加数组元素
    protected static $_paths = array( PATH_ACT );
                                      
    protected static $_files    = array();
    public    static $ext       = '.php';
    protected static $_init     = FALSE;
    
    /**
     * 环境变量初始化
     *
     * @author dulu
     *
     * @return void
     */
    public static function init() {
        if(self::$_init)
        {
            return;
        }
        self::$_init = TRUE;
        spl_autoload_register(array('Boyaa', 'load'));
    }
    
    /**
     * 类自动加载器
     *
     * @author dulu
     *
     * @param {string} $class 类名
     *
     * @return boolean
     */
    public static function load($class)
    {  
         /**
         * You only need this file if you are not using composer.
         * Why are you not using composer?
         * https://getcomposer.org/
         */
        if (version_compare(PHP_VERSION, '5.4.0', '<')) {
            throw new Exception('The Facebook SDK v4 requires PHP version 5.4 or higher.');
        }
        
        //判断加载FB模块
        $prefix = 'Facebook\\';
        $len = strlen($prefix);
        if (strncmp($prefix, $class, $len) === 0) {
            $base_dir = defined('FACEBOOK_SDK_V4_SRC_DIR') ? FACEBOOK_SDK_V4_SRC_DIR : PATH_LIB.'Facebook/';  //初始化文件路径
            $relative_class = substr($class, $len);
            $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';  //拼接文件路径
            if (file_exists($file)) { //文件存在就加载文件
                require($file);
                return true;
            }
        } else {
            $pos  = strrpos($class, '_');
            $class = strtolower($class);
            $dir = "";
            if($pos > 0) {
                $classData = explode("_", $class);
                $dir = $classData[0] . "/" . $classData[1] . "/";
            } else {
                $dir = "{$class}/"; 
            }

            $file = str_replace('_', '.', $class);
            if($file = Boyaa::find_file($dir, $file)) {
                require($file);
                return true;
            }
        }
        return false;
    }

    /**
     * 查找文件
     *
     * @author dulu
     *
     * @param mix    $dir   目录名
     * @param string $file  文件名
     * @param string $ext   扩展名
     * @param bool   $array 是否返回匹配的文件列表
     *
     * @return string|false 成功返回文件路径，失败返回FALSE
     */
    public static function find_file($dir, $file, $ext=NULL, $array=FALSE) {
        $ext = $ext ? ".{$ext}" : Boyaa::$ext;
        $found = FALSE;
        foreach(Boyaa::$_paths as $path) {
            $path = $path . $dir;
            $filePath = $path . $file . $ext;
            $key = md5($filePath);
            if(isset(Boyaa::$_files[$key])) {
                $found = Boyaa::$_files[$key];
                break;
            }
            if(is_file($filePath)) {                
                Boyaa::$_files[$key] = $filePath;
            } else {
                continue;
            }
            
            if(isset(Boyaa::$_files[$key]) && $array==FALSE) {
                $found = Boyaa::$_files[$key];
                break;
            } else {
                $found[] = $filePath;
            }
        }
        return $found;        
    }
}