我独自走进 Laravel5.5 的❤(二)
上一节我分析了 laravel 的 index 表面,这次我将继续深入,autoload_real.php 文件中引入的 loadClassLoader.php 是实现 autoload 的关键,这节我也会记录我对 loadClassLoader.php 的理解。
1.
// PSR-4
private $prefixLengthsPsr4 = array();
private $prefixDirsPsr4 = array();
private $fallbackDirsPsr4 = array();
// PSR-0
private $prefixesPsr0 = array();
private $fallbackDirsPsr0 = array();
private $useIncludePath = false;
private $classMap = array();
private $classMapAuthoritative = false;
private $missingClasses = array();
private $apcuPrefix;
解读:初始化 psr0、psr4,以及类 map 数组
2.
//获取psr0规范前缀
public function getPrefixes()
{
if (!empty($this->prefixesPsr0)) {
return call_user_func_array('array_merge', $this->prefixesPsr0);
}
return array();
}
//获取psr4规范前缀
public function getPrefixesPsr4()
{
return $this->prefixDirsPsr4;
}
//获取psr0规范路径
public function getFallbackDirs()
{
return $this->fallbackDirsPsr0;
}
//获取psr4规范路径
public function getFallbackDirsPsr4()
{
return $this->fallbackDirsPsr4;
}
//获取类map
public function getClassMap()
{
return $this->classMap;
}
解读:获取函数的定义
3.
public function addClassMap(array $classMap)
{
if ($this->classMap) {
//如果该类map存在,则合并,新类map覆盖旧的
$this->classMap = array_merge($this->classMap, $classMap);
} else {
//如果不存在该map,则加入
$this->classMap = $classMap;
}
}
解读:类 map 的自加载添加
4.
public function add($prefix, $paths, $prepend = false)
{
if (!$prefix) {
if ($prepend) {
//如果$prefix为空且$prepend为true添加psr0规范文件回调路径,旧的覆盖新的
$this->fallbackDirsPsr0 = array_merge(
(array) $paths,
$this->fallbackDirsPsr0
);
} else {
//如果$prefix为空且$prepend为false天剑psr0规范文件回调路径,新的覆盖旧的
$this->fallbackDirsPsr0 = array_merge(
$this->fallbackDirsPsr0,
(array) $paths
);
}
return;
}
$first = $prefix[0];
if (!isset($this->prefixesPsr0[$first][$prefix])) {
//设置psr0规范文件前缀
$this->prefixesPsr0[$first][$prefix] = (array) $paths;
return;
}
if ($prepend) {
//如果$prepend为true,旧的psr0规范文件前缀覆盖新的
$this->prefixesPsr0[$first][$prefix] = array_merge(
(array) $paths,
$this->prefixesPsr0[$first][$prefix]
);
} else {
//如果$prepend为false,新的psr0规范文件前缀覆盖新的
$this->prefixesPsr0[$first][$prefix] = array_merge(
$this->prefixesPsr0[$first][$prefix],
(array) $paths
);
}
}
解读:psr0 规范文件的自加载添加
5.
public function addPsr4($prefix, $paths, $prepend = false)
{
if (!$prefix) {
// Register directories for the root namespace.
//为根命名空间注册路径
if ($prepend) {
//如果$prefix为空且$prepend为true添加psr4规范文件回调路径,$this->fallbackDirsPsr4覆盖$paths
$this->fallbackDirsPsr4 = array_merge(
(array) $paths,
$this->fallbackDirsPsr4
);
} else {
//如果$prefix为空且$prepend为true添加psr4规范文件回调路径,新的覆盖新的
$this->fallbackDirsPsr4 = array_merge(
$this->fallbackDirsPsr4,
(array) $paths
);
}
} elseif (!isset($this->prefixDirsPsr4[$prefix])) {
//如果尚未设置该psr4规范
// Register directories for a new namespace.
//为新命名空间注册路径
//获取前缀长度
$length = strlen($prefix);
if ('\\' !== $prefix[$length - 1]) {
//如果前缀不是以'\\'结束,抛出错误
throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
}
//获取psr4规范前缀长度
$this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
//获取psr4规范前缀路径
$this->prefixDirsPsr4[$prefix] = (array) $paths;
} elseif ($prepend) {
//如果已经设置该psr4规范
// Prepend directories for an already registered namespace.
//为已经注册的命名空间预加载路径prefixDirsPsr4[$prefix]覆盖$paths
$this->prefixDirsPsr4[$prefix] = array_merge(
(array) $paths,
$this->prefixDirsPsr4[$prefix]
);
} else {
// Append directories for an already registered namespace.
//为已经注册的命名空间追加一个路径
$this->prefixDirsPsr4[$prefix] = array_merge(
$this->prefixDirsPsr4[$prefix],
(array) $paths
);
}
}
解读:psr4 规范文件的自加载添加
6.
public function setUseIncludePath($useIncludePath)
{
$this->useIncludePath = $useIncludePath;
}
解读:打开类文件包含路径的搜索
7.
public function getUseIncludePath()
{
return $this->useIncludePath;
}
解读:可以用来检查包含路径是否已经被自动加载器采用
8.
public function setClassMapAuthoritative($classMapAuthoritative)
{
$this->classMapAuthoritative = $classMapAuthoritative;
}
解读:关掉还没有注册 class map 的类前缀和回调路径的搜索
9.
public function isClassMapAuthoritative()
{
return $this->classMapAuthoritative;
}
解读:判断是否报搜索失败如果没有在 class map 找到当前类
10.
public function setApcuPrefix($apcuPrefix)
{
$this->apcuPrefix = function_exists('apcu_fetch') && ini_get('apc.enabled') ? $apcuPrefix : null;
}
解读:如果扩展是合法的,APCu 前缀用来缓存 found/not-found 类
11.
public function getApcuPrefix()
{
return $this->apcuPrefix;
}
解读:如果 APCu 缓存不合法,获取正在使用的,或空的 APCu 前缀
12.
public function register($prepend = false)
{
//注册指定方法为__autoload实现
spl_autoload_register(array($this, 'loadClass'), true, $prepend);
}
解读:注册该实例为一个自动加载器
13.
public function unregister()
{
//从spl中注销autoload
spl_autoload_unregister(array($this, 'loadClass'));
}
解读:注销指定类实例自动加载器
14.
public function loadClass($class)
{
//如果可以找到该类
if ($file = $this->findFile($class)) {
includeFile($file);
return true;
}
}
解读:加载指定类或接口
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: