改进扩展类库的自动加载

This commit is contained in:
thinkphp
2015-12-25 09:36:42 +08:00
parent 7ea4250331
commit cdd0dc6048
2 changed files with 7 additions and 10 deletions

View File

@@ -18,6 +18,7 @@ define('THINK_VERSION', '5.0.0beta');
defined('DS') or define('DS', DIRECTORY_SEPARATOR); defined('DS') or define('DS', DIRECTORY_SEPARATOR);
defined('THINK_PATH') or define('THINK_PATH', dirname(__FILE__) . DS); defined('THINK_PATH') or define('THINK_PATH', dirname(__FILE__) . DS);
defined('LIB_PATH') or define('LIB_PATH', THINK_PATH . 'library' . DS); defined('LIB_PATH') or define('LIB_PATH', THINK_PATH . 'library' . DS);
defined('EXTEND_PATH') or define('EXTEND_PATH', THINK_PATH . 'extend' . DS);
defined('MODE_PATH') or define('MODE_PATH', THINK_PATH . 'mode' . DS); // 系统应用模式目录 defined('MODE_PATH') or define('MODE_PATH', THINK_PATH . 'mode' . DS); // 系统应用模式目录
defined('CORE_PATH') or define('CORE_PATH', LIB_PATH . 'think' . DS); defined('CORE_PATH') or define('CORE_PATH', LIB_PATH . 'think' . DS);
defined('ORG_PATH') or define('ORG_PATH', LIB_PATH . 'org' . DS); defined('ORG_PATH') or define('ORG_PATH', LIB_PATH . 'org' . DS);

View File

@@ -34,24 +34,20 @@ class Loader
} elseif ($file = self::findFileInComposer($class)) { } elseif ($file = self::findFileInComposer($class)) {
include $file; include $file;
} else { } else {
// 项目命名空间
$path = APP_PATH;
// 命名空间自动加载 // 命名空间自动加载
$name = strtolower(strstr($class, '\\', true)); $name = strtolower(strstr($class, '\\', true));
if (isset(self::$namespace[$name])) { if (isset(self::$namespace[$name])) {
// 注册的命名空间 // 注册的命名空间
$path = dirname(self::$namespace[$name]) . DS; $path = dirname(self::$namespace[$name]) . DS;
} elseif (in_array($name, ['think', 'org', 'behavior', 'com', 'traits']) || is_dir(LIB_PATH . $name)) { } elseif (in_array($name, ['think', 'behavior', 'traits']) || is_dir(LIB_PATH . $name)) {
// Library目录下面的命名空间自动定位 // Library目录下面的命名空间自动定位
$path = LIB_PATH; $path = LIB_PATH;
} elseif (is_dir(EXTEND_PATH . $name)) {
// 扩展类库命名空间
$path = EXTEND_PATH;
} else { } else {
$extend = Config::get('extend_library') ?: []; // 项目命名空间
foreach ($extend as $_extend) { $path = APP_PATH;
if (is_dir($_extend . $name)) {
$path = $_extend;
break;
}
}
} }
$filename = $path . str_replace('\\', DS, str_replace('\\_', '\\', strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $class), "_")))) . EXT; $filename = $path . str_replace('\\', DS, str_replace('\\_', '\\', strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $class), "_")))) . EXT;
if (is_file($filename)) { if (is_file($filename)) {