【再读lara】生命周期 ~ 服务容器实例化

程序启动准备阶段

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;
实现服务容器实例化,并向服务容器绑定核心类服务,返回服务容器实例

Illuminate\Foundation\Application.php

 //创建一个新的服务容器 
public function __construct($basePath = null)
{
        if ($basePath) { $this->setBasePath($basePath);}

        $this->registerBaseBindings();
        $this->registerBaseServiceProviders();
        $this->registerCoreContainerAliases();
}

//向容器中注册基础绑定
protected function registerBaseBindings()
{
    static::setInstance($this);
    $this->instance('app', $this);
    $this->instance('Illuminate\Container\Container', $this);
}

 //  注册基础服务提供者 
protected function registerBaseServiceProviders()
{
    $this->register(new EventServiceProvider($this));
    $this->register(new RoutingServiceProvider($this));
}
//在服务容器中注册一个服务提供者   
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;
}
//如果服务提供者已经存在, 则获取这个实例对象
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;
        });
}
//通过类名实例化一个服务提供者
 public function resolveProviderClass($provider)
 {
        return new $provider($this);
 }
 //启动规定的服务提供者
 protected function bootProvider(ServiceProvider $provider)
 {
        if (method_exists($provider, 'boot')) {
            return $this->call([$provider, 'boot']);
        }
}

   //在容器中注册核心类的别名
    public function registerCoreContainerAliases()
    {
        $aliases = [
          'app'     => [
            'Illuminate\Foundation\Application', 
            'Illuminate\Contracts\Container\Container',
            'Illuminate\Contracts\Foundation\Application'],
            'auth'    => 'Illuminate\Auth\AuthManager',
            //这里省略了别名数组中部分内容
        ];

        foreach ($aliases as $key => $aliases) {
            foreach ((array) $aliases as $alias) {
                $this->alias($key, $alias);
            }
        }
}
//注册应用的基础路径
    public function setBasePath($basePath)
    {
        $this->basePath = rtrim($basePath, '\/');
        $this->bindPathsInContainer();
        return $this;
    }

//在容器中绑定应用程序的基础路径 
    protected function bindPathsInContainer()
    {
        $this->instance('path', $this->path());
        foreach (['base', 'config', 'database', 'lang', 'public', 'storage'] as $path) {
            $this->instance('path.'.$path, $this->{$path.'Path'}());
        }
    }

你可能感兴趣的:(【再读lara】生命周期 ~ 服务容器实例化)