laravel容器初始化registerBaseServiceProviders方法

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

    在app调用过registerBaseBindings方法后,打印app实例,发现bindings中存放的确实是闭包,shared为true表示单例绑定,instances中表示容器中可以直接复用的实例
    Illuminate\Foundation\Application {#2 ▼
    #basePath: "/home/vagrant/code/test1"
    #hasBeenBootstrapped: false
    #booted: false
    #bootingCallbacks: []
    #bootedCallbacks: []
    #terminatingCallbacks: []
    #serviceProviders: []
    #loadedProviders: []
    #deferredServices: []
    #appPath: null
    #databasePath: null
    #storagePath: null
    #environmentPath: null
    #environmentFile: ".env"
    #isRunningInConsole: null
    #namespace: null
    #resolved: []
    #bindings: array:1 [▼
    "Illuminate\Foundation\Mix" => array:2 [▼
    "concrete" => Closure($container, $parameters = []) {#4 }
    "shared" => true
    ]
    ]
    #methodBindings: []
    #instances: array:12 [▼
    "path" => "/home/vagrant/code/test1/app"
    "path.base" => "/home/vagrant/code/test1"
    "path.lang" => "/home/vagrant/code/test1/resources/lang"
    "path.config" => "/home/vagrant/code/test1/config"
    "path.public" => "/home/vagrant/code/test1/public"
    "path.storage" => "/home/vagrant/code/test1/storage"
    "path.database" => "/home/vagrant/code/test1/database"
    "path.resources" => "/home/vagrant/code/test1/resources"
    "path.bootstrap" => "/home/vagrant/code/test1/bootstrap"
    "app" => Illuminate\Foundation\Application {#2}
    "Illuminate\Container\Container" => Illuminate\Foundation\Application {#2}
    "Illuminate\Foundation\PackageManifest" => Illuminate\Foundation\PackageManifest {#5 }
    ]
    #aliases: []
    #abstractAliases: []
    #extenders: []
    #tags: []
    #buildStack: []
    #with: []
    +contextual: []
    #reboundCallbacks: []
    #globalResolvingCallbacks: []
    #globalAfterResolvingCallbacks: []
    #resolvingCallbacks: []
    #afterResolvingCallbacks: []
    } 下面继续基础服务注册
    /**
    * Register all of the base service providers.
    *
    * @return void
    */
    protected function registerBaseServiceProviders()
    {
    // 跳转到register方法
    $this->register(new EventServiceProvider($this)); $this->register(new LogServiceProvider($this)); $this->register(new RoutingServiceProvider($this));
    } /**
    * Register a service provider with the application.
    *
    * @param \Illuminate\Support\ServiceProvider|string $provider
    * @param bool $force
    * @return \Illuminate\Support\ServiceProvider
    */
    public function register($provider, $force = false)
    {
    // 跳转到getProvider
    if (($registered = $this->getProvider($provider)) && !$force) {
    // $this->registerBaseServiceProvider没进来
    return $registered;
    } // If the given "provider" is a string, we will resolve it, passing in the
    // application instance automatically for the developer. This is simply
    // a more convenient way of specifying your service provider classes.
    // 可以看到register方法 是官方更加推荐的注册服务提供者的方式
    if (is_string($provider)) {
    // 跳转到resolveProvider方法
    // new一个provider
    // 我们可以传递一个字符串 laravel会自动帮我们解析
    // 框架启动后 可以在任何能够拿到app实例地方调用register方法 会执行自定义服务提供者中的register方法 大家可以自行尝试
    $provider = $this->resolveProvider($provider);
    } $provider->register(); // If there are bindings / singletons set as properties on the provider we
    // will spin through them and register them with the application, which
    // serves as a convenience layer while registering a lot of bindings. // laravel为我们提供了方便的进行绑定的方式 那就是将绑定映射写在对应服务提供者中的对应属性中
    // 官方建议没有特殊要求的情况下 写在AppServiceProvider中即可
    if (property_exists($provider, 'bindings')) {
    foreach ($provider->bindings as $key => $value) {
    $this->bind($key, $value);
    }
    } if (property_exists($provider, 'singletons')) {
    foreach ($provider->singletons as $key => $value) {
    $this->singleton($key, $value);
    }
    } // 将已经注册的服务保存到app实例中的对应属性中 serviceProviders loadedProviders
    // 标识该服务已经注册
    $this->markAsRegistered($provider); // If the application has already booted, we will call this boot method on
    // the provider class so it has an opportunity to do its boot logic and
    // will be ready for any usage by this developer's application logic. // 如果app已经引导完毕 那么在此刻意调用provider的boot方法
    if ($this->isBooted()) {
    $this->bootProvider($provider);
    } return $provider;
    // 建议每进行一步都打印下app实例 看到容器中属性的变化即可
    } app构造方法中的最后一个registerCoreContainerAliases方法
    /**
    * Register the core class aliases in the container.
    *
    * @return void
    */
    public function registerCoreContainerAliases()
    {
    foreach ([
    'app' => [self::class, \Illuminate\Contracts\Container\Container::class, \Illuminate\Contracts\Foundation\Application::class, \Psr\Container\ContainerInterface::class],
    'auth' => [\Illuminate\Auth\AuthManager::class, \Illuminate\Contracts\Auth\Factory::class],
    'auth.driver' => [\Illuminate\Contracts\Auth\Guard::class],
    'blade.compiler' => [\Illuminate\View\Compilers\BladeCompiler::class],
    'cache' => [\Illuminate\Cache\CacheManager::class, \Illuminate\Contracts\Cache\Factory::class],
    'cache.store' => [\Illuminate\Cache\Repository::class, \Illuminate\Contracts\Cache\Repository::class, \Psr\SimpleCache\CacheInterface::class],
    'cache.psr6' => [\Symfony\Component\Cache\Adapter\Psr16Adapter::class, \Symfony\Component\Cache\Adapter\AdapterInterface::class, \Psr\Cache\CacheItemPoolInterface::class],
    'config' => [\Illuminate\Config\Repository::class, \Illuminate\Contracts\Config\Repository::class],
    'cookie' => [\Illuminate\Cookie\CookieJar::class, \Illuminate\Contracts\Cookie\Factory::class, \Illuminate\Contracts\Cookie\QueueingFactory::class],
    'encrypter' => [\Illuminate\Encryption\Encrypter::class, \Illuminate\Contracts\Encryption\Encrypter::class],
    'db' => [\Illuminate\Database\DatabaseManager::class, \Illuminate\Database\ConnectionResolverInterface::class],
    'db.connection' => [\Illuminate\Database\Connection::class, \Illuminate\Database\ConnectionInterface::class],
    'events' => [\Illuminate\Events\Dispatcher::class, \Illuminate\Contracts\Events\Dispatcher::class],
    'files' => [\Illuminate\Filesystem\Filesystem::class],
    'filesystem' => [\Illuminate\Filesystem\FilesystemManager::class, \Illuminate\Contracts\Filesystem\Factory::class],
    'filesystem.disk' => [\Illuminate\Contracts\Filesystem\Filesystem::class],
    'filesystem.cloud' => [\Illuminate\Contracts\Filesystem\Cloud::class],
    'hash' => [\Illuminate\Hashing\HashManager::class],
    'hash.driver' => [\Illuminate\Contracts\Hashing\Hasher::class],
    'translator' => [\Illuminate\Translation\Translator::class, \Illuminate\Contracts\Translation\Translator::class],
    'log' => [\Illuminate\Log\LogManager::class, \Psr\Log\LoggerInterface::class],
    'mailer' => [\Illuminate\Mail\Mailer::class, \Illuminate\Contracts\Mail\Mailer::class, \Illuminate\Contracts\Mail\MailQueue::class],
    'auth.password' => [\Illuminate\Auth\Passwords\PasswordBrokerManager::class, \Illuminate\Contracts\Auth\PasswordBrokerFactory::class],
    'auth.password.broker' => [\Illuminate\Auth\Passwords\PasswordBroker::class, \Illuminate\Contracts\Auth\PasswordBroker::class],
    'queue' => [\Illuminate\Queue\QueueManager::class, \Illuminate\Contracts\Queue\Factory::class, \Illuminate\Contracts\Queue\Monitor::class],
    'queue.connection' => [\Illuminate\Contracts\Queue\Queue::class],
    'queue.failer' => [\Illuminate\Queue\Failed\FailedJobProviderInterface::class],
    'redirect' => [\Illuminate\Routing\Redirector::class],
    'redis' => [\Illuminate\Redis\RedisManager::class, \Illuminate\Contracts\Redis\Factory::class],
    'redis.connection' => [\Illuminate\Redis\Connections\Connection::class, \Illuminate\Contracts\Redis\Connection::class],
    'request' => [\Illuminate\Http\Request::class, \Symfony\Component\HttpFoundation\Request::class],
    'router' => [\Illuminate\Routing\Router::class, \Illuminate\Contracts\Routing\Registrar::class, \Illuminate\Contracts\Routing\BindingRegistrar::class],
    'session' => [\Illuminate\Session\SessionManager::class],
    'session.store' => [\Illuminate\Session\Store::class, \Illuminate\Contracts\Session\Session::class],
    'url' => [\Illuminate\Routing\UrlGenerator::class, \Illuminate\Contracts\Routing\UrlGenerator::class],
    'validator' => [\Illuminate\Validation\Factory::class, \Illuminate\Contracts\Validation\Factory::class],
    'view' => [\Illuminate\View\Factory::class, \Illuminate\Contracts\View\Factory::class],
    ] as $key => $aliases) {
    foreach ($aliases as $alias) {
    // 跳转到alias方法
    $this->alias($key, $alias);
    }
    }
    } /**
    * Alias a type to a different name.
    *
    * @param string $abstract
    * @param string $alias
    * @return void
    *
    * @throws \LogicException
    */
    // 注册别名
    // 可以在Application类的构造方法最后打印一下我们壮观的app实例
    // 至此得到了bootstrap/app.php下的$app
    public function alias($abstract, $alias)
    {
    if ($alias === $abstract) {
    throw new LogicException("[{$abstract}] is aliased to itself.");
    } $this->aliases[$alias] = $abstract; $this->abstractAliases[$abstract][] = $alias;
    }

下篇会从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. luogu P5043 【模板】树同构 hash 最小表示法

    LINK:模板 树同构 题目说的很迷 给了一棵有根树 但是重新标号 言外之意还是一棵无根树 然后要求判断是否重构. 由于时无根的 所以一个比较显然的想法暴力枚举根. 然后做树hash或者树的最小表示法 ...

  2. three.js 自制骨骼动画(二)

    上一篇说了一下自制骨骼动画,这一篇郭先生使用帧动画让骨骼动画动起来.帧动画是一套比较完善的动画剪辑方法,详细我的api我们就不多说了,网上有很多例子,自行查找学习.在线案例请点击博客原文.话不多说先上 ...

  3. [转]Nginx介绍-反向代理、负载均衡

    原文:https://www.cnblogs.com/wcwnina/p/8728391.html 作者:失恋的蔷薇 1. Nginx的产生 没有听过Nginx?那么一定听过它的"同行&qu ...

  4. jmeter如何设置全局变量

    场景:性能测试或者接口测试,如果想跨线程引用(案例:A线程组里面的一个输出,是B线程组里面的一个输入,这个时候如果要引用),这个时候你就必须要设置全局变量;全链路压测也需要分不同场景,通常情况,一个场 ...

  5. Qt实现的多菜单选择界面

    文章目录 1.效果展示 2.实现代码 2.1 菜单实现代码 2.1.1 头文件 2.1.2 源文件 2.2 应用代码 1.效果展示 这种菜单样式比较常用,实现的方法也有很多种,比如可以直接使用QTab ...

  6. 005_针对于go语言中速率限制的思考

    在之前的go语言的速率限制这篇文章里,我们尝试了普通的速率限制,和脉冲型速率限制.其中,脉冲型速率限制是放开了限制,里面有3个请求是一次性到达,然后再按照200ms的速度限制的,之前的代码如下所示: ...

  7. 云服务器远程连接mysql数据库

    首先需要在云服务器上,下载安装好mysql与Navicat. mysql下载好以后,打开云端的开始,找到mysql的命令窗,进入输入自己的mysql密码,稍等片刻进入mysql数据库 进入之后输入下列 ...

  8. Python利用Twilio(国际)以及腾讯云服务做一些事情

    短信服务验证服务已经不是什么新鲜事了,但是免费的手机短信服务却不多见,本次利用Python3.0基于Twilio和腾讯云服务分别来体验一下国际短信和国内短信接口. 首先,注册Twilio: www.t ...

  9. IE6和IE11之间 表单提交 按钮设置了disabled属性

    JSP代码可以不看,就是一个表单,通过submit提交. <form action="mainAction.do?method=saveQuote" method=" ...

  10. C++字符串转整形、浮点型stof()、atoi()、strtol()等

    头文件:#include<stdlib.h>string str;stof:float val=stof(str);atoi:int val=atoi(str);atol:long val ...