Laravel (5.5.33) 載入過程---instance方法(三)

来源:https://www.cnblogs.com/Sunlight1992/archive/2018/02/17/8451932.html
-Advertisement-
Play Games

在bootstrap/app.php 實例化 vendor/laravel/framework/src/Illuminate/Foundation/Application.php類 該類的魔術方法 查看註冊 app 和container到 instances數組中 查看今天主要的方法 instanc ...


在bootstrap/app.php

/**
 * 對於其中的instance register singleton 方法到時候單獨拎出來說明
 * 
 * 1.設置基礎路徑
 * 2.使用instance 方法 綁定app 和Illuminate\Foundation\Application類的關係
 * 3.使用instance 方法 綁定Container 和Illuminate\Foundation\Application類的關係
 * 4.app變數中註冊事件服務EventServiceProvider
 * 5.app變數中註冊日誌服務LogServiceProvider
 * 6.app變數中註冊路由服務RoutingServiceProvider
 * 7.別名的註冊(vendor/laravel/framework/src/Illuminate/Foundation/Application.php文件中的 registerCoreContainerAliases 方法)
 */
$app = new Illuminate\Foundation\Application(
    realpath(__DIR__.'/../')
);

實例化 vendor/laravel/framework/src/Illuminate/Foundation/Application.php類  該類的魔術方法

    public function __construct($basePath = null)
    {
        /**
         * 如果有傳地址  設置基礎路徑 設置 
         * path                 $this->path()
         * path.base              $this->basePath()
         * path.lang             $this->langPath()
         * path.config             $this->configPath()
         * path.public            $this->publicPath()
         * path.storage            $this->storagePath()
         * path.database        $this->databasePath()
         * path.resources        $this->resourcePath()
         * path.bootstrap         $this->bootstrapPath()
         */
        if ($basePath) {
            $this->setBasePath($basePath);
        }

        /**
         * 註冊 app 和container到 instances數組中
         */
        $this->registerBaseBindings();

        /**
         * 註冊EventServiceProvider LogServiceProvider  RoutingServiceProvider
         */
        $this->registerBaseServiceProviders();

        /**
         * 設置核心類的別名
         */
        $this->registerCoreContainerAliases();
    }

查看註冊 app 和container到 instances數組中

    protected function registerBaseBindings()
    {
        static::setInstance($this);

        /**
         * 由於初始化中  app沒有添加到instances數組中 所以 不會執行build函數  只做了 instances數組中記錄了app和$this
         */
        $this->instance('app', $this);

        /**
         * 由於初始化中  Container沒有添加到instances數組中 所以 不會執行build函數 只做了 instances數組中記錄了app和$this
         */
        $this->instance(Container::class, $this);
    }

 

 


 

查看今天主要的方法 instance

流程圖

 public function instance($abstract, $instance)
    {

        /**
         * 如果aliases 數組總存在  則游離abstractAliases 數組  刪除其中的存在的值
         */
        $this->removeAbstractAlias($abstract);

        /**
         * 判斷  在bindings aliases instances其中有一個存現  則返回true
         */
        $isBound = $this->bound($abstract);

        /**
         * 刪除別名數組中對於的建
         */
        unset($this->aliases[$abstract]);

        // We'll check to determine if this type has been bound before, and if it has
        // we will fire the rebound callbacks registered with the container and it
        // can be updated with consuming classes that have gotten resolved here.
        /**
         * 在instances 數組中添加對於的建
         */
        $this->instances[$abstract] = $instance;

        /**
         * 如果之前存在實例化 則運行
         */
        if ($isBound) {
            $this->rebound($abstract);
        }
    }

 

第一個方法  removeAbstractAlias

    /**
     * Remove an alias from the contextual binding alias cache.
     *
     * @param  string  $searched
     * @return void
     */
    protected function removeAbstractAlias($searched)
    {
        /**
         * 如果在別名數組中不存在則直接返回
         */
        if (! isset($this->aliases[$searched])) {
            return;
        }

        /**
         * 游離抽象別名 吧存在抽象類別名數組中的存在的刪除
         * @example abstractAliases = ['nameabstract'=>['aliase1','aliases2']]
         */
        foreach ($this->abstractAliases as $abstract => $aliases) {
            foreach ($aliases as $index => $alias) {
                if ($alias == $searched) {
                    unset($this->abstractAliases[$abstract][$index]);
                }
            }
        }
    }

 

