一、在base.php中 \core\Loader::register();
载入自动加载
/**
* @param null $autoload
* 自动加载
*/
public static function register($autoload = null) {
spl_autoload_register($autoload ?: '\core\\Loader::autoload', true, true);
self::addNamespace([
'core' => ROOT_PATH . 'core' . DS,
]);
}
二、核心目录Loader的autoload方法
public static function autoload($class) {
if (!empty(self::$namespaceAlias)) {
$namespace = dirname($class);
if (isset(self::$namespaceAlias[$namespace])) {
$original = self::$namespaceAlias[$namespace] . '\\' . basename($class);
if (class_exists($original)) {
return class_alias($original, $class, false);
}
}
}
if ($file = self::findFile($class)) {
if (!IS_WIN || pathinfo($file, PATHINFO_FILENAME) == pathinfo(realpath($file), PATHINFO_FILENAME)) {
__include_file($file);
return true;
}
}
return false;
}
三、查找类文件是否存在
private static function findFile($class) {
if (!empty(self::$classMap[$class])) {
return self::$classMap[$class];
}
$logicalPathPsr4 = strtr($class, '\\', DS) . EXT;
$first = $class[0];
if (isset(self::$prefixLengthsPsr4[$first])) {
foreach (self::$prefixLengthsPsr4[$first] as $prefix => $length) {
if (0 === strpos($class, $prefix)) {
foreach (self::$prefixDirsPsr4[$prefix] as $dir) {
if (is_file($file = $dir . DS . substr($logicalPathPsr4, $length))) {
return $file;
}
}
}
}
}
foreach (self::$fallbackDirsPsr4 as $dir) {
if (is_file($file = $dir . DS . $logicalPathPsr4)) {
return $file;
}
}
if (false !== $pos = strrpos($class, '\\')) {
$logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1)
. strtr(substr($logicalPathPsr4, $pos + 1), '_', DS);
} else {
$logicalPathPsr0 = strtr($class, '_', DS) . EXT;
}
if (isset(self::$prefixesPsr0[$first])) {
foreach (self::$prefixesPsr0[$first] as $prefix => $dirs) {
if (0 === strpos($class, $prefix)) {
foreach ($dirs as $dir) {
if (is_file($file = $dir . DS . $logicalPathPsr0)) {
return $file;
}
}
}
}
}
foreach (self::$fallbackDirsPsr0 as $dir) {
if (is_file($file = $dir . DS . $logicalPathPsr0)) {
return $file;
}
}
return self::$classMap[$class] = false;
}
四、引入类文件
/**
* 引入文件
* @param string $file 文件路径
* @return mixed
*/
function __include_file($file) {
return include $file;
}
五、加载核心目录命名空间【core】
self::addNamespace([ 'core' => ROOT_PATH . 'core' . DS,]);
public static function addNamespace($namespace, $path = '') {
if (is_array($namespace)) {
foreach ($namespace as $prefix => $paths) {
self::addPsr4($prefix . '\\', rtrim($paths, DS), true);
}
} else {
self::addPsr4($namespace . '\\', rtrim($path, DS), true);
}
}
添加 PSR-4 空间
private static function addPsr4($prefix, $paths, $prepend = false) {
if (!$prefix) {
self::$fallbackDirsPsr4 = $prepend ?
array_merge((array)$paths, self::$fallbackDirsPsr4) :
array_merge(self::$fallbackDirsPsr4, (array)$paths);
} elseif (!isset(self::$prefixDirsPsr4[$prefix])) {
$length = strlen($prefix);
if ('\\' !== $prefix[$length - 1]) {
throw new \InvalidArgumentException(
"A non-empty PSR-4 prefix must end with a namespace separator."
);
}
self::$prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
self::$prefixDirsPsr4[$prefix] = (array)$paths;
} else {
self::$prefixDirsPsr4[$prefix] = $prepend ?
array_merge((array)$paths, self::$prefixDirsPsr4[$prefix]) :
array_merge(self::$prefixDirsPsr4[$prefix], (array)$paths);
}
}