首页 > 后端开发 > php教程 > laravel5源码讲解整理,laravel5源码讲解_PHP教程

laravel5源码讲解整理,laravel5源码讲解_PHP教程

WBOY
发布: 2016-07-12 09:04:51
原创
1414 人浏览过

laravel5源码讲解整理,laravel5源码讲解

来源:http://yuez.me/laravel-yuan-ma-jie-du/?utm_source=tuicool&utm_medium=referral

目录

  • 入口文件 index.php
    • Illuminate\Foundation\Application 类
    • 注入所有基础 Service Provider

 

入口文件 index.php

一个基于Laravel的应用,当WEB服务器接受到来自外部的请求后,会将这个这个请求解析到 应用根目录的 public/index.php 中。

Laravel源码解读-index.php (laravel_index.php)download

<span class="line-number">1
<span class="line-number">2
<span class="line-number">3
<span class="line-number">4
<span class="line-number">5
<span class="line-number">6
<span class="line-number">7
<span class="line-number">8
<span class="line-number">9
<span class="line-number">10
<span class="line-number">11
<span class="line-number">12
<span class="line-number">13
<span class="line-number">14
<span class="line-number">15
<span class="line-number">16
<span class="line-number">17
<span class="line-number">18
<span class="line-number">19
<span class="line-number">20
<span class="line-number">21
<span class="line-number">22
<span class="line-number">23
<span class="line-number">24
<span class="line-number">25
<span class="line-number">26
<span class="line-number">27
<span class="line-number">28
<span class="line-number">29
<span class="line-number">30
<span class="line-number">31
<span class="line-number">32
<span class="line-number">33
<span class="line-number">34
<span class="line-number">35
<span class="line-number">36
<span class="line-number">37
<span class="line-number">38
<span class="line-number">39
<span class="line-number">40
<span class="line-number">41
<span class="line-number">42
<span class="line-number">43
<span class="line-number">44
<span class="line-number">45
<span class="line-number">46
<span class="line-number">47
<span class="line-number">48
<span class="line-number">49
<span class="line-number">50
<span class="line-number">51
<span class="line-number">52
<span class="line-number">53
<span class="line-number">54
<span class="line-number">55
<span class="line-number">56
<span class="line-number">57
</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
登录后复制
<code class="php"><span class="line"><span class="o"><?<span class="nx">php
<span class="line"><span class="sd">/**
<span class="line"><span class="sd"> * Laravel - A PHP Framework For Web Artisans
<span class="line"><span class="sd"> *
<span class="line"><span class="sd"> * @package  Laravel
<span class="line"><span class="sd"> * @author   Taylor Otwell <taylorotwell@gmail.com>
<span class="line"><span class="sd"> */
<span class="line">
<span class="line"><span class="cm">/*
<span class="line"><span class="cm">|--------------------------------------------------------------------------
<span class="line"><span class="cm">| Register The Auto Loader
<span class="line"><span class="cm">|--------------------------------------------------------------------------
<span class="line"><span class="cm">|
<span class="line"><span class="cm">| Composer provides a convenient, automatically generated class loader for
<span class="line"><span class="cm">| our application. We just need to utilize it! We'll simply require it
<span class="line"><span class="cm">| into the script here so that we don't have to worry about manual
<span class="line"><span class="cm">| loading any of our classes later on. It feels nice to relax.
<span class="line"><span class="cm">|
<span class="line"><span class="cm">*/
<span class="line">
<span class="line"><span class="k">require <span class="nx">__DIR__<span class="o">.<span class="s1">'/../bootstrap/autoload.php'<span class="p">;
<span class="line">
<span class="line"><span class="cm">/*
<span class="line"><span class="cm">|--------------------------------------------------------------------------
<span class="line"><span class="cm">| Turn On The Lights
<span class="line"><span class="cm">|--------------------------------------------------------------------------
<span class="line"><span class="cm">|
<span class="line"><span class="cm">| We need to illuminate PHP development, so let us turn on the lights.
<span class="line"><span class="cm">| This bootstraps the framework and gets it ready for use, then it
<span class="line"><span class="cm">| will load up this application so that we can run it and send
<span class="line"><span class="cm">| the responses back to the browser and delight our users.
<span class="line"><span class="cm">|
<span class="line"><span class="cm">*/
<span class="line">
<span class="line"><span class="nv">$app <span class="o">= <span class="k">require_once <span class="nx">__DIR__<span class="o">.<span class="s1">'/../bootstrap/app.php'<span class="p">;
<span class="line">
<span class="line"><span class="cm">/*
<span class="line"><span class="cm">|--------------------------------------------------------------------------
<span class="line"><span class="cm">| Run The Application
<span class="line"><span class="cm">|--------------------------------------------------------------------------
<span class="line"><span class="cm">|
<span class="line"><span class="cm">| Once we have the application, we can handle the incoming request
<span class="line"><span class="cm">| through the kernel, and send the associated response back to
<span class="line"><span class="cm">| the client's browser allowing them to enjoy the creative
<span class="line"><span class="cm">| and wonderful application we have prepared for them.
<span class="line"><span class="cm">|
<span class="line"><span class="cm">*/
<span class="line">
<span class="line"><span class="nv">$kernel <span class="o">= <span class="nv">$app<span class="o">-><span class="na">make<span class="p">(<span class="nx">Illuminate\Contracts\Http\Kernel<span class="o">::<span class="na">class<span class="p">);
<span class="line">
<span class="line"><span class="nv">$response <span class="o">= <span class="nv">$kernel<span class="o">-><span class="na">handle<span class="p">(
<span class="line">    <span class="nv">$request <span class="o">= <span class="nx">Illuminate\Http\Request<span class="o">::<span class="na">capture<span class="p">()
<span class="line"><span class="p">);
<span class="line">
<span class="line"><span class="nv">$response<span class="o">-><span class="na">send<span class="p">();
<span class="line">
<span class="line"><span class="nv">$kernel<span class="o">-><span class="na">terminate<span class="p">(<span class="nv">$request<span class="p">, <span class="nv">$response<span class="p">);
</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>
登录后复制

