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 接口。php
自身成员变量
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();
}
复制代码
赋值后的结果: laravel
继续分析 registerBaseServiceProviders();
数组
protected function registerBaseServiceProviders()
{
$this->register(new EventServiceProvider($this)); // 最后调用EventServiceProvider->register();
$this->register(new LogServiceProvider($this)); // 同上
$this->register(new RoutingServiceProvider($this)); // 同上
}
复制代码
全部基础服务涉及成员$serviceProviders,$loadedProiders,$bindings
bash
$serviceProvoiders 表示已经注册到应用的服务提供者对象,
而且包含是否延迟加载信息`
$loadedProviders 表示已经被加载的服务
$bindings 容器中的绑定数组
复制代码
registerCoreContainerAliases();
注册核心类的别名到容器app
到这里为止整个 Application 对象的构造方法完成composer
主要工做是初始化对象的一些成员属性,因为继承了 Container 类框架
继承的部分红员也进行初始化,上图就是在构造函数完成时赋值的相关成员ide
关于这些成员的做用会在后面框架启动使用中时经常使用到,目前为止不须要再了解更多。函数