我独自走进 Laravel5.5 的❤(三)
上一节我走到了classloader的findFile(),接下来我将继续走下去
1.
public function findFile($class)
{
// class map lookup
//判断是否已经设置指定类
if (isset($this->classMap[$class])) {
//返回该类
return $this->classMap[$class];
}
//判断该类是否已经注册或已经设置指定类为忽略类
if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) {
//返回false
return false;
}
//如果指定类的apcu前缀不为空
if (null !== $this->apcuPrefix) {
//对比指定类的apcu前缀和apcu缓存中的类的前缀,返回$hit(success或false)
$file = apcu_fetch($this->apcuPrefix.$class, $hit);
if ($hit) {
//如果对比成功返回对比结果
return $file;
}
}
//寻找指定类的扩展
$file = $this->findFileWithExtension($class, '.php');
// Search for Hack files if we are running on HHVM
//寻找破解文件如果我们正在运行在HHVM引擎上
//如果寻找指定类扩展失败且没有定义HHVM版本
if (false === $file && defined('HHVM_VERSION')) {
//定义指定类扩展为".hh"
$file = $this->findFileWithExtension($class, '.hh');
}
//如果指定类apcu前缀不为空
if (null !== $this->apcuPrefix) {
//apcu缓存添加指定变量(指定类apcu前置+指定类名)为指定类扩展
apcu_add($this->apcuPrefix.$class, $file);
}
//如果没有找到指定类扩展
if (false === $file) {
// Remember that this class does not exist.
//保存指定类不存在
$this->missingClasses[$class] = true;
}
//返回指定类定义的文件的路径,否则返回false
return $file;
}
解读:根据文件名寻找路径
2.
private function findFileWithExtension($class, $ext)
{
// PSR-4 lookup
//替换psr4规范文件扩展中的'\\'为DIRECTORY_SEPARATOR('\')+$ext
$logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext;
//类前缀
$first = $class[0];
//判断是否已经注册类前缀
if (isset($this->prefixLengthsPsr4[$first])) {
//获取类路径
$subPath = $class;
//当取到有'\\'的类路径
while (false !== $lastPos = strrpos($subPath, '\\')) {
//提取'\\'前的字符串
$subPath = substr($subPath, 0, $lastPos);
//添加'\\'
$search = $subPath.'\\';
//判断是否已经注册类命名空间
if (isset($this->prefixDirsPsr4[$search])) {
//循环指定类空间路径
foreach ($this->prefixDirsPsr4[$search] as $dir) {
//获取长度
$length = $this->prefixLengthsPsr4[$first][$search];
//如果存在寻找的文件
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $length))) {
//返回路径
return $file;
}
}
}
}
}
// PSR-4 fallback dirs
//循环psr4规范文件回调路径
foreach ($this->fallbackDirsPsr4 as $dir) {
//如果存在指定类回调路径
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) {
//返回路径
return $file;
}
}
// PSR-0 lookup
//如果指定类名含有'\\',strrpos:指定字符串最后出现的位置
if (false !== $pos = strrpos($class, '\\')) {
// namespaced class name
//获取psr4规范路径
$logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1)
. strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR);
} else {
// PEAR-like class name
//获取psr0规范文件路径
$logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext;
}
//如果设置了psr0规范前缀
if (isset($this->prefixesPsr0[$first])) {
//循环psr0规范数组
foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) {
//如果已注册类前缀和指定类前缀一样
if (0 === strpos($class, $prefix)) {
//循环路径
foreach ($dirs as $dir) {
//如果存在文件或路径存在
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
//返回路径
return $file;
}
}
}
}
}
// PSR-0 fallback dirs
//psr0规范回调路径
//循环psr0规范路径
foreach ($this->fallbackDirsPsr0 as $dir) {
//如果存在文件或路径
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
//返回路径
return $file;
}
}
// PSR-0 include paths.
//psr0规范包含路径
//如果应用包含路径存在并且包含路径中解析了指定psr0规范文件物理路径
if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) {
//返回路径
return $file;
}
//找不到文件,返回false
return false;
}
解读:根据扩展寻找文件
3.
function includeFile($file)
{
include $file;
}
解读:隔离包含,包含文件无法访问指定指定文件
本作品采用《CC 协议》,转载必须注明作者和本文链接