From 8d79e07dc5aa4391d5da16f93bd97ba648b12179 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sun, 28 Feb 2016 12:28:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0loader=E7=B1=BB=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Loader.php | 11 ++-- .../library/think/loader/test/Hello.php | 7 +++ tests/thinkphp/library/think/loaderTest.php | 63 +++++++++++++++++++ 3 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 tests/thinkphp/library/think/loader/test/Hello.php create mode 100644 tests/thinkphp/library/think/loaderTest.php diff --git a/library/think/Loader.php b/library/think/Loader.php index dd2c126b..3baf8868 100644 --- a/library/think/Loader.php +++ b/library/think/Loader.php @@ -34,6 +34,8 @@ class Loader // 记录加载信息 APP_DEBUG && self::$load[] = self::$map[$class]; include self::$map[$class]; + } else { + return false; } } elseif ($file = self::findFileInComposer($class)) { // Composer自动加载 @@ -43,7 +45,7 @@ class Loader } else { // 命名空间自动加载 if (!strpos($class, '\\')) { - return; + return false; } list($name, $class) = explode('\\', $class, 2); if (isset(self::$namespace[$name])) { @@ -53,7 +55,7 @@ class Loader // 扩展类库命名空间 $path = EXTEND_PATH . $name . DS; } else { - return; + return false; } $filename = $path . str_replace('\\', DS, $class) . EXT; if (is_file($filename)) { @@ -66,8 +68,10 @@ class Loader include $filename; } else { Log::record('autoloader error : ' . $filename, 'notice'); + return false; } } + return true; } // 注册classmap @@ -199,8 +203,6 @@ class Loader $class = str_replace(['.', '#'], [DS, '.'], $class); if (isset($_file[$class . $baseUrl])) { return true; - } else { - $_file[$class . $baseUrl] = true; } if (empty($baseUrl)) { @@ -228,6 +230,7 @@ class Loader return false; } include $filename; + $_file[$class . $baseUrl] = true; return true; } return false; diff --git a/tests/thinkphp/library/think/loader/test/Hello.php b/tests/thinkphp/library/think/loader/test/Hello.php new file mode 100644 index 00000000..c1414561 --- /dev/null +++ b/tests/thinkphp/library/think/loader/test/Hello.php @@ -0,0 +1,7 @@ + +// +---------------------------------------------------------------------- + +/** + * Loader测试 + * @author liu21st + */ + +namespace tests\thinkphp\library\think; + +use think\Loader; + +class loaderTest extends \PHPUnit_Framework_TestCase +{ + + public function testAutoload() + { + $this->assertEquals(true, Loader::autoload('think\Session')); + $this->assertEquals(false, Loader::autoload('think\COOKIE')); + $this->assertEquals(false, Loader::autoload('\think\Url')); + $this->assertEquals(false, Loader::autoload('think\Test')); + $this->assertEquals(false, Loader::autoload('my\HelloTest')); + } + + public function testAddMap() + { + Loader::addMap('my\hello\Test', 'Test.php'); + $this->assertEquals(false, Loader::autoload('my\hello\Test')); + } + + public function testAddNamespace() + { + Loader::addNamespace('top', __DIR__ . DS . 'loader' . DS); + $this->assertEquals(true, Loader::autoload('top\test\Hello')); + } + + public function testImport() + { + $this->assertEquals(true, Loader::import('think.log.driver.Sae')); + $this->assertEquals(false, Loader::import('think.log.driver.MyTest')); + } + + public function testParseName() + { + $this->assertEquals('HelloTest', Loader::parseName('hello_test', 1)); + $this->assertEquals('hello_test', Loader::parseName('HelloTest', 0)); + } + + public function testParseClass() + { + $this->assertEquals('app\index\controller\User', Loader::parseClass('index', 'controller', 'user')); + $this->assertEquals('app\index\controller\user\Type', Loader::parseClass('index', 'controller', 'user.type')); + $this->assertEquals('app\admin\model\UserType', Loader::parseClass('admin', 'model', 'user_type')); + } +}