composer 类加载器,对 <PSR-4的风格>、<PSR-0的风格>、<PEAR的风格> 风格的类的加载

 

class ClassLoader
{
    // ... 
    
    /**
     * composer 类加载器,对 <PSR-4的风格>、<PSR-0的风格>、<PEAR的风格> 风格的类的加载
     */
    private function findFileWithExtension($class, $ext)
    {
        // PSR-4 lookup  <PSR-4的风格>: Dir1\Dir2\ClassName1  ---> Dir1/Dir2/ClassName1
        $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext;
    
        $first = $class[0];
        if (isset($this->prefixLengthsPsr4[$first])) {
            foreach ($this->prefixLengthsPsr4[$first] as $prefix => $length) {
                if (0 === strpos($class, $prefix)) {
                    foreach ($this->prefixDirsPsr4[$prefix] as $dir) {
                        if (file_exists($file = $dir . DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $length))) {
                            return $file;
                        }
                    }
                }
            }
        }
    
        // PSR-4 fallback dirs 匹配失败
        foreach ($this->fallbackDirsPsr4 as $dir) {
            if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) {
                return $file;
            }
        }
    
        // PSR-0 lookup  <PSR-0的风格>: Dir1\Dir2\Dir3_ClassName1  ---> Dir1\Dir2/ClassName1
        if (false !== $pos = strrpos($class, '\\')) {
            // namespaced class name
            $logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1)
            . strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR);
        } else {
            // PEAR-like class name <PEAR的风格>: Dir1_Dir2_ClassName1 --->Dir1/Dir2/ClassName1
            $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext;
        }
    
        if (isset($this->prefixesPsr0[$first])) {
            foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) {
                if (0 === strpos($class, $prefix)) {
                    foreach ($dirs as $dir) {
                        if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
                            return $file;
                        }
                    }
                }
            }
        }
    
        // PSR-0 fallback dirs
        foreach ($this->fallbackDirsPsr0 as $dir) {
            if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
                return $file;
            }
        }
    
        // PSR-0 include paths.
        if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) {
            return $file;
        }
    }
    
    // ...
}

 

posted on 2017-01-09 17:20  周~~  阅读(525)  评论(0编辑  收藏  举报

导航