改进Loader自动加载 支持一个命名空间对应多个路径 兼容composer

This commit is contained in:
thinkphp
2016-06-22 14:54:28 +08:00
parent f14ef530e8
commit b454adeef5

View File

@@ -76,8 +76,17 @@ class Loader
} else { } else {
return false; return false;
} }
$filename = $path . str_replace('\\', DS, $class) . EXT; // 定位文件
$match = false;
foreach ((array) $path as $p) {
$filename = $p . str_replace('\\', DS, $class) . EXT;
if (is_file($filename)) { if (is_file($filename)) {
$match = true;
break;
}
}
if ($match) {
// 开启调试模式Win环境严格区分大小写 // 开启调试模式Win环境严格区分大小写
if (IS_WIN && false === strpos(realpath($filename), $class . EXT)) { if (IS_WIN && false === strpos(realpath($filename), $class . EXT)) {
return false; return false;
@@ -87,7 +96,6 @@ class Loader
return false; return false;
} }
} }
return true;
} }
// 注册classmap // 注册classmap
@@ -143,12 +151,13 @@ class Loader
// 读取Composer自动加载文件 // 读取Composer自动加载文件
$autoload = include VENDOR_PATH . 'think_autoload.php'; $autoload = include VENDOR_PATH . 'think_autoload.php';
if (is_array($autoload)) { if (is_array($autoload)) {
self::addClassMap($autoload); // 命名空间和类库映射注册
self::addNamespace($autoload['namespace']);
self::addClassMap($autoload['classmap']);
// 载入composer包的文件列表
foreach ($autoload['files'] as $file) {
include $file;
} }
} elseif (is_file(RUNTIME_PATH . 'autoload_composer.php')) {
$autoload = include RUNTIME_PATH . 'autoload_composer.php';
if (is_array($autoload)) {
self::addNamespace($autoload);
} }
} elseif (AUTO_SCAN_PACKAGE && is_dir(VENDOR_PATH)) { } elseif (AUTO_SCAN_PACKAGE && is_dir(VENDOR_PATH)) {
self::scanComposerPackage(VENDOR_PATH); self::scanComposerPackage(VENDOR_PATH);