vendor/laravel/framework/src/Illuminate/Foundation/Application.php

Application是laravel的核心容器,几乎处理所有东西的时候都需要用到,主要功能有:

1. app首先继承了container,作为一个容器类存在

2。注册了laravel运行过程的需要的基础类进容器,并且生成了运行需要的实例。承担了初始化功能。

这里需要额外说一下,app里面说的所谓注册,不是指绑定,应该是直接直接实例化了,并注入到容器。但是,针对provider,实例化了provider,并运行了,并不会生成实际的类,而是将类绑定。

3. 额外提供了更方便的一些功能,主要是与程序的环境相关的,比如:provider处理运行是在这里,config app里面的配置就在本类读取并处理。

具体功能如下:

1. container的功能就不说了,前面微博已经描述这个了。

2. 注册程序运行中需要的最基础的类

   在 __construct:构造函数中处理:

if ($basePath) {
            $this->setBasePath($basePath);
        }
        $this->registerBaseBindings();
        $this->registerBaseServiceProviders();
        $this->registerCoreContainerAliases();

  setBasePath 基本路径添加进容器实例,这也是这绝大多数框架里面都需要做都事情,防止出现相对绝对等各种路径使用时候的混乱,总共添加了这么多:

   $this->instance('path', $this->path());
        $this->instance('path.base', $this->basePath());
        $this->instance('path.lang', $this->langPath());
        $this->instance('path.config', $this->configPath());
        $this->instance('path.public', $this->publicPath());
        $this->instance('path.storage', $this->storagePath());
        $this->instance('path.database', $this->databasePath());
        $this->instance('path.resources', $this->resourcePath());
        $this->instance('path.bootstrap', $this->bootstrapPath());

  registerBaseBindings

注册基本绑定,添加app本身进容器实例,然后,PackageManifest添加进容器实例,本功能可以处理一些默认的provider和aliases,本功能后续展开描述。

  registerBaseServiceProviders

    注册基本的Providers,包括event/log/router 这三部分

    event:vendor/laravel/framework/src/Illuminate/Events/EventServiceProvider.php

    log:vendor/laravel/framework/src/Illuminate/Log/LogServiceProvider.php

    router:vendor/laravel/framework/src/Illuminate/Routing/RoutingServiceProvider.php

router的provider跟别的不一样,提供了多个相关绑定:router url redirect Psr\Http\Message\ServerRequestInterface Psr\Http\Message\ResponseInterface Illuminate\Contracts\Routing\ResponseFactory Illuminate\Routing\Contracts\ControllerDispatcher

  registerCoreContainerAliases 注册一些核心的别名,包括:

'app'                  => [\Illuminate\Foundation\Application::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],
                        '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],
                        '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\Contracts\Hashing\Hasher::class],
                        'translator'           => [\Illuminate\Translation\Translator::class, \Illuminate\Contracts\Translation\Translator::class],
                        'log'                  => [\Illuminate\Log\Writer::class, \Illuminate\Contracts\Logging\Log::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],
                        '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],

所以,初始化之后的app里面的内容如下:

bindings 里面有如下内容:

events

log
router
url
redirect
Psr\Http\Message\ServerRequestInterface
Psr\Http\Message\ResponseInterface
Illuminate\Contracts\Routing\ResponseFactory
Illuminate\Routing\Contracts\ControllerDispatcher

总结一下,application初始化的时候,就干了这么几个事情:

1. 定位自己,确定文件运行的相关目录

2. 把自己扔进容器,把用来引用其他代码的package类实例化进容器。

3. log/event/router 三大基本功能提高provider

4. 绑定别名,当然,许多别名,这这个时候,实际上是无效的。因为根本没绑定到实际类,估计是为了占空。

在container之外扩展的功能:

1.  启动时额外提供的初始化功能,并且添加了相关事件的注册,主要函数bootstrapWith。

  添加事件 bootstrapping bootstrapped 并且注册外面类提供的所有初始化功能。准备程序运行的参数和环境的注入。

2. registerConfiguredProviders 函数,读取app.providers里面定义的provider,并注册。

3. provider的处理,主要函数,register(注册provider),boot等。

4. 延迟加载deferredServices,延迟加载的provider,只是注册进container,但是,并不实例化provider,只有调用相关类的时候,才会运行provider进行注册。

5.handle 直接调用kernel处理request,也是针对网络访问的一个出口函数。

app在container之外,添加了provider的概念,添加了request相关处理的概念,将container包装成了一个处理网络请求的新container。

instances里面有如下内容:

path::/Users/yuxuezhong/git/webservice/api/app
        path.base::/Users/yuxuezhong/git/webservice/api
        path.lang::/Users/yuxuezhong/git/webservice/api/resources/lang
        path.config::/Users/yuxuezhong/git/webservice/api/config
        path.public::/Users/yuxuezhong/git/webservice/api/public
        path.storage::/Users/yuxuezhong/git/webservice/api/storage
        path.database::/Users/yuxuezhong/git/webservice/api/database
        path.resources::/Users/yuxuezhong/git/webservice/api/resources
        path.bootstrap::/Users/yuxuezhong/git/webservice/api/bootstrap
        app::Illuminate\Foundation\Application
        Illuminate\Container\Container::Illuminate\Foundation\Application
        Illuminate\Foundation\PackageManifest::Illuminate\Foundation\PackageManifest

