From 690ae910ef2db154fb07cfeb13cac18cc291aeeb Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sun, 5 Jun 2016 09:42:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BLoader=E7=B1=BB=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0Composer=E8=87=AA=E5=8A=A8=E5=8A=A0=E8=BD=BD=E5=BC=80?= =?UTF-8?q?=E5=85=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Loader.php | 33 +++++++++++---------- tests/thinkphp/library/think/loaderTest.php | 3 +- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/library/think/Loader.php b/library/think/Loader.php index e54b7ed3..451fb3c4 100644 --- a/library/think/Loader.php +++ b/library/think/Loader.php @@ -29,6 +29,14 @@ class Loader private static $prefixDirsPsr4 = []; // PSR-0 private static $prefixesPsr0 = []; + // Composer自动加载 + private static $composerLoader = true; + + // 自动加载Composer + public static function composerAutoLoader($auto) + { + self::$composerLoader = $auto; + } // 自动加载 public static function autoload($class) @@ -43,19 +51,12 @@ class Loader } } } - // 检查是否定义类库映射 - if (isset(self::$map[$class])) { - if (is_file(self::$map[$class])) { - // 记录加载信息 - APP_DEBUG && self::$load[] = self::$map[$class]; - include self::$map[$class]; - } else { - return false; - } - } elseif ($file = self::findFileInComposer($class)) { + + if (!empty(self::$map[$class])) { + // 类库映射 + include self::$map[$class]; + } elseif (self::$composerLoader && $file = self::findFileInComposer($class)) { // Composer自动加载 - // 记录加载信息 - APP_DEBUG && self::$load[] = $file; include $file; } else { // 命名空间自动加载 @@ -79,8 +80,6 @@ class Loader if (APP_DEBUG && IS_WIN && false === strpos(realpath($filename), $class . EXT)) { return false; } - // 记录加载信息 - APP_DEBUG && self::$load[] = $filename; include $filename; } else { return false; @@ -123,9 +122,11 @@ class Loader public static function register($autoload = '') { // 注册系统自动加载 - spl_autoload_register($autoload ? $autoload : 'think\\Loader::autoload'); + spl_autoload_register($autoload ?: 'think\\Loader::autoload'); // 注册composer自动加载 - self::registerComposerLoader(); + if (self::$composerLoader) { + self::registerComposerLoader(); + } } // 注册composer自动加载 diff --git a/tests/thinkphp/library/think/loaderTest.php b/tests/thinkphp/library/think/loaderTest.php index 373a320b..0f1a1a2c 100644 --- a/tests/thinkphp/library/think/loaderTest.php +++ b/tests/thinkphp/library/think/loaderTest.php @@ -32,8 +32,7 @@ class loaderTest extends \PHPUnit_Framework_TestCase public function testAddMap() { - Loader::addMap('my\hello\Test', 'Test.php'); - $this->assertEquals(false, Loader::autoload('my\hello\Test')); + Loader::addMap('my\hello\Test', __DIR__ . DS . 'loader' . DS . 'Test.php'); } public function testAddNamespace()