public\index.php
require __DIR__.'/../bootstrap/autoload.php';
$app = require_once __DIR__.'/../bootstrap/app.php';
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
$response = $kernel->handle($request = Illuminate\Http\Request::capture());
$response->send();
$kernel->terminate($request, $response);
bootstrap\app.php
$app = new Illuminate\Foundation\Application(
realpath(__DIR__.'/../')
);
$app->singleton(
Illuminate\Contracts\Http\Kernel::class,
App\Http\Kernel::class
);
$app->singleton(
Illuminate\Contracts\Console\Kernel::class,
App\Console\Kernel::class
);
$app->singleton(
Illuminate\Contracts\Debug\ExceptionHandler::class,
App\Exceptions\Handler::class
);
return $app;
實(shí)現(xiàn)服務(wù)容器實(shí)例化,并向服務(wù)容器綁定核心類服務(wù),返回服務(wù)容器實(shí)例
Illuminate\Foundation\Application.php
//創(chuàng)建一個(gè)新的服務(wù)容器
public function __construct($basePath = null)
{
if ($basePath) { $this->setBasePath($basePath);}
$this->registerBaseBindings();
$this->registerBaseServiceProviders();
$this->registerCoreContainerAliases();
}
//向容器中注冊(cè)基礎(chǔ)綁定
protected function registerBaseBindings()
{
static::setInstance($this);
$this->instance('app', $this);
$this->instance('Illuminate\Container\Container', $this);
}
// 注冊(cè)基礎(chǔ)服務(wù)提供者
protected function registerBaseServiceProviders()
{
$this->register(new EventServiceProvider($this));
$this->register(new RoutingServiceProvider($this));
}
//在服務(wù)容器中注冊(cè)一個(gè)服務(wù)提供者
public function register($provider, $options = [], $force = false)
{
if ($registered = $this->getProvider($provider) && !$force) {
return $registered;
}
if (is_string($provider)) {
$provider = $this->resolveProviderClass($provider);
}
$provider->register();
$provider = $this->resolveProviderClass($provider);
}
$provider->register();
foreach ($options as $key => $value) {
$this[$key] = $value;
}
$this->markAsRegistered($provider);
if ($this->booted) {
$this->bootProvider($provider);
}
return $provider;
}
//如果服務(wù)提供者已經(jīng)存在, 則獲取這個(gè)實(shí)例對(duì)象
public function getProvider($provider)
{
$name = is_string($provider) ? $provider : get_class($provider);
return Arr::first($this->serviceProviders, function ($key, $value) use ($name) {
return $value instanceof $name;
});
}
//通過(guò)類名實(shí)例化一個(gè)服務(wù)提供者
public function resolveProviderClass($provider)
{
return new $provider($this);
}
//啟動(dòng)規(guī)定的服務(wù)提供者
protected function bootProvider(ServiceProvider $provider)
{
if (method_exists($provider, 'boot')) {
return $this->call([$provider, 'boot']);
}
}
//在容器中注冊(cè)核心類的別名
public function registerCoreContainerAliases()
{
$aliases = [
'app' => [
'Illuminate\Foundation\Application',
'Illuminate\Contracts\Container\Container',
'Illuminate\Contracts\Foundation\Application'],
'auth' => 'Illuminate\Auth\AuthManager',
//這里省略了別名數(shù)組中部分內(nèi)容
];
foreach ($aliases as $key => $aliases) {
foreach ((array) $aliases as $alias) {
$this->alias($key, $alias);
}
}
}
//注冊(cè)應(yīng)用的基礎(chǔ)路徑
public function setBasePath($basePath)
{
$this->basePath = rtrim($basePath, '\/');
$this->bindPathsInContainer();
return $this;
}
//在容器中綁定應(yīng)用程序的基礎(chǔ)路徑
protected function bindPathsInContainer()
{
$this->instance('path', $this->path());
foreach (['base', 'config', 'database', 'lang', 'public', 'storage'] as $path) {
$this->instance('path.'.$path, $this->{$path.'Path'}());
}
}