版权声明:本文为博主原创文章,未经博主允许不得转载。

有些注释来着原文的百度翻译,可以有些难理解或者奇怪,我后面会根据自己的理解做调整的哈!!!不喜勿喷,层主英语不过关。。。

先来看看入口文件public/index.php

//请求头
header('Content-Type: application/json; charset=utf-8'); /*
|--------------------------------------------------------------------------
| Create The Application(创建应用程序)
|--------------------------------------------------------------------------
| 首先我们需要一个应用实例。
| 这将创建一个应用程序的实例/容器和应用程序准备好接收HTTP /控制台从环境要求。
*/
$app = require __DIR__.'/../bootstrap/app.php'; /*
|--------------------------------------------------------------------------
| Run The Application (运行应用程序)
|--------------------------------------------------------------------------
| 一旦我们有了应用程序,
| 我们就可以通过内核处理传入的请求,并将相关的响应发送回客户机的浏览器,
| 让他们享受我们为他们准备的创造性和奇妙的应用程序。
*/
$app->run();

  

那么现在最重要的就是bootstrap/app.php了

require_once __DIR__ . '/../vendor/autoload.php';

try {
(new Dotenv\Dotenv(__DIR__ . '/../'))->load();
} catch (Dotenv\Exception\InvalidPathException $e) {
//
}

这里是引入Composer的包,之前讲过,这里不详谈。Dotenv是env配置的使用。

往下看

