改进Loader类 增加Composer自动加载开关

This commit is contained in:
thinkphp
2016-06-05 09:42:27 +08:00
parent 850aa53374
commit 690ae910ef
2 changed files with 18 additions and 18 deletions

View File

@@ -29,6 +29,14 @@ class Loader
private static $prefixDirsPsr4 = []; private static $prefixDirsPsr4 = [];
// PSR-0 // PSR-0
private static $prefixesPsr0 = []; private static $prefixesPsr0 = [];
// Composer自动加载
private static $composerLoader = true;
// 自动加载Composer
public static function composerAutoLoader($auto)
{
self::$composerLoader = $auto;
}
// 自动加载 // 自动加载
public static function autoload($class) public static function autoload($class)
@@ -43,19 +51,12 @@ class Loader
} }
} }
} }
// 检查是否定义类库映射
if (isset(self::$map[$class])) { if (!empty(self::$map[$class])) {
if (is_file(self::$map[$class])) { // 类库映射
// 记录加载信息 include self::$map[$class];
APP_DEBUG && self::$load[] = self::$map[$class]; } elseif (self::$composerLoader && $file = self::findFileInComposer($class)) {
include self::$map[$class];
} else {
return false;
}
} elseif ($file = self::findFileInComposer($class)) {
// Composer自动加载 // Composer自动加载
// 记录加载信息
APP_DEBUG && self::$load[] = $file;
include $file; include $file;
} else { } else {
// 命名空间自动加载 // 命名空间自动加载
@@ -79,8 +80,6 @@ class Loader
if (APP_DEBUG && IS_WIN && false === strpos(realpath($filename), $class . EXT)) { if (APP_DEBUG && IS_WIN && false === strpos(realpath($filename), $class . EXT)) {
return false; return false;
} }
// 记录加载信息
APP_DEBUG && self::$load[] = $filename;
include $filename; include $filename;
} else { } else {
return false; return false;
@@ -123,9 +122,11 @@ class Loader
public static function register($autoload = '') public static function register($autoload = '')
{ {
// 注册系统自动加载 // 注册系统自动加载
spl_autoload_register($autoload ? $autoload : 'think\\Loader::autoload'); spl_autoload_register($autoload ?: 'think\\Loader::autoload');
// 注册composer自动加载 // 注册composer自动加载
self::registerComposerLoader(); if (self::$composerLoader) {
self::registerComposerLoader();
}
} }
// 注册composer自动加载 // 注册composer自动加载

View File

@@ -32,8 +32,7 @@ class loaderTest extends \PHPUnit_Framework_TestCase
public function testAddMap() public function testAddMap()
{ {
Loader::addMap('my\hello\Test', 'Test.php'); Loader::addMap('my\hello\Test', __DIR__ . DS . 'loader' . DS . 'Test.php');
$this->assertEquals(false, Loader::autoload('my\hello\Test'));
} }
public function testAddNamespace() public function testAddNamespace()