【Laravel-海贼王系列】第二章,Application类解析

666 阅读2分钟

Application 是框架作为应用最核心的一个类!

Application 头部声明

namespace Illuminate\Foundation;

use Closure; // php 默认匿名函数类
use RuntimeException; // 运行异常类
use Illuminate\Support\Arr; // laravel 数组操作类
use Illuminate\Support\Str; // laravel 字符串操作类
use Illuminate\Http\Request; // laravel 请求对象类
use Illuminate\Support\Collection; // laravel 集合类
use Illuminate\Container\Container; // laravel 容器类
use Illuminate\Filesystem\Filesystem; // laravel 文件系统类
use Illuminate\Log\LogServiceProvider; // laravel 日志服务提供者
use Illuminate\Support\ServiceProvider; // laravel 服务提供者类
use Illuminate\Events\EventServiceProvider;// laravel 事件服务提供者
use Illuminate\Routing\RoutingServiceProvider;// laravel 路由服务提供者
use Symfony\Component\HttpKernel\HttpKernelInterface;// Symfony 内核接口
use Symfony\Component\HttpKernel\Exception\HttpException;/ /Symfony Http 异常类
use Illuminate\Contracts\Http\Kernel as HttpKernelContract;// laravel 内核类
use Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables;// laravel 载入环境变量类
use Symfony\Component\HttpFoundation\Request as SymfonyRequest; // Symfony 请求类
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; // Symfony NotFound 异常类
use Illuminate\Contracts\Foundation\Application as ApplicationContract;// laravel 应用契约

class Application extends Container implements ApplicationContract, HttpKernelInterface

Application 对象继承了 Container 对象,同时实现 ApplicationContract 契约和 HttpKernelInterface 接口。

成员变量

自身成员变量  
const VERSION = '5.7.26'; // 当前版本号
protected $basePath; // 应用的基础路径
protected $hasBeenBootstrapped = false; // 标识应用之前是否启动过
protected $booted = false; // 标识应用是否已经被启动
protected $bootingCallbacks = []; // 应用在启动中的回调
protected $bootedCallbacks = []; // 应用启动后的回调
protected $terminatingCallbacks = []; // 应用终止中的回调
protected $serviceProviders = []; // 所有注册的服务提供者
protected $loadedProviders = []; // 已经加载的服务提供者
protected $deferredServices = []; // 延迟加载的服务提供者
protected $appPath; // 应用路径
protected $databasePath; // 数据路径
protected $storagePath; // 存储目录路径
protected $environmentPath; // 环境变量路径
protected $environmentFile = '.env';
protected $namespace; // 从composer的autoload.psr-4中读取,对应的值为'App\'

继承于 Container 的成员
protected static $instance; 
protected $resolved = [];
protected $bindings = [];
protected $methodBindings = [];
protected $instances = [];
protected $aliases = [];
protected $abstractAliases = [];
protected $extenders = [];
protected $tags = [];
protected $buildStack = [];
protected $with = [];
public $contextual = [];
protected $reboundCallbacks = [];
protected $globalResolvingCallbacks = [];
protected $globalAfterResolvingCallbacks = [];
protected $resolvingCallbacks = [];
protected $afterResolvingCallbacks = [];

构造函数

public function __construct($basePath = null)
{
    if ($basePath) {
        $this->setBasePath($basePath);  // 对$this->instances赋值
    }
    
    $this->registerBaseBindings(); //初始化$instance成员的值为$this (Application对象)  ,继续对$this->instances赋值

    $this->registerBaseServiceProviders();

    $this->registerCoreContainerAliases();
}

赋值后的结果:

继续分析 registerBaseServiceProviders();

protected function registerBaseServiceProviders()
    {
        $this->register(new EventServiceProvider($this));  // 最后调用EventServiceProvider->register();
        $this->register(new LogServiceProvider($this)); // 同上
        $this->register(new RoutingServiceProvider($this)); // 同上
    }

所有基础服务涉及成员$serviceProviders,$loadedProiders,$bindings

 $serviceProvoiders 表示已经注册到应用的服务提供者对象,
 并且包含是否延迟加载信息`

 $loadedProviders 表示已经被加载的服务
 
 $bindings 容器中的绑定数组

registerCoreContainerAliases(); 注册核心类的别名到容器

总结

到这里为止整个 Application 对象的构造方法完成

主要工作是初始化对象的一些成员属性,由于继承了 Container 类

继承的部分成员也进行初始化,上图就是在构造函数完成时赋值的相关成员

关于这些成员的作用会在后面框架启动使用中时常用到,目前为止不需要再了解更多。