laravel容器初始化registerBaseServiceProviders方法

  • 上篇讲解了laravel容器的基本使用和原理,这篇继续Application构造方法中的registerBaseServiceProviders方法

    1. app调用过registerBaseBindings方法后,打印app实例,发现bindings中存放的确实是闭包,sharedtrue表示单例绑定,instances中表示容器中可以直接复用的实例
    2. Illuminate\Foundation\Application {#2
    3. #basePath: "/home/vagrant/code/test1"
    4. #hasBeenBootstrapped: false
    5. #booted: false
    6. #bootingCallbacks: []
    7. #bootedCallbacks: []
    8. #terminatingCallbacks: []
    9. #serviceProviders: []
    10. #loadedProviders: []
    11. #deferredServices: []
    12. #appPath: null
    13. #databasePath: null
    14. #storagePath: null
    15. #environmentPath: null
    16. #environmentFile: ".env"
    17. #isRunningInConsole: null
    18. #namespace: null
    19. #resolved: []
    20. #bindings: array:1 [▼
    21. "Illuminate\Foundation\Mix" => array:2 [▼
    22. "concrete" => Closure($container, $parameters = []) {#4 }
    23. "shared" => true
    24. ]
    25. ]
    26. #methodBindings: []
    27. #instances: array:12 [▼
    28. "path" => "/home/vagrant/code/test1/app"
    29. "path.base" => "/home/vagrant/code/test1"
    30. "path.lang" => "/home/vagrant/code/test1/resources/lang"
    31. "path.config" => "/home/vagrant/code/test1/config"
    32. "path.public" => "/home/vagrant/code/test1/public"
    33. "path.storage" => "/home/vagrant/code/test1/storage"
    34. "path.database" => "/home/vagrant/code/test1/database"
    35. "path.resources" => "/home/vagrant/code/test1/resources"
    36. "path.bootstrap" => "/home/vagrant/code/test1/bootstrap"
    37. "app" => Illuminate\Foundation\Application {#2}
    38. "Illuminate\Container\Container" => Illuminate\Foundation\Application {#2}
    39. "Illuminate\Foundation\PackageManifest" => Illuminate\Foundation\PackageManifest {#5 }
    40. ]
    41. #aliases: []
    42. #abstractAliases: []
    43. #extenders: []
    44. #tags: []
    45. #buildStack: []
    46. #with: []
    47. +contextual: []
    48. #reboundCallbacks: []
    49. #globalResolvingCallbacks: []
    50. #globalAfterResolvingCallbacks: []
    51. #resolvingCallbacks: []
    52. #afterResolvingCallbacks: []
    53. }
    54. 下面继续基础服务注册
    55. /**
    56. * Register all of the base service providers.
    57. *
    58. * @return void
    59. */
    60. protected function registerBaseServiceProviders()
    61. {
    62. // 跳转到register方法
    63. $this->register(new EventServiceProvider($this));
    64. $this->register(new LogServiceProvider($this));
    65. $this->register(new RoutingServiceProvider($this));
    66. }
    67. /**
    68. * Register a service provider with the application.
    69. *
    70. * @param \Illuminate\Support\ServiceProvider|string $provider
    71. * @param bool $force
    72. * @return \Illuminate\Support\ServiceProvider
    73. */
    74. public function register($provider, $force = false)
    75. {
    76. // 跳转到getProvider
    77. if (($registered = $this->getProvider($provider)) && !$force) {
    78. // $this->registerBaseServiceProvider没进来
    79. return $registered;
    80. }
    81. // If the given "provider" is a string, we will resolve it, passing in the
    82. // application instance automatically for the developer. This is simply
    83. // a more convenient way of specifying your service provider classes.
    84. // 可以看到register方法 是官方更加推荐的注册服务提供者的方式
    85. if (is_string($provider)) {
    86. // 跳转到resolveProvider方法
    87. // new一个provider
    88. // 我们可以传递一个字符串 laravel会自动帮我们解析
    89. // 框架启动后 可以在任何能够拿到app实例地方调用register方法 会执行自定义服务提供者中的register方法 大家可以自行尝试
    90. $provider = $this->resolveProvider($provider);
    91. }
    92. $provider->register();
    93. // If there are bindings / singletons set as properties on the provider we
    94. // will spin through them and register them with the application, which
    95. // serves as a convenience layer while registering a lot of bindings.
    96. // laravel为我们提供了方便的进行绑定的方式 那就是将绑定映射写在对应服务提供者中的对应属性中
    97. // 官方建议没有特殊要求的情况下 写在AppServiceProvider中即可
    98. if (property_exists($provider, 'bindings')) {
    99. foreach ($provider->bindings as $key => $value) {
    100. $this->bind($key, $value);
    101. }
    102. }
    103. if (property_exists($provider, 'singletons')) {
    104. foreach ($provider->singletons as $key => $value) {
    105. $this->singleton($key, $value);
    106. }
    107. }
    108. // 将已经注册的服务保存到app实例中的对应属性中 serviceProviders loadedProviders
    109. // 标识该服务已经注册
    110. $this->markAsRegistered($provider);
    111. // If the application has already booted, we will call this boot method on
    112. // the provider class so it has an opportunity to do its boot logic and
    113. // will be ready for any usage by this developer's application logic.
    114. // 如果app已经引导完毕 那么在此刻意调用provider的boot方法
    115. if ($this->isBooted()) {
    116. $this->bootProvider($provider);
    117. }
    118. return $provider;
    119. // 建议每进行一步都打印下app实例 看到容器中属性的变化即可
    120. }
    121. app构造方法中的最后一个registerCoreContainerAliases方法
    122. /**
    123. * Register the core class aliases in the container.
    124. *
    125. * @return void
    126. */
    127. public function registerCoreContainerAliases()
    128. {
    129. foreach ([
    130. 'app' => [self::class, \Illuminate\Contracts\Container\Container::class, \Illuminate\Contracts\Foundation\Application::class, \Psr\Container\ContainerInterface::class],
    131. 'auth' => [\Illuminate\Auth\AuthManager::class, \Illuminate\Contracts\Auth\Factory::class],
    132. 'auth.driver' => [\Illuminate\Contracts\Auth\Guard::class],
    133. 'blade.compiler' => [\Illuminate\View\Compilers\BladeCompiler::class],
    134. 'cache' => [\Illuminate\Cache\CacheManager::class, \Illuminate\Contracts\Cache\Factory::class],
    135. 'cache.store' => [\Illuminate\Cache\Repository::class, \Illuminate\Contracts\Cache\Repository::class, \Psr\SimpleCache\CacheInterface::class],
    136. 'cache.psr6' => [\Symfony\Component\Cache\Adapter\Psr16Adapter::class, \Symfony\Component\Cache\Adapter\AdapterInterface::class, \Psr\Cache\CacheItemPoolInterface::class],
    137. 'config' => [\Illuminate\Config\Repository::class, \Illuminate\Contracts\Config\Repository::class],
    138. 'cookie' => [\Illuminate\Cookie\CookieJar::class, \Illuminate\Contracts\Cookie\Factory::class, \Illuminate\Contracts\Cookie\QueueingFactory::class],
    139. 'encrypter' => [\Illuminate\Encryption\Encrypter::class, \Illuminate\Contracts\Encryption\Encrypter::class],
    140. 'db' => [\Illuminate\Database\DatabaseManager::class, \Illuminate\Database\ConnectionResolverInterface::class],
    141. 'db.connection' => [\Illuminate\Database\Connection::class, \Illuminate\Database\ConnectionInterface::class],
    142. 'events' => [\Illuminate\Events\Dispatcher::class, \Illuminate\Contracts\Events\Dispatcher::class],
    143. 'files' => [\Illuminate\Filesystem\Filesystem::class],
    144. 'filesystem' => [\Illuminate\Filesystem\FilesystemManager::class, \Illuminate\Contracts\Filesystem\Factory::class],
    145. 'filesystem.disk' => [\Illuminate\Contracts\Filesystem\Filesystem::class],
    146. 'filesystem.cloud' => [\Illuminate\Contracts\Filesystem\Cloud::class],
    147. 'hash' => [\Illuminate\Hashing\HashManager::class],
    148. 'hash.driver' => [\Illuminate\Contracts\Hashing\Hasher::class],
    149. 'translator' => [\Illuminate\Translation\Translator::class, \Illuminate\Contracts\Translation\Translator::class],
    150. 'log' => [\Illuminate\Log\LogManager::class, \Psr\Log\LoggerInterface::class],
    151. 'mailer' => [\Illuminate\Mail\Mailer::class, \Illuminate\Contracts\Mail\Mailer::class, \Illuminate\Contracts\Mail\MailQueue::class],
    152. 'auth.password' => [\Illuminate\Auth\Passwords\PasswordBrokerManager::class, \Illuminate\Contracts\Auth\PasswordBrokerFactory::class],
    153. 'auth.password.broker' => [\Illuminate\Auth\Passwords\PasswordBroker::class, \Illuminate\Contracts\Auth\PasswordBroker::class],
    154. 'queue' => [\Illuminate\Queue\QueueManager::class, \Illuminate\Contracts\Queue\Factory::class, \Illuminate\Contracts\Queue\Monitor::class],
    155. 'queue.connection' => [\Illuminate\Contracts\Queue\Queue::class],
    156. 'queue.failer' => [\Illuminate\Queue\Failed\FailedJobProviderInterface::class],
    157. 'redirect' => [\Illuminate\Routing\Redirector::class],
    158. 'redis' => [\Illuminate\Redis\RedisManager::class, \Illuminate\Contracts\Redis\Factory::class],
    159. 'redis.connection' => [\Illuminate\Redis\Connections\Connection::class, \Illuminate\Contracts\Redis\Connection::class],
    160. 'request' => [\Illuminate\Http\Request::class, \Symfony\Component\HttpFoundation\Request::class],
    161. 'router' => [\Illuminate\Routing\Router::class, \Illuminate\Contracts\Routing\Registrar::class, \Illuminate\Contracts\Routing\BindingRegistrar::class],
    162. 'session' => [\Illuminate\Session\SessionManager::class],
    163. 'session.store' => [\Illuminate\Session\Store::class, \Illuminate\Contracts\Session\Session::class],
    164. 'url' => [\Illuminate\Routing\UrlGenerator::class, \Illuminate\Contracts\Routing\UrlGenerator::class],
    165. 'validator' => [\Illuminate\Validation\Factory::class, \Illuminate\Contracts\Validation\Factory::class],
    166. 'view' => [\Illuminate\View\Factory::class, \Illuminate\Contracts\View\Factory::class],
    167. ] as $key => $aliases) {
    168. foreach ($aliases as $alias) {
    169. // 跳转到alias方法
    170. $this->alias($key, $alias);
    171. }
    172. }
    173. }
    174. /**
    175. * Alias a type to a different name.
    176. *
    177. * @param string $abstract
    178. * @param string $alias
    179. * @return void
    180. *
    181. * @throws \LogicException
    182. */
    183. // 注册别名
    184. // 可以在Application类的构造方法最后打印一下我们壮观的app实例
    185. // 至此得到了bootstrap/app.php下的$app
    186. public function alias($abstract, $alias)
    187. {
    188. if ($alias === $abstract) {
    189. throw new LogicException("[{$abstract}] is aliased to itself.");
    190. }
    191. $this->aliases[$alias] = $abstract;
    192. $this->abstractAliases[$abstract][] = $alias;
    193. }

下篇会从bootstrap/app.php讲解了。发现错误劳烦指教,感谢!

laravel Application实例化后两个方法的更多相关文章

  1. C++类的实例化的两种方法

    C++ 类的实例化有两种方法: 直接定义对象: 先定义一个类:   class A { public: A(); virtual ~A(); ... ... };   类实现略. 用的时候: A a; ...

  2. 实例化的两种方法(new和函数法)

    // 定义类 类名字是 classA  function classA(){      this.b=1;  }  classA.prototype.b=44;  classA.prototype.s ...

  3. Java基础(42):Java中主类中定义方法加static和不加static的区别(前者可以省略类名直接在主方法调用,后者必须先实例化后用实例调用)

    package lsg.ap.april4th2; /* 知识点:1.Getter and Setter 的应用 2.局部变量与成员变量(也可叫做全局变量) 3.Static关键字的用法 a.成员变量 ...

  4. linux尝试登录失败后锁定用户账户的两种方法

    linux尝试登录失败后锁定用户账户的两种方法 更新时间:2017年06月23日 08:44:31   作者:Carey    我要评论   这篇文章主要给大家分享了linux尝试登录失败后锁定用户账 ...

  5. Laravel 和 Spring Boot 两个框架比较创业篇(二:人工成本)

    前面从开发效率比较了 Laravel 和 Spring Boot两个框架,见:Laravel 和 Spring Boot 两个框架比较创业篇(一:开发效率) ,这一篇打算比较一下人工成本. 本文说的人 ...

  6. jQuery为开发插件提拱了两个方法:jQuery.fn.extend(); jQuery.extend();

    jQuery为开发插件提拱了两个方法,分别是: jQuery.fn.extend(); jQuery.extend(); jQuery.fn jQuery.fn = jQuery.prototype ...

  7. WPF程序将DLL嵌入到EXE的两种方法

    WPF程序将DLL嵌入到EXE的两种方法 这一篇可以看作是<Visual Studio 版本转换工具WPF版开源了>的续,关于<Visual Studio 版本转换工具WPF版开源了 ...

  8. C# web api返回类型设置为json的两种方法

    web api写api接口时默认返回的是把你的对象序列化后以XML形式返回,那么怎样才能让其返回为json呢,下面就介绍两种方法: 方法一:(改配置法) 找到Global.asax文件,在Applic ...

  9. [转]Delphi调用cmd的两种方法

    delphi调用cmd的两种方法vars:string;begins:='cmd.exe /c '+edit1.Text+' >c:\1.txt';winexec(pchar(s),sw_hid ...

随机推荐

  1. 4.26 省选模拟赛 T3 状压dp 差分求答案

    LINK:T3 比较好的题目 考试的时候被毒瘤的T2给搞的心态爆炸 这道题连正解的思路都没有想到. 一看到题求删除点的最少个 可以使得不连通. 瞬间想到最小割 发现对于10分直接跑最小割即可. 不过想 ...

  2. github开源文章生成pdf

    最近需要研究ELK,然后在网上发现了有本书写的不错,然后搜到是在 github 上开源过的.这本书的时间有点久了,就想通过源码自己来生成一个 pdf 我使用的是 ubuntu 系统 step1:安装 ...

  3. 【目标检测】:SPP-Net深入理解(从R-CNN到SPP-Net)

    一. 导论 SPP-Net是何凯明在基于R-CNN的基础上提出来的目标检测模型,使用SPP-Net可以大幅度提升目标检测的速度,检测同样一张图片当中的所有目标,SPP-Net所花费的时间仅仅是RCNN ...

  4. MYSQL 按某个字段分组,然后取每组前3条记录

    先初始化一些数据,表名为 test ,字段及数据为: SQL执行结果为:每个 uid  都只有 3 条记录.   SQL语句为: SELECT   * FROM   test main WHERE   ...

  5. 前端面试 vue 部分 (3)——v-show和v-if的区别

    v-if 适用于在运行时很少改变条件,不需要频繁切换条件的场景: v-show 则适用于需要非常频繁切换条件的场景. v-if 是条件渲染,如果在初始渲染时条件为假,则什么也不做--直到条件第一次变为 ...

  6. 算法面试题:一个List<Student>,要求删除里面的男生,不用Linq和Lamda,求各种解,并说明优缺点!

    算法面试题:一个List,要求删除里面的男生,不用Linq和Lamda,求各种解,并说明优缺点! 解题思路 这是群里某位小伙伴去面试碰到的面试题,从题目本身来看,面试官应该是要考察面试者对泛型 Lis ...

  7. java类的定义与使用

    一 引用数据类型 1.引用数据类型的分类 我们可以把类的类型为两种: 第一种,Java为我们提供好的类,如Scanner类,Random类等,这些已存在的类中包 含了很多的方法与属性,可供我们使用. ...

  8. C#LeetCode刷题之#4-两个排序数组的中位数(Median of Two Sorted Arrays)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4005 访问. 给定两个大小为 m 和 n 的有序数组 nums1 ...

  9. neo4j批量导入数据的两种解决方案

    neo4j批量导入数据有两种方法,第一种是使用cypher语法中的LOAD CSV,第二种是使用neo4j自带的工具neo4j-admin import. LOAD CSV 导入的文件必须是csv文件 ...

  10. 轻量级Java EE企业应用实战:Struts2+Spring5+Hibernate5/JPA2

    轻量级Java EE企业应用实战(第5版)——Struts 2+Spring 5+Hibernate 5/JPA 2整合开发是<轻量级Java EE企业应用实战>的第5版,这一版保持了前几 ...