第二十一行代码

<span class="line-number">1
</span>
登录后复制
登录后复制
<code class="php"><span class="line"><span class="k">require <span class="nx">__DIR__<span class="o">.<span class="s1">'/../bootstrap/autoload.php'<span class="p">;
</span></span></span></span></span></span></code>
登录后复制

为Laravel应用引入了由Composer提供的类加载器,这样Laravel应用便无需再手动加载任 何的类。其加载原理不是此次探究的目标,所以仅仅这样使用就好了。接下的代码,便是重 点。

Illuminate\Foundation\Application 类

该类的继承结构如下:

类继承结构

第三十五行代码

<span class="line-number">1
</span>
登录后复制
登录后复制
<code class="php"><span class="line"><span class="nv">$app <span class="o">= <span class="k">require_once <span class="nx">__DIR__<span class="o">.<span class="s1">'/../bootstrap/app.php'<span class="p">;
</span></span></span></span></span></span></span></span></code>
登录后复制

它将我的视线引入到了另外一个文件中,去看看到底发生了什么吧。

Laravel源码解读-app.php (laravel_app.php)download

<span class="line-number">1
<span class="line-number">2
<span class="line-number">3
<span class="line-number">4
<span class="line-number">5
<span class="line-number">6
<span class="line-number">7
<span class="line-number">8
<span class="line-number">9
<span class="line-number">10
<span class="line-number">11
<span class="line-number">12
<span class="line-number">13
<span class="line-number">14
<span class="line-number">15
<span class="line-number">16
<span class="line-number">17
<span class="line-number">18
<span class="line-number">19
<span class="line-number">20
<span class="line-number">21
<span class="line-number">22
<span class="line-number">23
<span class="line-number">24
<span class="line-number">25
<span class="line-number">26
<span class="line-number">27
<span class="line-number">28
<span class="line-number">29
<span class="line-number">30
<span class="line-number">31
<span class="line-number">32
<span class="line-number">33
<span class="line-number">34
<span class="line-number">35
<span class="line-number">36
<span class="line-number">37
<span class="line-number">38
<span class="line-number">39
<span class="line-number">40
<span class="line-number">41
<span class="line-number">42
<span class="line-number">43
<span class="line-number">44
<span class="line-number">45
<span class="line-number">46
<span class="line-number">47
<span class="line-number">48
<span class="line-number">49
<span class="line-number">50
<span class="line-number">51
<span class="line-number">52
<span class="line-number">53
<span class="line-number">54
<span class="line-number">55
</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
登录后复制
<code class="php"><span class="line"><span class="o"><?<span class="nx">php
<span class="line">
<span class="line"><span class="cm">/*
<span class="line"><span class="cm">|--------------------------------------------------------------------------
<span class="line"><span class="cm">| Create The Application
<span class="line"><span class="cm">|--------------------------------------------------------------------------
<span class="line"><span class="cm">|
<span class="line"><span class="cm">| The first thing we will do is create a new Laravel application instance
<span class="line"><span class="cm">| which serves as the "glue" for all the components of Laravel, and is
<span class="line"><span class="cm">| the IoC container for the system binding all of the various parts.
<span class="line"><span class="cm">|
<span class="line"><span class="cm">*/
<span class="line">
<span class="line"><span class="nv">$app <span class="o">= <span class="k">new <span class="nx">Illuminate\Foundation\Application<span class="p">(
<span class="line">    <span class="nb">realpath<span class="p">(<span class="nx">__DIR__<span class="o">.<span class="s1">'/../'<span class="p">)
<span class="line"><span class="p">);
<span class="line">
<span class="line"><span class="cm">/*
<span class="line"><span class="cm">|--------------------------------------------------------------------------
<span class="line"><span class="cm">| Bind Important Interfaces
<span class="line"><span class="cm">|--------------------------------------------------------------------------
<span class="line"><span class="cm">|
<span class="line"><span class="cm">| Next, we need to bind some important interfaces into the container so
<span class="line"><span class="cm">| we will be able to resolve them when needed. The kernels serve the
<span class="line"><span class="cm">| incoming requests to this application from both the web and CLI.
<span class="line"><span class="cm">|
<span class="line"><span class="cm">*/
<span class="line">
<span class="line"><span class="nv">$app<span class="o">-><span class="na">singleton<span class="p">(
<span class="line">    <span class="nx">Illuminate\Contracts\Http\Kernel<span class="o">::<span class="na">class<span class="p">,
<span class="line">    <span class="nx">App\Http\Kernel<span class="o">::<span class="na">class
<span class="line"><span class="p">);
<span class="line">
<span class="line"><span class="nv">$app<span class="o">-><span class="na">singleton<span class="p">(
<span class="line">    <span class="nx">Illuminate\Contracts\Console\Kernel<span class="o">::<span class="na">class<span class="p">,
<span class="line">    <span class="nx">App\Console\Kernel<span class="o">::<span class="na">class
<span class="line"><span class="p">);
<span class="line">
<span class="line"><span class="nv">$app<span class="o">-><span class="na">singleton<span class="p">(
<span class="line">    <span class="nx">Illuminate\Contracts\Debug\ExceptionHandler<span class="o">::<span class="na">class<span class="p">,
<span class="line">    <span class="nx">App\Exceptions\Handler<span class="o">::<span class="na">class
<span class="line"><span class="p">);
<span class="line">
<span class="line"><span class="cm">/*
<span class="line"><span class="cm">|--------------------------------------------------------------------------
<span class="line"><span class="cm">| Return The Application
<span class="line"><span class="cm">|--------------------------------------------------------------------------
<span class="line"><span class="cm">|
<span class="line"><span class="cm">| This script returns the application instance. The instance is given to
<span class="line"><span class="cm">| the calling script so we can separate the building of the instances
<span class="line"><span class="cm">| from the actual running of the application and sending responses.
<span class="line"><span class="cm">|
<span class="line"><span class="cm">*/
<span class="line">
<span class="line"><span class="k">return <span class="nv">$app<span class="p">;
</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>
登录后复制

