laravel5 ソースコードのチュートリアルとコンパイル

WBOY
リリース: 2016-06-13 12:27:34
オリジナル
828 人が閲覧しました

Laravel5 ソースコードの説明とコンパイル

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

ディレクトリ

  • エントリ ファイルindex.php
    • IlluminateFoundationApplication クラス
    • すべての基本的なサービス プロバイダーを注入します

エントリfileindex .php

Laravelベースのアプリケーションでは、WEBサーバーが外部からリクエストを受信すると、アプリケーションのルートディレクトリにあるpublic/index.phpへのリクエストを解析します。

Laravel ソースコード解釈-index.php (laravel_index.php)ダウンロード

<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 <[email&#160;protected]><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>
ログイン後にコピー

コードの 21 行目

<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>
ログイン後にコピー

は、Composer によって提供されるクラスローダーを Laravel アプリケーションに導入するため、Laravel アプリケーションはクラスを手動でロードする必要がなくなりました。そのローディング原理はこの研究の目的ではないので、単にこの方法で使用してください。次のコードが焦点です。

IlluminateFoundationApplication クラス

このクラスの継承構造は次のとおりです:

laravel5 ソースコードのチュートリアルとコンパイル

コードの 35 行目

<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>
ログイン後にコピー

14 行目を見ると、$appIlluminateFoundationApplication オブジェクト では、このオブジェクトが作成されたときに何が起こったのでしょうか?

構築メソッドから見る:

IlluminateFoundationApplication 構築メソッド

<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 メソッドが呼び出されます。

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>
ログイン後にコピー
<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 既存のオブジェクトをコンテナにバインドする機能です。このオブジェクトはコンテナ内で共有されており、キーによって取得できます。

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>
ログイン後にコピー
<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()

< と呼ばれました🎜> 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>
ログイン後にコピー
<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 はそれぞれ

  • IlluminateEventsEventServiceProvider
  • IlluminateRoutingRoutingServiceProvider
これらの ServiceProvider は

のサブクラスであり、IlluminateSupportServiceProvider オブジェクトをコンストラクター パラメーターとして受け入れ、インスタンス変数 Application に格納されます。 $app

すべての基本的なサービス プロバイダーを挿入します

メソッドでは、各 ServiceProvider は独自の register メソッドと呼ばれます。まずはregisterを見てみましょう。 EventServiceProvider

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>
ログイン後にコピー
<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>
ログイン後にコピー
上記のメソッド本体は、キー

を使用して IlluminateEventsDispatcher オブジェクトをコンテナーにバインドします。これはイベントのスケジュールを担当します。 。 events

もう一度見てください

:IlluminateRoutingRoutingServiceProvider

IlluminateRoutingRoutingServiceProvider#register

の先頭は、Laravel 内で接触する最も多くの route に登録されており、IlluminateRoutingRouter の対象です。

> ;>>>>>>>>>>>>>>>>>>>>>>>>> ;>>>>>>>>>>>>>>>>>>>>>>>>> ;>>

その他の良い文

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

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

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート