在bootstrap/app.php

  1. /**
  2. * 对于其中的instance register singleton 方法到时候单独拎出来说明
  3. *
  4. * 1.设置基础路径
  5. * 2.使用instance 方法 绑定app 和Illuminate\Foundation\Application类的关系
  6. * 3.使用instance 方法 绑定Container 和Illuminate\Foundation\Application类的关系
  7. * 4.app变量中注册事件服务EventServiceProvider
  8. * 5.app变量中注册日志服务LogServiceProvider
  9. * 6.app变量中注册路由服务RoutingServiceProvider
  10. * 7.别名的注册(vendor/laravel/framework/src/Illuminate/Foundation/Application.php文件中的 registerCoreContainerAliases 方法)
  11. */
  12. $app = new Illuminate\Foundation\Application(
  13. realpath(__DIR__.'/../')
  14. );

实例化 vendor/laravel/framework/src/Illuminate/Foundation/Application.php类  该类的魔术方法

  1. public function __construct($basePath = null)
  2. {
  3. /**
  4. * 如果有传地址 设置基础路径 设置
  5. * path $this->path()
  6. * path.base $this->basePath()
  7. * path.lang $this->langPath()
  8. * path.config $this->configPath()
  9. * path.public $this->publicPath()
  10. * path.storage $this->storagePath()
  11. * path.database $this->databasePath()
  12. * path.resources $this->resourcePath()
  13. * path.bootstrap $this->bootstrapPath()
  14. */
  15. if ($basePath) {
  16. $this->setBasePath($basePath);
  17. }
  18.  
  19. /**
  20. * 注册 app 和container到 instances数组中
  21. */
  22. $this->registerBaseBindings();
  23.  
  24. /**
  25. * 注册EventServiceProvider LogServiceProvider RoutingServiceProvider
  26. */
  27. $this->registerBaseServiceProviders();
  28.  
  29. /**
  30. * 设置核心类的别名
  31. */
  32. $this->registerCoreContainerAliases();
  33. }

查看注册 app 和container到 instances数组中

  1. protected function registerBaseBindings()
  2. {
  3. static::setInstance($this);
  4.  
  5. /**
  6. * 由于初始化中 app没有添加到instances数组中 所以 不会执行build函数 只做了 instances数组中记录了app和$this
  7. */
  8. $this->instance('app', $this);
  9.  
  10. /**
  11. * 由于初始化中 Container没有添加到instances数组中 所以 不会执行build函数 只做了 instances数组中记录了app和$this
  12. */
  13. $this->instance(Container::class, $this);
  14. }

查看今天主要的方法 instance

流程图

  1. public function instance($abstract, $instance)
  2. {
  3.  
  4. /**
  5. * 如果aliases 数组总存在 则游离abstractAliases 数组 删除其中的存在的值
  6. */
  7. $this->removeAbstractAlias($abstract);
  8.  
  9. /**
  10. * 判断 在bindings aliases instances其中有一个存现 则返回true
  11. */
  12. $isBound = $this->bound($abstract);
  13.  
  14. /**
  15. * 删除别名数组中对于的建
  16. */
  17. unset($this->aliases[$abstract]);
  18.  
  19. // We'll check to determine if this type has been bound before, and if it has
  20. // we will fire the rebound callbacks registered with the container and it
  21. // can be updated with consuming classes that have gotten resolved here.
  22. /**
  23. * 在instances 数组中添加对于的建
  24. */
  25. $this->instances[$abstract] = $instance;
  26.  
  27. /**
  28. * 如果之前存在实例化 则运行
  29. */
  30. if ($isBound) {
  31. $this->rebound($abstract);
  32. }
  33. }

第一个方法  removeAbstractAlias

  1. /**
  2. * Remove an alias from the contextual binding alias cache.
  3. *
  4. * @param string $searched
  5. * @return void
  6. */
  7. protected function removeAbstractAlias($searched)
  8. {
  9. /**
  10. * 如果在别名数组中不存在则直接返回
  11. */
  12. if (! isset($this->aliases[$searched])) {
  13. return;
  14. }
  15.  
  16. /**
  17. * 游离抽象别名 吧存在抽象类别名数组中的存在的删除
  18. * @example abstractAliases = ['nameabstract'=>['aliase1','aliases2']]
  19. */
  20. foreach ($this->abstractAliases as $abstract => $aliases) {
  21. foreach ($aliases as $index => $alias) {
  22. if ($alias == $searched) {
  23. unset($this->abstractAliases[$abstract][$index]);
  24. }
  25. }
  26. }
  27. }

第二个方法:  bound

  1. /**
  2. * Determine if the given abstract type has been bound.
  3. *
  4. * @param string $abstract
  5. * @return bool
  6. */
  7. public function bound($abstract)
  8. {
  9. return isset($this->bindings[$abstract]) ||
  10. isset($this->instances[$abstract]) ||
  11. $this->isAlias($abstract);
  12. }

第三个方法: rebound

  1. /**
  2. * Fire the "rebound" callbacks for the given abstract type.
  3. *
  4. * @param string $abstract
  5. * @return void
  6. */
  7. protected function rebound($abstract)
  8. {
  9. /**
  10. * 主要实现的功能为 build 方法 实例化制定的类 并且返回该类
  11. */
  12. $instance = $this->make($abstract);
  13.  
  14. /**
  15. * 查看reboundCallbacks 数组中是否存在该别名创建完成之后需要调用的方法数组
  16. * 存在返回需要调用的方法数组 并且逐个执行
  17. */
  18. foreach ($this->getReboundCallbacks($abstract) as $callback) {
  19. call_user_func($callback, $this, $instance);
  20. }
  21. }

Laravel (5.5.33) 加载过程---instance方法(二)的更多相关文章

  1. Laravel (5.5.33) 加载过程---make方法(四)

    /** * Resolve the given type from the container. * * @param string $abstract * @return mixed */ publ ...

  2. Laravel (5.5.33) 加载过程(一)

    说明:  由于公司项目使用Laravel 框架  也是第一次接触此框架  作为一个新手 记录使用过程的一些事情  以及对于框架源码分析的记录  整理自己的思路 也希望对大家有帮助  如果那里不对的地方 ...

  3. Laravel (5.5.33) 加载过程(二)

    本次说明代码 /* |-------------------------------------------------------------------------- | Turn On The ...

  4. Spring IOC bean加载过程

    首先我们不要在学习Spring的开始产生畏难情绪.Spring没有臆想的那么高深,相反,它帮我们再项目开发中制定项目框架,简化项目开发.它的主要功能是将项目开发中繁琐的过程流程化,模式化,使用户仅在固 ...

  5. mybatis的sqlSessionFactory的加载过程

    使用过SSM的框架的都知道mybatis这个持久层框架,今天小编就来简单说说这个框架的核心工厂类sqlSessionFactory的加载过程,一般的SSM框架我们都会在spring的applicati ...

  6. Dubbo源码分析之ExtensionLoader加载过程解析

    ExtensionLoader加载机制阅读: Dubbo的类加载机制是模仿jdk的spi加载机制:  Jdk的SPI扩展加载机制:约定是当服务的提供者每增加一个接口的实现类时,需要在jar包的META ...

  7. 重温.NET下Assembly的加载过程 ASP.NET Core Web API下事件驱动型架构的实现(三):基于RabbitMQ的事件总线

    重温.NET下Assembly的加载过程   最近在工作中牵涉到了.NET下的一个古老的问题:Assembly的加载过程.虽然网上有很多文章介绍这部分内容,很多文章也是很久以前就已经出现了,但阅读之后 ...

  8. Tomcat源码分析三:Tomcat启动加载过程(一)的源码解析

    Tomcat启动加载过程(一)的源码解析 今天,我将分享用源码的方式讲解Tomcat启动的加载过程,关于Tomcat的架构请参阅<Tomcat源码分析二:先看看Tomcat的整体架构>一文 ...

  9. Dubbo源码解析之SPI(一):扩展类的加载过程

    Dubbo是一款开源的.高性能且轻量级的Java RPC框架,它提供了三大核心能力:面向接口的远程方法调用.智能容错和负载均衡,以及服务自动注册和发现. Dubbo最早是阿里公司内部的RPC框架,于 ...

随机推荐

  1. UEP-confirm和alert弹窗

    function stuDel(){ var ds = ajaxgrid.getCheckedRecords(); if(ds.length==0){ $.alert("提示信息" ...

  2. [国嵌攻略][156][I2C自编设备驱动设计]

    AT24C08的驱动在Linux内核中已经提供,在/drivers/misc/eeprom/at24.c文件中.在对应的probe函数中有一个创建/sys/.../eeprom文件的函数,应用程序通过 ...

  3. 获取屏幕宽高度与可视区域宽高度(availWidth、clientWidth、width、innerWidth)

    经常会遇到需要获取屏幕宽度.高度,可视区域宽度.高度等问题,也就常跟这几个打交道,一不小心,还真爱弄混淆了. 先来列举下这几个吧: screen.availHeight.screen.availWid ...

  4. 关于jquery ajax跨域请求获取response headers问题

    背景:最近项目jwt用户认证方式,关于jwt本人就不再赘述,大家可自行百度. jwt token基本流程是这样的: 用户使用用户名密码来请求服务器 服务器进行验证用户的信息 服务器通过验证发送给用户一 ...

  5. php中urldecode()和urlencode()起什么作用啊

    urlencode()函数原理就是首先把中文字符转换为十六进制,然后在每个字符前面加一个标识符%. urldecode()函数与urlencode()函数原理相反,用于解码已编码的 URL 字符串,其 ...

  6. putty 与winscp 区别

    https://zhidao.baidu.com/question/377968180.html putty 与winscp 有什么区别, 装了 winscp 可以由 putty 替换么 ? 具体用法 ...

  7. Java进阶篇(六)——Swing程序设计(上)

    Swing是GUI(图形用户界面)开发工具包,内容有很多,这里会分块编写,但在进阶篇中只编写Swing中的基本要素,包括容器.组件和布局等,更深入的内容会在高级篇中出现.想深入学习的朋友们可查阅有关资 ...

  8. Mysql索引分析:适合建索引?不适合建索引?【转】

    数据库建立索引常用的规则如下: 1.表的主键.外键必须有索引: 2.数据量超过300的表应该有索引: 3.经常与其他表进行连接的表,在连接字段上应该建立索引: 4.经常出现在Where子句中的字段,特 ...

  9. Web前端学习(4):显示图片、url与文件路径

    本章主旨 介绍<img>标签及其基本属性:介绍URL和文件路径 在上一章中,我简单地介绍了HTML的一些基本标签及基本属性,例如,我们用<p>标签来标记文本段落,用<h1 ...

  10. 《UNIX实用教程》读书笔记

    原著:<Just Enough UNIX>  Fifth Edition  [美]Paul K.Andersen 译著:<UNIX实用教程> 第5版 宋虹 曾庆冬 段桂华 杨路 ...