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

本文深入解析Laravel框架中的Application类,探讨其在框架中的核心作用,包括类的继承、成员变量、构造函数及初始化过程。理解Application如何初始化并管理服务提供者和服务,是掌握Laravel框架关键的第一步。

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->registerBaseBindings();

        $this->registerBaseServiceProviders();

        $this->registerCoreContainerAliases();
    }
复制代码

setBasePath($basePath);

 对$instances赋值
复制代码

registerBaseBindings();

 初始化$instance成员的值为$this (Application对象)    
 继续对$instances赋值
复制代码

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 类, 继承的部分成员也进行初始化,上图就是在构造函数完成时赋值的相关成员,关于这些成员的作用会在后面框架启动使用中时常用到,目前为止不需要再了解更多。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值