Laravel源码入门-启动引导过程(三)bootstrap/app.php

上篇:Laravel源码入门-启动引导过程(二)bootstrap/autoload.php

从请求发起开始,进入 public/index.php,到 bootstrap/autoload.php,下一步就进入了创建应用实例,代码如下:

/*  /* 来自 public/index.php */
|
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate PHP development, so let us turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight our users.
|
*/

$app = require_once __DIR__.'/../bootstrap/app.php'

这句代码成为点亮(Turn on),创建了 $app,一切都有了开始,我们看看 bootstrap/app.php。

注:我本来在这里要配置$app->useStoragePath (Storage的目录)写$_ENV的时候读不到,原因php.ini中variables_order = "GPCS"全局变量中没有加env,改成variables_order = "EGPCS" ,(Environment、Get、Post、Cookies、Server)。

<?php /* bootstrap/app.php */

/*
|--------------------------------------------------------------------------
| Create The Application 创建应用实例
|--------------------------------------------------------------------------
|
| The first thing we will do is create a new Laravel application instance
| which serves as the "glue" for all the components of Laravel, and is
| the IoC container for the system binding all of the various parts.
|
| $app 是 Application 的实例,在面向对象编程(OOP)中实例是基本单位,原因就是面向对象
| 编程,不是面向过程编程,在我们眼中,要开发的东西都是对象,跑起来后都是一个一个的实例。
| 
| $app 这个实例,是个最开始的,也是最基本的实例,说像胶水,应该说像蜘蛛,能产丝一样,把
| Laravel 的各个部分(其实也都是对象)连接起来,有机的连接,就能跑起来了!
|
| $app 也是个容器(Container),Application 继承自 Container,粗浅理解,$app,
| 更像一副骨架,带皮肤的骨架,还没有内部各种组织,是个空的容器,当我们往里填充时,使用的
| 是 Injection 的方法,像科幻片,注入一种生物水,那种生物就在容器内繁殖起来,并发挥作用
| ,如果非要用个好听的名字,用《攻壳特工队》里面的话,就是强化,注入强化!再回顾一下上篇,
| 有一个 make() 方法,这个方法意味解析(resolve)
| 而不是简单的 new 操作,之所以意味着解析(resolve),是因为 $app 是一个容器,各种组件像药水
| 一样以 Injection 的方式注入到了 $app 体内,而在体外,比如 index.php 文件就相当于在 $app
| 体外,要获得 $kernel,就可以用 $app->make(类的名字) 从各种有机融合在一起的药水中来析出来,
| 这也就是 $app 是一个容器的 IoC 控制反转 的理解方式。
| 具体参见https://laravel.com/docs/5.4/container,搜索 “make method”。
|
| 备注:realpath() 函数返回值,作为 Application 构造函数输入参数,这里值为:
| 部署目录:/Users/me/mykaifa/laravel_orig,也就是 bootstrap 的上一级目录。
|
*/

$app = new Illuminate\Foundation\Application(
   $_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
);

/*
|--------------------------------------------------------------------------
| Bind Important Interfaces 绑定重要接口
|--------------------------------------------------------------------------
|
| Next, we need to bind some important interfaces into the container so
| we will be able to resolve them when needed. The kernels serve the
| incoming requests to this application from both the web and CLI.
|
| 这里绑定的三个接口实在重要,一个Web端Kernel,二个是命令行端Kernel,最后一个是异常处理
| 。最基本的。用单例(singleton)来绑定,把三个协议 Contracts 纯接口(无实现)绑定到
| 具体的 App\Http\、App\Console\和App\Exceptions 下的。查阅 Container::singleton()
| 实现代码:singleton($abstract, $concrete),不知是不是 abstract 和 concrete 的关系。
|
*/

$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 The Application 脚本执行完返回 $app 到 public/index.php
|--------------------------------------------------------------------------
|
| This script returns the application instance. The instance is given to
| the calling script so we can separate the building of the instances
| from the actual running of the application and sending responses.
|
*/

return $app;

== 总结 ==

bootstrap/app.php 两三行代码,做了大量工作,如果用 dump($app),可以看到。总之是创建了 $app 容器实例,等待着更丰富的注入,让应用活起来、跑起来。

下篇:Laravel源码入门-启动引导过程(四)app/Http/Kernel.php

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章