看第十四行,原来$app是一个 Illuminate\Foundation\Application 对象,那么在创 建这个对象的时候又发生了什么呢?

从它的构造方法看起:

Illuminate\Foundation\Application 构造方法

<span class="line-number">1
<span class="line-number">2
<span class="line-number">3
<span class="line-number">4
<span class="line-number">5
<span class="line-number">6
<span class="line-number">7
<span class="line-number">8
<span class="line-number">9
<span class="line-number">10
<span class="line-number">11
<span class="line-number">12
<span class="line-number">13
<span class="line-number">14
<span class="line-number">15
<span class="line-number">16
<span class="line-number">17
<span class="line-number">18
</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
登录后复制
<code class="php"><span class="line"><span class="sd">/**
<span class="line"><span class="sd"> * Create a new Illuminate application instance.
<span class="line"><span class="sd"> *
<span class="line"><span class="sd"> * @param  string|null  $basePath
<span class="line"><span class="sd"> * @return void
<span class="line"><span class="sd"> */
<span class="line"><span class="k">public <span class="k">function <span class="nf">__construct<span class="p">(<span class="nv">$basePath <span class="o">= <span class="k">null<span class="p">)
<span class="line"><span class="p">{
<span class="line">    <span class="nv">$this<span class="o">-><span class="na">registerBaseBindings<span class="p">();
<span class="line">
<span class="line">    <span class="nv">$this<span class="o">-><span class="na">registerBaseServiceProviders<span class="p">();
<span class="line">
<span class="line">    <span class="nv">$this<span class="o">-><span class="na">registerCoreContainerAliases<span class="p">();
<span class="line">
<span class="line">    <span class="k">if <span class="p">(<span class="nv">$basePath<span class="p">) <span class="p">{
<span class="line">        <span class="nv">$this<span class="o">-><span class="na">setBasePath<span class="p">(<span class="nv">$basePath<span class="p">);
<span class="line">    <span class="p">}
<span class="line"><span class="p">}
</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>
登录后复制

