Laravel5 source code explanation and arrangement, laravel5 source code explanation_PHP tutorial

WBOY
Release: 2016-07-12 09:04:51
Original
1397 people have browsed it

Laravel5 source code explanation and arrangement, laravel5 source code explanation

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

Table of Contents

  • Entry file index.php
    • IlluminateFoundationApplication class
    • Inject all basic Service Providers

Entry file index.php

For an application based on Laravel, when the WEB server receives an external request, it will parse the request to public/index.php in the application root directory.

Laravel source code interpretation-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>
Copy after login
<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>
Copy after login

Line 21 of code

<span class="line-number">1
</span>
Copy after login
Copy after login
<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>
Copy after login

Introduces the class loader provided by Composer for Laravel applications, so that Laravel applications no longer need to manually load any classes. Its loading principle is not the goal of this study, so just use it this way. The following code is the focus.

IlluminateFoundationApplication class

The inheritance structure of this class is as follows:

Laravel5 source code explanation and arrangement, laravel5 source code explanation_PHP tutorial

Line 35 of code

<span class="line-number">1
</span>
Copy after login
Copy after login
<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>
Copy after login

It draws my attention to another file to see what is going on.

Laravel source code interpretation-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>
Copy after login
<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>
Copy after login

Look at line 14. It turns out that $app is a IlluminateFoundationApplication object, so what happened when this object was created?

From its construction method:

IlluminateFoundationApplication constructor

<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>
Copy after login
<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>
Copy after login

Follow the function calls and look down. In this constructor, the registerBaseBindings method is called first.

IlluminateFoundationApplication#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>
Copy after login
<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>
Copy after login

This code injects the instance object into the container. So, what is this container? The answer still needs to be found from this call.

What

static::setInstance($this) does is assign $this to its own instance static variable. Focus on $this->instance('app', $this).

The function of

instance is to bind an existing object to the container. This object is shared in the container and can be obtained by key.

IlluminateContainerContainer#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>
Copy after login
<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>
Copy after login

The line of sight returned to the Application class, and then this method was called $this->registerBaseServiceProviders(),

IlluminateFoundationApplication#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>
Copy after login
<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>
Copy after login

Among them, EventServiceProvider and RoutingServiceProvider are respectively

  • IlluminateEventsEventServiceProvider
  • IlluminateRoutingRoutingServiceProvider

These ServiceProviders are subclasses of IlluminateSupportServiceProvider , which accept a Application object as a constructor parameter, which is stored in the instance variable $app .

Inject all basic Service Providers

In the register method, each ServiceProvider is called its own register method. Let’s take a look at EventServiceProvider first.

IlluminateEventsEventServiceProvider#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>
Copy after login
<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>
Copy after login

The above method body binds a IlluminateEventsDispatcher object to the container with the key events, which is responsible for event scheduling.

Look again IlluminateRoutingRoutingServiceProvider:

IlluminateRoutingRoutingServiceProvider#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>
Copy after login
<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>
Copy after login

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

 

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

其他好文

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 IlluminateFou...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template