1. 程式人生 > >laravel application 容器app

laravel application 容器app

resource quest esp rac cts 過程 ram auth 目錄

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