顺着函数调用,往下看。在这个构造函数中,首先调用了registerBaseBindings方法。

Illuminate\Foundation\Application#registerBaseBindings

<span class="line-number">1
<span class="line-number">2
<span class="line-number">3
<span class="line-number">4
<span class="line-number">5
<span class="line-number">6
<span class="line-number">7
<span class="line-number">8
<span class="line-number">9
<span class="line-number">10
<span class="line-number">11
<span class="line-number">12
<span class="line-number">13
</span></span></span></span></span></span></span></span></span></span></span></span></span>
登录后复制
<code class="php"><span class="line"><span class="sd">/**
<span class="line"><span class="sd">  * Register the basic bindings into the container.
<span class="line"><span class="sd">  *
<span class="line"><span class="sd">  * @return void
<span class="line"><span class="sd">  */
<span class="line"><span class="k">protected <span class="k">function <span class="nf">registerBaseBindings<span class="p">()
<span class="line"><span class="p">{
<span class="line">    <span class="k">static<span class="o">::<span class="na">setInstance<span class="p">(<span class="nv">$this<span class="p">);
<span class="line">
<span class="line">    <span class="nv">$this<span class="o">-><span class="na">instance<span class="p">(<span class="s1">'app'<span class="p">, <span class="nv">$this<span class="p">);
<span class="line">
<span class="line">    <span class="nv">$this<span class="o">-><span class="na">instance<span class="p">(<span class="s1">'Illuminate\Container\Container'<span class="p">, <span class="nv">$this<span class="p">);
<span class="line"><span class="p">}
</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>
登录后复制

这段代码,是将实例对象注入到容器中。那么,这个容器是什么呢?答案还是要从这段调用 中去寻找。

static::setInstance($this) 所做的就是将 $this 赋值给自身的 instance 静态变 量。重点看 $this->instance('app', $this)

instance 函数的作用是绑定一个已有对象到容器中,这个对象在容器中共享并且可以通 过键获取。

Illuminate\Container\Container#instance