laravel application 容器app的更多相关文章

  1. [php]laravel框架容器管理的一些要点

    本文面向php语言的laravel框架的用户,介绍一些laravel框架里面容器管理方面的使用要点.文章很长,但是内容应该很有用,希望有需要的朋友能看到.php经验有限,不到位的地方,欢迎帮忙指正. ...

  2. laravel框架容器管理

    来自http://www.cnblogs.com/chy1000/p/7072936.html 本文面向php语言的laravel框架的用户,介绍一些laravel框架里面容器管理方面的使用要点.文章 ...

  3. laravel框架容器管理的一些要点

    本文面向php语言的laravel框架的用户,介绍一些laravel框架里面容器管理方面的使用要点.文章很长,但是内容应该很有用,希望有需要的朋友能看到.php经验有限,不到位的地方,欢迎帮忙指正. ...

  4. laravel框架容器管理的一些要点(转)

    本文面向php语言的laravel框架的用户,介绍一些laravel框架里面容器管理方面的使用要点.文章很长,但是内容应该很有用,希望有需要的朋友能看到.php经验有限,不到位的地方,欢迎帮忙指正. ...

  5. Laravel服务容器的绑定与解析

    本篇文章给大家带来的内容是关于Laravel服务容器的绑定与解析,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 前言   老实说,第一次老大让我看laravel框架手册的那天早上,我 ...

  6. laravel 服务容器实现原理

    前言 通过实现laravel 框架功能,以便深入理解laravel框架的先进思想. 什么是服务容器 服务容器是用来管理类依赖与运行依赖注入的工具.Laravel框架中就是使用服务容器来实现 ** 控制 ...

  7. Laravel 服务容器、服务提供器、契约实例讲解

        前言 刚开始看laravel服务容器.契约.服务提供器的确生涩难懂,不单单是概念繁多,而且实际的demo很难找(找是找到了,但难用啊),最后就隔一段时间看一遍,大概个十来遍,还真给看出个门道, ...

  8. laravel服务容器

    laravel框架底层解析 本文参考陈昊<Laravel框架关键技术解析>,搭建一个属于自己的简化版服务容器.其中涉及到反射.自动加载,还是需要去了解一下. laravel服务容器 建立项 ...

  9. 关于使用 Laravel 服务容器的优势介绍

    如果说laravel框架的核心是什么,那么无疑是服务容器.理解服务容器的概念,对于我们使用laravel太重要了,应该说是否理解服务容器的概念是区分是否入门laravel的重要条件.因为整个框架正是在 ...

随机推荐

  1. Go语言基础(一)

    Go语言基础(一) 国庆体验一下大名鼎鼎的Go语言,IDE使用IEDA+Go插件,边敲代码边体会,感觉Go语言好酷 一.Hello World 和Java类似,go文件需要一个package包含,代码 ...

  2. centos7.4安装redis

    参考连接 https://blog.csdn.net/qq_19399235/article/details/78313633/

  3. vue_class 绑定_style 绑定

    1. class 绑定 data: { myClass: "bClass", hasA: true, hasB: false } 字符串模式: 类名不确定 <p class= ...

  4. Spark性能优化

    1.Spark优化 1) 使用foreachPartitions替代foreach. 原理类似于“使用mapPartitions替代map”,也是一次函数调用处理一个partition的所有数据,而不 ...

  5. 《javascript设计模式与开发实践》--- (单一职责原则)

    看的这本书叫<JavaScript设计模式与开发实践> 先规划一下看书的顺序,基础知识我已经大概的浏览了一遍了,没有留下笔记,以后有时间还会补上.本来打算顺着看的.但是我感觉我很难短时间内 ...

  6. sql server里中自增长的ID重新开始排

    dbcc checkident('tablename',reseed,0); 执行:dbcc checkident('TableA',reseed,0); 执行结束:中途报了几次插入重复键. 结论:用 ...

  7. 64bit program invoke 32bit library with rpcgen

    https://www.cnblogs.com/ddk3000/p/5051108.html 这篇博客介绍了一种用rpc的方法实现64位程序调用32位动态库的方法,核心是利用rpcgen简化了进程间通 ...

  8. linux mysql 定时备份

    1.查看磁盘空间情况: 既然是定时备份,就要选择一个空间充足的磁盘空间,避免出现因空间不足导致备份失败,数据丢失的恶果! 存储到当前磁盘这是最简单,却是最不推荐的:服务器有多块硬盘,最好是把备份存放到 ...

  9. Python---函数的相关知识点总结一:

    1:定义函数 def printInfo(): print("I love Python!") #调用函数 #注意:函数定义完毕并不会被默认执行,只能通过调用的方式来让它执行 pr ...

  10. c++11 强类型枚举 enum class

    在标准C++中,枚举类型不是类型安全的.枚举类型被视为整数,这使得两种不同的枚举类型之间可以进行比较.C++03 唯一提供的安全机制是一个整数或一个枚举型值不能隐式转换到另一个枚举别型. 此外,枚举所 ...