laravel application 容器app

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

Application是laravel的核心容器,幾乎處理全部東西的時候都須要用到,主要功能有:

1. app首先繼承了container,做爲一個容器類存在html

2。註冊了laravel運行過程的須要的基礎類進容器,而且生成了運行須要的實例。承擔了初始化功能。laravel

   這裏須要額外說一下,app裏面說的所謂註冊,不是指綁定,應該是直接直接實例化了,並注入到容器。可是,針對provider,實例化了provider,並運行了,並不會生成實際的類,而是將類綁定。git

3. 額外提供了更方便的一些功能,主要是與程序的環境相關的,好比:provider處理運行是在這裏,config app裏面的配置就在本類讀取並處理。web

具體功能以下:

1. container的功能就不說了,前面微博已經描述這個了。

2. 註冊程序運行中須要的最基礎的類

   在 __construct:構造函數中處理:redis

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

 

  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());api

 

  registerBaseBindings

        註冊基本綁定,添加app自己進容器實例,而後,PackageManifest添加進容器實例,本功能能夠處理一些默認的provider和aliases,本功能後續展開描述。cookie

  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

相關文章
相關標籤/搜索