<span class="line-number">1
<span class="line-number">2
<span class="line-number">3
<span class="line-number">4
<span class="line-number">5
<span class="line-number">6
<span class="line-number">7
<span class="line-number">8
<span class="line-number">9
<span class="line-number">10
<span class="line-number">11
<span class="line-number">12
<span class="line-number">13
<span class="line-number">14
<span class="line-number">15
<span class="line-number">16
<span class="line-number">17
<span class="line-number">18
<span class="line-number">19
<span class="line-number">20
<span class="line-number">21
<span class="line-number">22
<span class="line-number">23
<span class="line-number">24
<span class="line-number">25
<span class="line-number">26
<span class="line-number">27
<span class="line-number">28
<span class="line-number">29
</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
登录后复制
<code class="php"><span class="line"><span class="sd">/**
<span class="line"><span class="sd">  * Register an existing instance as shared in the container.
<span class="line"><span class="sd">  *
<span class="line"><span class="sd">  * @param  string  $abstract
<span class="line"><span class="sd">  * @param  mixed   $instance
<span class="line"><span class="sd">  * @return void
<span class="line"><span class="sd">  */
<span class="line"><span class="k">public <span class="k">function <span class="nf">instance<span class="p">(<span class="nv">$abstract<span class="p">, <span class="nv">$instance<span class="p">)
<span class="line"><span class="p">{
<span class="line">    <span class="k">if <span class="p">(<span class="nb">is_array<span class="p">(<span class="nv">$abstract<span class="p">)) <span class="p">{
<span class="line">        <span class="c1">// $abstract 是这样的一个数组 ['actual key' => 'alias']
<span class="line">        <span class="k">list<span class="p">(<span class="nv">$abstract<span class="p">, <span class="nv">$alias<span class="p">) <span class="o">= <span class="nv">$this<span class="o">-><span class="na">extractAlias<span class="p">(<span class="nv">$abstract<span class="p">);
<span class="line">
<span class="line">        <span class="c1">// 实际上的行为是 $this->aliases[$alias] = $abstract;
<span class="line">        <span class="nv">$this<span class="o">-><span class="na">alias<span class="p">(<span class="nv">$abstract<span class="p">, <span class="nv">$alias<span class="p">);
<span class="line">    <span class="p">}
<span class="line">
<span class="line">    <span class="nb">unset<span class="p">(<span class="nv">$this<span class="o">-><span class="na">aliases<span class="p">[<span class="nv">$abstract<span class="p">]);
<span class="line">
<span class="line">    <span class="c1">// 检查是否有这个键是否已经注册到容器中
<span class="line">    <span class="c1">// $bound 是一个boolean值
<span class="line">    <span class="nv">$bound <span class="o">= <span class="nv">$this<span class="o">-><span class="na">bound<span class="p">(<span class="nv">$abstract<span class="p">);
<span class="line">
<span class="line">    <span class="nv">$this<span class="o">-><span class="na">instances<span class="p">[<span class="nv">$abstract<span class="p">] <span class="o">= <span class="nv">$instance<span class="p">;
<span class="line">
<span class="line">    <span class="k">if <span class="p">(<span class="nv">$bound<span class="p">) <span class="p">{
<span class="line">        <span class="nv">$this<span class="o">-><span class="na">rebound<span class="p">(<span class="nv">$abstract<span class="p">);
<span class="line">    <span class="p">}
<span class="line"><span class="p">}
</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>
登录后复制

视线重新回到Application类中,接下来调用了这个方法 $this->registerBaseServiceProviders()

Illuminate\Foundation\Application#registerBaseServiceProviders

<span class="line-number">1
<span class="line-number">2
<span class="line-number">3
<span class="line-number">4
<span class="line-number">5
<span class="line-number">6
<span class="line-number">7
<span class="line-number">8
<span class="line-number">9
<span class="line-number">10
<span class="line-number">11
<span class="line-number">12
<span class="line-number">13
<span class="line-number">14
<span class="line-number">15
<span class="line-number">16
<span class="line-number">17
<span class="line-number">18
<span class="line-number">19
<span class="line-number">20
<span class="line-number">21
<span class="line-number">22
<span class="line-number">23
<span class="line-number">24
<span class="line-number">25
<span class="line-number">26
<span class="line-number">27
<span class="line-number">28
<span class="line-number">29
<span class="line-number">30
<span class="line-number">31
<span class="line-number">32
<span class="line-number">33
<span class="line-number">34
<span class="line-number">35
<span class="line-number">36
<span class="line-number">37
<span class="line-number">38
<span class="line-number">39
<span class="line-number">40
<span class="line-number">41
<span class="line-number">42
<span class="line-number">43
<span class="line-number">44
<span class="line-number">45
<span class="line-number">46
<span class="line-number">47
<span class="line-number">48
<span class="line-number">49
<span class="line-number">50
<span class="line-number">51
<span class="line-number">52
<span class="line-number">53
</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
登录后复制
<code class="php"><span class="line"><span class="sd">/**
<span class="line"><span class="sd">  * Register all of the base service providers.
<span class="line"><span class="sd">  *
<span class="line"><span class="sd">  * @return void
<span class="line"><span class="sd">  */
<span class="line"><span class="k">protected <span class="k">function <span class="nf">registerBaseServiceProviders<span class="p">()
<span class="line"><span class="p">{
<span class="line">    <span class="nv">$this<span class="o">-><span class="na">register<span class="p">(<span class="k">new <span class="nx">EventServiceProvider<span class="p">(<span class="nv">$this<span class="p">));
<span class="line">
<span class="line">    <span class="nv">$this<span class="o">-><span class="na">register<span class="p">(<span class="k">new <span class="nx">RoutingServiceProvider<span class="p">(<span class="nv">$this<span class="p">));
<span class="line"><span class="p">}
<span class="line">
<span class="line"><span class="sd">/**
<span class="line"><span class="sd">  * Register a service provider with the application.
<span class="line"><span class="sd">  *
<span class="line"><span class="sd">  * @param  \Illuminate\Support\ServiceProvider|string  $provider
<span class="line"><span class="sd">  * @param  array  $options
<span class="line"><span class="sd">  * @param  bool   $force
<span class="line"><span class="sd">  * @return \Illuminate\Support\ServiceProvider
<span class="line"><span class="sd">  */
<span class="line"><span class="k">public <span class="k">function <span class="nf">register<span class="p">(<span class="nv">$provider<span class="p">, <span class="nv">$options <span class="o">= <span class="p">[], <span class="nv">$force <span class="o">= <span class="k">false<span class="p">)
<span class="line"><span class="p">{
<span class="line">    <span class="k">if <span class="p">(<span class="nv">$registered <span class="o">= <span class="nv">$this<span class="o">-><span class="na">getProvider<span class="p">(<span class="nv">$provider<span class="p">) <span class="o">&& <span class="o">!<span class="nv">$force<span class="p">) <span class="p">{
<span class="line">        <span class="k">return <span class="nv">$registered<span class="p">;
<span class="line">    <span class="p">}
<span class="line">
<span class="line">    <span class="c1">// If the given "provider" is a string, we will resolve it, passing in the
<span class="line">    <span class="c1">// application instance automatically for the developer. This is simply
<span class="line">    <span class="c1">// a more convenient way of specifying your service provider classes.
<span class="line">    <span class="k">if <span class="p">(<span class="nb">is_string<span class="p">(<span class="nv">$provider<span class="p">)) <span class="p">{
<span class="line">        <span class="nv">$provider <span class="o">= <span class="nv">$this<span class="o">-><span class="na">resolveProviderClass<span class="p">(<span class="nv">$provider<span class="p">);
<span class="line">    <span class="p">}
<span class="line">
<span class="line">    <span class="nv">$provider<span class="o">-><span class="na">register<span class="p">();
<span class="line">
<span class="line">    <span class="c1">// Once we have registered the service we will iterate through the options
<span class="line">    <span class="c1">// and set each of them on the application so they will be available on
<span class="line">    <span class="c1">// the actual loading of the service objects and for developer usage.
<span class="line">    <span class="k">foreach <span class="p">(<span class="nv">$options <span class="k">as <span class="nv">$key <span class="o">=> <span class="nv">$value<span class="p">) <span class="p">{
<span class="line">        <span class="nv">$this<span class="p">[<span class="nv">$key<span class="p">] <span class="o">= <span class="nv">$value<span class="p">;
<span class="line">    <span class="p">}
<span class="line">
<span class="line">    <span class="nv">$this<span class="o">-><span class="na">markAsRegistered<span class="p">(<span class="nv">$provider<span class="p">);
<span class="line">
<span class="line">    <span class="c1">// If the application has already booted, we will call this boot method on
<span class="line">    <span class="c1">// the provider class so it has an opportunity to do its boot logic and
<span class="line">    <span class="c1">// will be ready for any usage by the developer's application logics.
<span class="line">    <span class="k">if <span class="p">(<span class="nv">$this<span class="o">-><span class="na">booted<span class="p">) <span class="p">{
<span class="line">        <span class="nv">$this<span class="o">-><span class="na">bootProvider<span class="p">(<span class="nv">$provider<span class="p">);
<span class="line">    <span class="p">}
<span class="line">
<span class="line">    <span class="k">return <span class="nv">$provider<span class="p">;
<span class="line"><span class="p">}
</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>
登录后复制

