既然我是走進(jìn)去的,就把我當(dāng)做 apache 吧把沼,我看到的第一個(gè)文件應(yīng)該是項(xiàng)目中 public 的 index.php 文件啡直。
define('LARAVEL_START', microtime(true));
解讀:在運(yùn)行時(shí)定義一個(gè)常量 LARAVEL_START 為當(dāng)前 Unix 時(shí)間戳房官。
require __DIR__.'/../vendor/autoload.php';
解讀:引入一個(gè)自動(dòng)加載器
2.1 進(jìn)入到 autoload.php 中
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit94ec56b468e10706582fb576e66d9831::getLoader();
解讀:從 autoload_real.php 中引入自加載入口 getLoader
public static function getLoader()
{
//如果 $loader 非空袖牙,返回 $loader
if (null !== self::$loader) {
return self::$loader;
}
//spl_autoload_register:注冊(cè)指定的函數(shù) loadClassLoader 作為__autoload的實(shí)現(xiàn), throw:拋出異常肯尺,prepend:添加到隊(duì)列之首
spl_autoload_register(array('ComposerAutoloaderInit94ec56b468e10706582fb576e66d9831', 'loadClassLoader'), true, true);
// 創(chuàng)建 \Composer\Autoload\ClassLoader() 對(duì)象,并對(duì)象化類屬性 $loader
self::$loader = $loader = new \Composer\Autoload\ClassLoader();
// 注銷已注冊(cè)的指定的__autoload()函數(shù)loadClassLoader
spl_autoload_unregister(array('ComposerAutoloaderInit94ec56b468e10706582fb576e66d9831', 'loadClassLoader'));
// 判斷 php 版本是否大于5.6躯枢,且沒有定義 HHVM 版本(一個(gè)高性能 PHP 執(zhí)行引擎则吟,類似于 apache ),
//且 (不存在函數(shù) zend_loader_file_encoded 或未執(zhí)行 zend_loader_file_encoded )
$useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION')
&& (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded());
if ($useStaticLoader) {
// 加載 autoload_static.php 的 ComposerStaticInit94ec56b468e10706582fb576e66d9831 類锄蹂,
//一個(gè)靜態(tài)信息文件氓仲,包含了需要自動(dòng)加載的files路徑、psr 規(guī)范信息得糜、laravel 框架類的路徑
require_once __DIR__ . '/autoload_static.php';
//執(zhí)行 ComposerStaticInit94ec56b468e10706582fb576e66d9831 類中的 getInitializer 靜態(tài)函數(shù)敬扛。
//call_user_func:運(yùn)行作為第一個(gè)參數(shù)的回調(diào)函數(shù),如果執(zhí)行失敗返回 false朝抖,避免發(fā)生錯(cuò)誤
/**
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit94ec56b468e10706582fb576e66d9831::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit94ec56b468e10706582fb576e66d9831::$prefixDirsPsr4;
$loader->prefixesPsr0 = ComposerStaticInit94ec56b468e10706582fb576e66d9831::$prefixesPsr0;
$loader->classMap = ComposerStaticInit94ec56b468e10706582fb576e66d9831::$classMap;
}, null, ClassLoader::class);
}
*/
// getInitializer 其實(shí)就是在返回 files 路徑啥箭、psr 規(guī)范信息、laravel 框架類的路徑
call_user_func(\Composer\Autoload\ComposerStaticInit94ec56b468e10706582fb576e66d9831::getInitializer($loader));
} else {
//加載 autoload_namespaces.php治宣,返回符合 prs0 自加載插件的命名空間-路徑數(shù)組
$map = require __DIR__ . '/autoload_namespaces.php';
foreach ($map as $namespace => $path) {
//向 $loader 添加自加載插件
$loader->set($namespace, $path);
}
//加載 autoload_psr4.php急侥,返回符合 prs4 自加載插件的命名空間-路徑數(shù)組
$map = require __DIR__ . '/autoload_psr4.php';
foreach ($map as $namespace => $path) {
//向 $loader 添加自加載插件
$loader->setPsr4($namespace, $path);
}
//加載 autoload_classmap.php,返回各種需自加載類的命名空間-路徑數(shù)組
$classMap = require __DIR__ . '/autoload_classmap.php';
if ($classMap) {
//向 $loader 添加自加載類
$loader->addClassMap($classMap);
}
}
// 預(yù)加載 autoloader
$loader->register(true);
// 加載框架文件
if ($useStaticLoader) {
$includeFiles = Composer\Autoload\ComposerStaticInit94ec56b468e10706582fb576e66d9831::$files;
} else {
$includeFiles = require __DIR__ . '/autoload_files.php';
}
foreach ($includeFiles as $fileIdentifier => $file) {
composerRequire94ec56b468e10706582fb576e66d9831($fileIdentifier, $file);
}
return $loader;
}
解讀:查看代碼注釋
$app = require_once __DIR__.'/../bootstrap/app.php';
解讀:引入 laravel 框架實(shí)例
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
$response = $kernel->handle(
$request = Illuminate\Http\Request::capture()
);
$response->send();
$kernel->terminate($request, $response);
解讀:?jiǎn)?dòng) laravel 應(yīng)用與 http 交互侮邀,完成 http 請(qǐng)求和響應(yīng)
/**
* Resolve the given type from the container.
* 映射指定類型的容器
* (Overriding Container::make)
*
* @param string $abstract
* @param array $parameters
* @return mixed
*/
public function make($abstract, array $parameters = [])
{
$abstract = $this->getAlias($abstract);
if (isset($this->deferredServices[$abstract]) && ! isset($this->instances[$abstract])) {
$this->loadDeferredProvider($abstract);
}
return parent::make($abstract, $parameters);
}
// Illuminate\Container\Container::class
/**
* Get the alias for an abstract if available.
* 獲取可信賴的類組
* @param string $abstract
* @return string
*
* @throws \LogicException
*/
public function getAlias($abstract)
{
if (! isset($this->aliases[$abstract])) {
return $abstract;
}
if ($this->aliases[$abstract] === $abstract) {
throw new LogicException("[{$abstract}] is aliased to itself.");
}
return $this->getAlias($this->aliases[$abstract]);
}
// Illuminate\Foundation\Application::class
public function loadDeferredProvider($service)
{
if (! isset($this->deferredServices[$service])) {
return;
}
$provider = $this->deferredServices[$service];
// If the service provider has not already been loaded and registered we can
// register it with the application and remove the service from this list
// of deferred services, since it will already be loaded on subsequent.
// 當(dāng)服務(wù)提供者即將被加載坏怪,如果服務(wù)提供者還沒有被加載和注冊(cè),
// 我們可以在這個(gè)應(yīng)用中注冊(cè)它绊茧,并且去除
// 這個(gè)列表中已經(jīng)緩存的服務(wù)
if (! isset($this->loadedProviders[$provider])) {
$this->registerDeferredProvider($provider, $service);
}
}
public function registerDeferredProvider($provider, $service = null)
{
// Once the provider that provides the deferred service has been registered we
// will remove it from our local list of the deferred services with related
// providers so that this container does not try to resolve it out again.
// 一旦服務(wù)提供者提供被注冊(cè)的緩存的服務(wù)铝宵,我們將從我們本地緩存的服務(wù)列表中去除它和
//與它相關(guān)聯(lián)的服務(wù)提供者,如此這個(gè)容器就不會(huì)再嘗試映射它华畏。
if ($service) {
unset($this->deferredServices[$service]);
}
$this->register($instance = new $provider($this));
if (! $this->booted) {
$this->booting(function () use ($instance) {
$this->bootProvider($instance);
});
}
}
————————————————
原文作者:LeoYao
轉(zhuǎn)自鏈接:https://learnku.com/articles/9052/i-walk-into-laravel55-alone-1
版權(quán)聲明:著作權(quán)歸作者所有鹏秋。商業(yè)轉(zhuǎn)載請(qǐng)聯(lián)系作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請(qǐng)保留以上作者信息和原文鏈接唯绍。