@Chiang
2020-03-28T12:32:27.000000Z
字数 3973
阅读 796
doitphp 2020-03
private static $_coreClassArray = array('Request' => 'core/Request.php','Response' => 'core/Response.php','Model' => 'core/Model.php','DbCommand' => 'core/DbCommand.php','DbPdo' => 'core/DbPdo.php','Log' => 'core/Log.php','DoitException' => 'core/DoitException.php','Widget' => 'core/Widget.php','View' => 'core/View.php','Template' => 'core/Template.php','WidgetTemplate' => 'core/WidgetTemplate.php','Extension' => 'core/Extension.php','Pagination' => 'library/Pagination.php','File' => 'library/File.php','Html' => 'library/Html.php','Cookie' => 'library/Cookie.php','Session' => 'library/Session.php','Image' => 'library/Image.php','Captcha' => 'library/Captcha.php','Curl' => 'library/Curl.php','Client' => 'library/Client.php','Validation' => 'library/Validation.php','FileDownload' => 'library/FileDownload.php','FileUpload' => 'library/FileUpload.php','Excel' => 'library/Excel.php','Csv' => 'library/Csv.php','Security' => 'library/Security.php','Text' => 'library/Text.php','Encrypt' => 'library/Encrypt.php','Tree' => 'library/Tree.php','MongoDb' => 'library/MongoDb.php','Language' => 'library/Language.php','Cache_Memcached' => 'library/cache/Memcached.php','Cache_Memcache' => 'library/cache/Memcache.php','Cache_Redis' => 'library/cache/Redis.php','Cache_File' => 'library/cache/File.php','Pinyin' => 'library/Pinyin.php','Calendar' => 'library/Calendar.php','Benchmark' => 'library/Benchmark.php','HttpResponse' => 'library/HttpResponse.php','Ftp' => 'library/Ftp.php',);
public static function loadClass($className) {//doitPHP核心类文件的加载分析if (isset(self::$_coreClassArray[$className])) {//当$className在核心类引导数组中存在时, 加载核心类文件Doit::loadFile(DOIT_ROOT . DS . self::$_coreClassArray[$className]);} elseif (substr($className, -10) == 'Controller') {//controller文件自动载分析self::_loadTagFile($className, 'Controller');} elseif (substr($className, -5) == 'Model') {//modlel文件自动加载分析self::_loadTagFile($className, 'Model');} elseif (substr($className, -6) == 'Widget') {//加载所要运行的widget文件self::_loadTagFile($className, 'Widget');} else {//加载所使用命名空间的类文件if (strpos($className, '\\') !== false) {$filePath = BASE_PATH . DS . str_replace('\\', DS, $className) . '.php';if (!is_file($filePath)) {//当使用命名空间的文件不存在时,提示错误信息Response::halt('The File: ' . $filePath .' is not found !');}Doit::loadFile($filePath);return true;}//分析加载扩展类文件目录(library)的文件if (!self::_loadTagFile($className, 'Library')) {//根据配置文件improt的引导设置,自动加载文件if (!self::_loadImportConfigFile($className)) {//最后,当运行上述自动加载规则,均没有加载所需要的文件时,提示错误信息Response::halt('The Class: ' . $className .' is not found !');}}}return true;}
- 加载核心文件
- 非核心文件,根据调用类后缀定义名称分类调用
- 根据命名空间调用
- 加载扩展类文件
- 根据配置文件improt的引导设置,自动加载文件
private static function _loadTagFile($className, $tagName) {//分析标签文件目录路径switch ($tagName) {case 'Controller':$dirName = 'controllers';//当controller文件存放于子目录时if (strpos($className, '_') !== false) {$childDirArray = explode('_', strtolower($className));$className = ucfirst(array_pop($childDirArray));$className = implode(DS, $childDirArray) . DS . $className;}break;case 'Model':$dirName = 'models';break;case 'Widget':$dirName = 'widgets';break;case 'Library':$dirName = 'library';break;default:$dirName = 'library';}//分析标签文件的实际路径$tagFilePath = BASE_PATH . DS . $dirName . DS . str_replace('_', DS, $className) . '.php';//当标签文件存在时if (!is_file($tagFilePath)) {if ($tagName == 'Library') {return false;}//当所要加载的标签文件不存在时,显示错误提示信息Response::halt('The File: ' . $tagFilePath . ' is not found!');}//加载标签文件Doit::loadFile($tagFilePath);return true;}
private static function _loadImportConfigFile($className) {//定义自动加载状态。(true:已加载/false:未加载)$atuoLoadStatus = false;//分析配置文件import引导信息$importRules = Configure::get('import');//当配置文件引导信息合法时if ($importRules && is_array($importRules)) {foreach ($importRules as $rules) {if (!$rules) {continue;}//当配置文件引导信息中含有*'时,将设置的规则中的*替换为所要加载的文件类名if (strpos($rules, '*') !== false) {$filePath = str_replace('*', $className, $rules);} else {$filePath = $rules . DS . str_replace('_', DS, $className) . '.php';}//当自定义自动加载的文件存在时if (is_file($filePath)) {//加载文件Doit::loadFile($filePath);$atuoLoadStatus = true;break;}}}return $atuoLoadStatus;}
根据全局配置文件中
import定义规则加载文件