/*
|--------------------------------------------------------------------------
| Create The Application(创建应用程序)
|--------------------------------------------------------------------------
|
| 在这里,我们将加载环境并创建作为该框架的中心部分的应用程序实例。
| 我们将使用这个应用程序作为这个框架的“IOC”容器和路由器。
|
*/ $app = new Laravel\Lumen\Application(
realpath(__DIR__ . '/../')
); $app->withFacades(); $app->withEloquent();
Laravel\Lumen\Application

    

  class Application extends Container
  {

    use Concerns\RoutesRequests,
      Concerns\RegistersExceptionHandlers;

     ...
  /**
* Create a new Lumen application instance.(创建一个新的Lumen应用程序实例。)
*/
public function __construct($basePath = null)
{
if (! empty(env('APP_TIMEZONE'))) {
date_default_timezone_set(env('APP_TIMEZONE', 'UTC'));
} $this->basePath = $basePath; $this->bootstrapContainer();
$this->registerErrorHandling();
}
这个可以说是整个Lumen应用程序最最核心的类了,上继承了核心容器类(Container),左右引用了Concerns\RoutesRequests(路由),Concerns\RegistersExceptionHandlers(异常处理),下又实例了app对象,很是重要喔!!!

$this->bootstrapContainer();
    /**
* Bootstrap the application container.(引导应用程序容器)
*
* @return void
*/
protected function bootstrapContainer()
{
static::setInstance($this); $this->instance('app', $this);
$this->instance('Laravel\Lumen\Application', $this); $this->instance('path', $this->path()); $this->registerContainerAliases();
}

Lumen的依赖注入服务主要都是在容器(Container)中进行,所以说这个步骤很重要,

先看看setInstance()函数

    /**
* Set the shared instance of the container.(设置容器的共享实例。)
*
* @param \Illuminate\Contracts\Container\Container|null $container
* @return static
*/
public static function setInstance(ContainerContract $container = null)
{
return static::$instance = $container;
}

简单一句话,把当前继承Container,实现ContainerContract接口的Application的app实例,注册到Container的$instance对象中,感觉这样就实现了相互之间的贯穿,后面会有大用!

然后就到了$this->instance()函数了,

  /**
* Register an existing instance as shared in the container.(登记一个现有的实例所共享的容器。)
*/
public function instance($abstract, $instance)
{
$this->removeAbstractAlias($abstract);//从上下文绑定缓存中删除一个别名。 $isBound = $this->bound($abstract);//确定给定的类型已被绑定。 unset($this->aliases[$abstract]);//删除对应注册类型别名。 //TODO 翻译修正
     //检查以确定此类型是否已被绑定,
//如果有我们将触发与容器注册的回调和反射可以消费类已经解决这里的更新。
$this->instances[$abstract] = $instance; if ($isBound) {
$this->rebound($abstract);//触发回调给定的抽象类型
}
}

再到$this->registerContainerAliases()函数,这个函数用于注释核心容器的别名

protected function registerContainerAliases()
{
$this->aliases = [
'Illuminate\Contracts\Foundation\Application' => 'app',
'Illuminate\Contracts\Auth\Factory' => 'auth',
'Illuminate\Contracts\Auth\Guard' => 'auth.driver',
'Illuminate\Contracts\Cache\Factory' => 'cache',
'Illuminate\Contracts\Cache\Repository' => 'cache.store',
'Illuminate\Contracts\Config\Repository' => 'config',
'Illuminate\Container\Container' => 'app',
'Illuminate\Contracts\Container\Container' => 'app',
'Illuminate\Database\ConnectionResolverInterface' => 'db',
'Illuminate\Database\DatabaseManager' => 'db',
'Illuminate\Contracts\Encryption\Encrypter' => 'encrypter',
'Illuminate\Contracts\Events\Dispatcher' => 'events',
'Illuminate\Contracts\Hashing\Hasher' => 'hash',
'log' => 'Psr\Log\LoggerInterface',
'Illuminate\Contracts\Queue\Factory' => 'queue',
'Illuminate\Contracts\Queue\Queue' => 'queue.connection',
'request' => 'Illuminate\Http\Request',
'Laravel\Lumen\Routing\UrlGenerator' => 'url',
'Illuminate\Contracts\Validation\Factory' => 'validator',
'Illuminate\Contracts\View\Factory' => 'view',
];
}

有木有对这些名称很熟悉的感觉!

至此$this->bootstrapContainer()就执行完了,再来看看$this->registerErrorHandling(),既然是异常处理,肯定是在Concerns\RegistersExceptionHandlers(异常处理)里了

    /**
* Set the error handling for the application.(设置应用程序的错误处理。)
*
* @return void
*/
protected function registerErrorHandling()
{
error_reporting(-1); set_error_handler(function ($level, $message, $file = '', $line = 0) {
if (error_reporting() & $level) {
throw new ErrorException($message, 0, $level, $file, $line);
}
}); set_exception_handler(function ($e) {
$this->handleUncaughtException($e);
}); register_shutdown_function(function () {
$this->handleShutdown();
});
}

这部分后面在写一篇文做补充,http://...

到这里,$app应用程序就初始好了,下一篇会接着讲

$app->withFacades();//为应用程序注册门面。

$app->withEloquent();//为应用程序加载功能强大的库。

和后面的内容!!!

版权声明:本文为博主原创文章,未经博主允许不得转载。

Lumen开发:lumen源码解读之初始化(1)——app实例的更多相关文章

  1. Lumen开发:lumen源码解读之初始化(4)——服务提供(ServiceProviders)与路由(Routes)

    版权声明:本文为博主原创文章,未经博主允许不得转载. 前面讲了singleton和Middleware,现在来继续讲ServiceProviders和Routes,还是看起始文件bootstrap/a ...

  2. Lumen开发:lumen源码解读之初始化(2)——门面(Facades)与数据库(db)

    版权声明:本文为博主原创文章,未经博主允许不得转载. 紧接上一篇 $app->withFacades();//为应用程序注册门面. $app->withEloquent();//为应用程序 ...

  3. mybatis源码解读(一)——初始化环境

    本系列博客将对mybatis的源码进行解读,关于mybatis的使用教程,可以查看我前面写的博客——传送门. 为了便于后面的讲解,我们这里首先构造一个统一环境.也可以参考mybatis官网. 1.数据 ...

  4. Lumen开发:lumen源码解读之初始化(3)——单例(singleton)与中间件(Middleware)

    版权声明:本文为博主原创文章,未经博主允许不得转载. 今天来讲讲Lumen的singleton和Middleware,先来看看起始文件bootstrap/app.php / * | --------- ...

  5. Lumen开发:lumen源码解读之初始化(5)——注册(register)与启动(boot)

    版权声明:本文为博主原创文章,未经博主允许不得转载. register()是在服务容器注册服务, bootstrap/app.php /** * 注册外部服务 */ $app->register ...

  6. Vue 源码解读(2)—— Vue 初始化过程

    当学习成为了习惯,知识也就变成了常识. 感谢各位的 点赞.收藏和评论. 新视频和文章会第一时间在微信公众号发送,欢迎关注:李永宁lyn 文章已收录到 github 仓库 liyongning/blog ...

  7. SDWebImage源码解读之SDWebImageDownloaderOperation

    第七篇 前言 本篇文章主要讲解下载操作的相关知识,SDWebImageDownloaderOperation的主要任务是把一张图片从服务器下载到内存中.下载数据并不难,如何对下载这一系列的任务进行设计 ...

  8. SDWebImage源码解读之SDWebImageCache(上)

    第五篇 前言 本篇主要讲解图片缓存类的知识,虽然只涉及了图片方面的缓存的设计,但思想同样适用于别的方面的设计.在架构上来说,缓存算是存储设计的一部分.我们把各种不同的存储内容按照功能进行切割后,图片缓 ...

  9. AFNetworking 3.0 源码解读 总结(干货)(下)

    承接上一篇AFNetworking 3.0 源码解读 总结(干货)(上) 21.网络服务类型NSURLRequestNetworkServiceType 示例代码: typedef NS_ENUM(N ...

随机推荐

  1. easyui numberbox precision属性

    //设置easyui numbox 最小值为0,保留2为小数 <input id="payPrice" type="text" name="pa ...

  2. Http协议三次握手过程

    Http协议三次握手过程   TCP(Transmission Control Protocol) 传输控制协议 TCP是主机对主机层的传输控制协议,提供可靠的连接服务,采用三次握手确认建立一个连接: ...

  3. leetcode 题解:Merge Sorted Array(两个已排序数组归并)

    题目: Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume ...

  4. tez参数

    https://tez.apache.org/releases/0.8.4/tez-api-javadocs/configs/TezConfiguration.html

  5. CentOS7关闭SELinux

    查看 [root@dev-server ~]# getenforce Disabled [root@dev-server ~]# /usr/sbin/sestatus -v SELinux statu ...

  6. ASP.NET MVC学习---(三)EF简单增删改查

    那么现在我们已经大概从本质上了解了ef 巴拉巴拉说了一大堆之后 总算要进入ef的正题了 总在口头说也太不行了是吧~ 没错,现在要用ef进行一些实际的操作 做什么呢? 就做一个入门级的增删改查操作吧 废 ...

  7. 配置Linux实现静态路由

    配置Linux实现静态路由 背景和原理 路由器的功能是实现一个网段到另一个网段之间的通信,路由分为静态路由.动态路由. 默认路由和直连路由.静态路由是手工指定的,使用静态路由的好处是网络安全保密性高. ...

  8. wmi在渗透测试中的运用

    Abusing WMI to Build a Persistent, Asynchronous, and Fileless Backdoor 滥用 WMI 打造一个永久.异步.无文件后门 http:/ ...

  9. Android学习(十) SQLite 基于SQLiteOpenHelper的操作方式

    main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns ...

  10. 【转】 从输入 URL 到页面加载完成的过程中都发生了什么事情?

    该问题总结 一. 往浏览器输入URL后给你一个页面,你天天在使用的东西,学过计算机网络的知道是怎么回事,就DNS解析然后页面的回馈,不过要讲好还是有难度. 之前fex团队的nwind专门写过这个问题的 ...