其中,EventServiceProvider和RoutingServiceProvider分别是

  • Illuminate\Events\EventServiceProvider
  • Illuminate\Routing\RoutingServiceProvider

这些ServiceProvider是 Illuminate\Support\ServiceProvider 的子类,它接受一个 Application 对象作为构造函数参数,存储在实例变量 $app 中。

注入所有基础 Service Provider

register 方法中,每个ServiceProvider被调用了自身的 register 方法。首先看 看 EventServiceProvider 中的吧。

Illuminate\Events\EventServiceProvider#register

<span class="line-number">1
<span class="line-number">2
<span class="line-number">3
<span class="line-number">4
<span class="line-number">5
<span class="line-number">6
<span class="line-number">7
<span class="line-number">8
</span></span></span></span></span></span></span></span>
登录后复制
<code class="php"><span class="line"><span class="k">public <span class="k">function <span class="nf">register<span class="p">()
<span class="line"><span class="p">{
<span class="line">    <span class="nv">$this<span class="o">-><span class="na">app<span class="o">-><span class="na">singleton<span class="p">(<span class="s1">'events'<span class="p">, <span class="k">function <span class="p">(<span class="nv">$app<span class="p">) <span class="p">{
<span class="line">        <span class="k">return <span class="p">(<span class="k">new <span class="nx">Dispatcher<span class="p">(<span class="nv">$app<span class="p">))<span class="o">-><span class="na">setQueueResolver<span class="p">(<span class="k">function <span class="p">() <span class="k">use <span class="p">(<span class="nv">$app<span class="p">) <span class="p">{
<span class="line">            <span class="k">return <span class="nv">$app<span class="o">-><span class="na">make<span class="p">(<span class="s1">'Illuminate\Contracts\Queue\Factory'<span class="p">);
<span class="line">        <span class="p">});
<span class="line">    <span class="p">});
<span class="line"><span class="p">}
</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>
登录后复制

上面方法体将一个 Illuminate\Events\Dispatcher 对象以键 events 绑定到了容器 中,它负责实现事件的调度。

再看看 Illuminate\Routing\RoutingServiceProvider:

Illuminate\Routing\RoutingServiceProvider#register

<span class="line-number">1
<span class="line-number">2
<span class="line-number">3
<span class="line-number">4
<span class="line-number">5
<span class="line-number">6
<span class="line-number">7
<span class="line-number">8
<span class="line-number">9
<span class="line-number">10
<span class="line-number">11
<span class="line-number">12
<span class="line-number">13
<span class="line-number">14
</span></span></span></span></span></span></span></span></span></span></span></span></span></span>
登录后复制
<code class="php"><span class="line"><span class="k">public <span class="k">function <span class="nf">register<span class="p">()
<span class="line"><span class="p">{
<span class="line">    <span class="nv">$this<span class="o">-><span class="na">registerRouter<span class="p">();
<span class="line">
<span class="line">    <span class="nv">$this<span class="o">-><span class="na">registerUrlGenerator<span class="p">();
<span class="line">
<span class="line">    <span class="nv">$this<span class="o">-><span class="na">registerRedirector<span class="p">();
<span class="line">
<span class="line">    <span class="nv">$this<span class="o">-><span class="na">registerPsrRequest<span class="p">();
<span class="line">
<span class="line">    <span class="nv">$this<span class="o">-><span class="na">registerPsrResponse<span class="p">();
<span class="line">
<span class="line">    <span class="nv">$this<span class="o">-><span class="na">registerResponseFactory<span class="p">();
<span class="line"><span class="p">}
</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>
登录后复制

首页是在Laravel中接触的最多的 route 被注册,它是 Illuminate\Routing\Router 对象。

 

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

其他好文

http://www.cnblogs.com/wish123/p/4756669.html

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1071501.htmlTechArticlelaravel5源码讲解整理,laravel5源码讲解 来源:http://yuez.me/laravel-yuan-ma-jie-du/?utm_source=tuicoolutm_medium=referral 目录 入口文件 index.php Illuminate\Fou...
相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板