第二個方法:  bound

    /**
     * Determine if the given abstract type has been bound.
     *
     * @param  string  $abstract
     * @return bool
     */
    public function bound($abstract)
    {
        return isset($this->bindings[$abstract]) ||
               isset($this->instances[$abstract]) ||
               $this->isAlias($abstract);
    }

 

第三個方法: rebound

 /**
     * Fire the "rebound" callbacks for the given abstract type.
     *
     * @param  string  $abstract
     * @return void
     */
    protected function rebound($abstract)
    {
        /**
         * 主要實現的功能為 build 方法 實例化制定的類 並且返回該類 
         */
        $instance = $this->make($abstract);

        /**
         * 查看reboundCallbacks 數組中是否存在該別名創建完成之後需要調用的方法數組 
         * 存在返回需要調用的方法數組   並且逐個執行
         */
        foreach ($this->getReboundCallbacks($abstract) as $callback) {
            call_user_func($callback, $this, $instance);
        }
    }

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 線程(上) 1.線程含義:一段指令集,也就是一個執行某個程式的代碼。不管你執行的是什麼,代碼量少與多,都會重新翻譯為一段指令集。可以理解為輕量級進程 比如,ipconfig,或者, python XX.py(執行某個py程式),這些都是指令集和,也就是各自都是一個線程。 2.線程的特性: 線程之間可 ...
  • 一、OGNL表達式 1.概述 ​ OGNL是Object-Graph Navigation Language的縮寫,俗稱對象圖導航語言. 它是一種功能強大的表達式語言,通過它簡單一致的表達式語法,可以存取對象的任意屬性,調用對象的方法,遍歷整個對象的結構圖,實現欄位類型轉化等功能。 ​ Eg: hi ...
  • 併發系統可以採用多種併發編程模型來實現。併發模型指定了系統中的線程如何通過協作來完成分配給它們的作業。不同的併發模型採用不同的方式拆分作業,同時線程間的協作和交互方式也不相同。這篇併發模型教程將會較深入地介紹目前(2015年,本文撰寫時間)比較流行的幾種併發模型。 併發模型與分散式系統之間的相似性 ...
  • static_cast 任何具有明確定義的類型轉換,只要不包含底層const,都可以使用static_cast。例如: int i = 3, j = 2; double slope = static_cast<double>(i) / j; static_cast還可以用於把void*轉換成別的類型 ...
  • pycharm是很強大的開發工具,但是每次註冊著實讓人頭疼。網路上很多註冊碼、註冊伺服器等等、但都只是一年或者不能用;為次有如下解決方案。親測有效!!! 如果想讓pycharm永久被激活,比如截止日到2099-12-31;這應該算是永久激活了吧; step1: 下載jar包: 此jar包的目的就是讓 ...
  • 已經鑽DELPHI很深了,當然現在DELPHI是過了最輝煌的時代。但為什麼要繼續下去,而不轉向其它的?這是不是死腦筋? 我看了一下C#的LINQ的產生,然後又被實體框架所代替。思考了一下: 1)LINQ的確是有好處,但是所用的場景又不多,這樣就會變得很雞肋。所以說學新的東西,有時對自己來說不一定有相 ...
  • 一、rest api a、api就是介面 如: - http://www.oldboyedu.com/get_user/ - http://www.oldboyedu.com/get_users/ b、api的兩個用途 1、為別人提供服務 2、前後端分離 二、restful a、--字面意思:表徵狀 ...
  • 引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中國電腦學會(CCF)發起的"電腦職業資格認證"考試,針對電腦軟體開發、軟體測試、信息管理等領域的專業人士進行能力認證。認證對象是從事或將要從事IT領域專業技術與技術管理人 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...