摘要:實例化各服務提供者,根據其屬性將服務進行分類延遲服務即時服務,從而得到一個數組格式如,延遲處理注冊延遲的服務,以后再進行調用注冊延遲的事件即時處理直接進行注冊調用等,并重新寫入到,然后根據此文件進行相應的處理。
Laravel Kernel引導流程分析 代碼展示
protected function sendRequestThroughRouter($request) { # $this->app->instance("request", $request); # Facade::clearResolvedInstance("request"); // 主要是這句代碼 $this->bootstrap(); # return (new Pipeline($this->app)) # ->send($request) # ->through($this->app->shouldSkipMiddleware() ? [] : $this->middleware) # ->then($this->dispatchToRouter()); } public function bootstrap() { if (! $this->app->hasBeenBootstrapped()) { $this->app->bootstrapWith($this->bootstrappers()); } } protected function bootstrappers() { ##################################################################### #$bootstrappers = [ # IlluminateFoundationBootstrapLoadEnvironmentVariables::class, # IlluminateFoundationBootstrapLoadConfiguration::class, # IlluminateFoundationBootstrapHandleExceptions::class, # IlluminateFoundationBootstrapRegisterFacades::class, # IlluminateFoundationBootstrapRegisterProviders::class, # IlluminateFoundationBootstrapBootProviders::class, #]; ##################################################################### return $this->bootstrappers; } public function bootstrapWith(array $bootstrappers) { $this->hasBeenBootstrapped = true; foreach ($bootstrappers as $bootstrapper) { $this["events"]->fire("bootstrapping: ".$bootstrapper, [$this]); $this->make($bootstrapper)->bootstrap($this); $this["events"]->fire("bootstrapped: ".$bootstrapper, [$this]); } }
$this->make($bootstrapper)->bootstrap($this):會先創建$bootstrapper對象,在執行對象的引導方法,參數為應用對象
處理流程
加載并設置應用的系統環境變量(IlluminateFoundationBootstrapLoadEnvironmentVariables)
public function bootstrap(Application $app) { // /var/www/laravel/bootstrap/cache/config.php 存在則直接返回 if ($app->configurationIsCached()) { return; } $this->checkForSpecificEnvironmentFile($app); try { // 委托Dotenv來臨時設置此次請求的系統環境變量,默認傳參依次為"/var/www/laravel"和".env" (new Dotenv($app->environmentPath(), $app->environmentFile()))->load(); } catch (InvalidPathException $e) { // } } protected function checkForSpecificEnvironmentFile($app) { // cli模式下,并且存在--env參數(類似命令為: cammond --env=example) if (php_sapi_name() == "cli" && with($input = new ArgvInput)->hasParameterOption("--env")) { // 將系統環境文件(類似:/var/www/laravel/.env.example)設置為$app應用的environmentFile屬性,供后面使用 $this->setEnvironmentFilePath( $app, $app->environmentFile().".".$input->getParameterOption("--env") ); } if (! env("APP_ENV")) { return; } $this->setEnvironmentFilePath( $app, $app->environmentFile().".".env("APP_ENV") ); }
(new Dotenv($app->environmentPath(), $app->environmentFile()))->load()
public function __construct($path, $file = ".env") { // 類似/var/www/laravel/.env $this->filePath = $this->getFilePath($path, $file); // 創建加載器,委托Loader處理 $this->loader = new Loader($this->filePath, true); } protected function getFilePath($path, $file) { if (!is_string($file)) { $file = ".env"; } $filePath = rtrim($path, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$file; return $filePath; } public function load() { return $this->loadData(); } protected function loadData($overload = false) { $this->loader = new Loader($this->filePath, !$overload); return $this->loader->load(); }
new Loader($this->filePath, !$overload)
public function __construct($filePath, $immutable = false) { $this->filePath = $filePath; $this->immutable = $immutable; } public function load() { $this->ensureFileIsReadable(); $filePath = $this->filePath; $lines = $this->readLinesFromFile($filePath); foreach ($lines as $line) { // 如果行不是注釋行且含有=號,則進行 if (!$this->isComment($line) && $this->looksLikeSetter($line)) { $this->setEnvironmentVariable($line); } } return $lines; } // 將文件按行的形式讀入到數組并返回 protected function readLinesFromFile($filePath) { $autodetect = ini_get("auto_detect_line_endings"); ini_set("auto_detect_line_endings", "1"); $lines = file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); ini_set("auto_detect_line_endings", $autodetect); return $lines; } public function setEnvironmentVariable($name, $value = null) { // 檢測過濾校驗環境變量 list($name, $value) = $this->normaliseEnvironmentVariable($name, $value); // 當immutable為真時,不覆蓋對應的環境變量 if ($this->immutable && $this->getEnvironmentVariable($name) !== null) { return; } // apache運行環境下,嘗試臨時覆蓋系統環境變量 if (function_exists("apache_getenv") && function_exists("apache_setenv") && apache_getenv($name)) { apache_setenv($name, $value); } // 嘗試臨時設置當前請求的系統環境變量 if (function_exists("putenv")) { putenv("$name=$value"); } // 賦值全局變量 $_ENV[$name] = $value; $_SERVER[$name] = $value; }
將應用配置文件目錄(/var/www/laravel/config)下所有php文件返回的數組載入到$config對象(IlluminateFoundationBootstrapLoadConfiguration)
public function bootstrap(Application $app) { $items = []; // /var/www/laravel/bootstrap/cache/config.php文件[配置文件的緩存合集,加快加載速度]存在則載入,并標記已加載 if (file_exists($cached = $app->getCachedConfigPath())) { $items = require $cached; $loadedFromCache = true; } // 構建config對象,并注入到服務容器 $app->instance("config", $config = new Repository($items)); if (! isset($loadedFromCache)) { // 將系統的配置文件載入到$config對象 $this->loadConfigurationFiles($app, $config); } // 設置$this["env"]為系統環境變量app.env,沒有則默認為production $app->detectEnvironment(function () use ($config) { return $config->get("app.env", "production"); }); date_default_timezone_set($config->get("app.timezone", "UTC")); mb_internal_encoding("UTF-8"); } $config = new IlluminateConfigRepository($items) public function __construct(array $items = []) { $this->items = $items; } protected function loadConfigurationFiles(Application $app, RepositoryContract $repository) { foreach ($this->getConfigurationFiles($app) as $key => $path) { // 此操作將在$repository對象里面構造一個多維數組屬性$this->items,值為相應的系統配置文件返回的數組,后續可以直接通過get獲取 $repository->set($key, require $path); } } /** $files數組形式如下 [ "app" => "/var/www/laravel/config/app.php", "auth" => "/var/www/laravel/config/auth.php", "xx.file" => "/var/www/laravel/config/xx/file.php", "xx.yy.file" => "/var/www/laravel/config/xx/yy/file.php", ] */ protected function getConfigurationFiles(Application $app) { $files = []; // 系統配置文件的路徑(/var/www/laravel/config) $configPath = realpath($app->configPath()); // 文件相關的操作委托給Finder類(很強大)來處理,Finder實現了IteratorAggregate的getIterator方法 foreach (Finder::create()->files()->name("*.php")->in($configPath) as $file) { // 迭代/var/www/laravel/config下面嵌套的層層子目錄構造成.形式的目錄 $directory = $this->getNestedDirectory($file, $configPath); $files[$directory.basename($file->getRealPath(), ".php")] = $file->getRealPath(); } return $files; } $repository->set($key, require $path) // 構造將.形式轉變為相應層級的數組$this->items。比如:$key="xx.yy.file",$value="/var/www/laravel/config/xx/yy/file.php",將會構建為:$this->items["xx"]["yy"]["file"] = $value返回的數組。 public function set($key, $value = null) { $keys = is_array($key) ? $key : [$key => $value]; foreach ($keys as $key => $value) { Arr::set($this->items, $key, $value); } }
根據默認的系統配置文件目錄,以上操作的結果如下:
$config對象(new Repository)里面的$this->items數組屬性,后期可以通過$config->get()來獲取
$this->items["app"] = /var/www/laravel/config/app.php返回的數組; $this->items["auth"] = /var/www/laravel/config/auth.php返回的數組; $this->items["broadcasting"] = /var/www/laravel/config/broadcasting.php返回的數組; $this->items["cache"] = /var/www/laravel/config/cache.php返回的數組; $this->items["database"] = /var/www/laravel/config/database.php返回的數組; $this->items["filesystems"] = /var/www/laravel/config/filesystems.php返回的數組; $this->items["mail"] = /var/www/laravel/config/mail.php返回的數組; $this->items["queue"] = /var/www/laravel/config/queue.php返回的數組; $this->items["services"] = /var/www/laravel/config/services.php返回的數組; $this->items["session"] = /var/www/laravel/config/session.php返回的數組; $this->items["view"] = /var/www/laravel/config/view.php返回的數組; 假如有這樣的文件(/var/www/laravel/config/xx/yy/zz/file.php),返回["a"=>"hello,world!"]數組 將得到:$this->items["xx"]["yy"]["zz"]["file"] = ["a"=>"hello,world!"]; 獲取方式: $config->get("xx.yy.zz.file.a", $default),直接返回"hello,world!";
設置應用的錯誤異常等處理事件(IlluminateFoundationBootstrapHandleExceptions)
public function bootstrap(Application $app) { $this->app = $app; error_reporting(-1); set_error_handler([$this, "handleError"]); set_exception_handler([$this, "handleException"]); register_shutdown_function([$this, "handleShutdown"]); if (! $app->environment("testing")) { ini_set("display_errors", "Off"); } } public function handleError($level, $message, $file = "", $line = 0, $context = []) { if (error_reporting() & $level) { throw new ErrorException($message, 0, $level, $file, $line); } } public function handleException($e) { if (! $e instanceof Exception) { $e = new FatalThrowableError($e); } $this->getExceptionHandler()->report($e); if ($this->app->runningInConsole()) { $this->renderForConsole($e); } else { $this->renderHttpResponse($e); } } // 核心代碼,獲取的AppExceptionsHandle對象 protected function getExceptionHandler() { // make時將會直接調用$this->bindings["IlluminateContractsDebugExceptionHandler"]["concrete"](此代碼位于/var/www/laravel/bootstrap/app.php,應用對象化后,直接注入到服務容器的幾個單例),返回AppExceptionsHandle對象,并將此對象注入到服務容器[參考] return $this->app->make(ExceptionHandler::class); } protected function renderHttpResponse(Exception $e) { $this->getExceptionHandler()->render($this->app["request"], $e)->send(); } // AppExceptionsHandle public function render($request, Exception $e) { $e = $this->prepareException($e); if ($e instanceof HttpResponseException) { return $e->getResponse(); } elseif ($e instanceof AuthenticationException) { return $this->unauthenticated($request, $e); } elseif ($e instanceof ValidationException) { return $this->convertValidationExceptionToResponse($e, $request); } return $this->prepareResponse($request, $e); } protected function renderHttpException(HttpException $e) { $status = $e->getStatusCode(); view()->replaceNamespace("errors", [ resource_path("views/errors"), __DIR__."/views", ]); if (view()->exists("errors::{$status}")) { return response()->view("errors::{$status}", ["exception" => $e], $status, $e->getHeaders()); } else { return $this->convertExceptionToResponse($e); } } public function handleShutdown() { if (! is_null($error = error_get_last()) && $this->isFatal($error["type"])) { $this->handleException($this->fatalExceptionFromError($error, 0)); } }
根據配置項設置應用的 Facades(IlluminateFoundationBootstrapRegisterFacades)
public function bootstrap(Application $app) { Facade::clearResolvedInstances(); Facade::setFacadeApplication($app); // 將配置文件/var/www/laravel/config/app.php返回數組的鍵為aliases的值賦給IlluminateFoundationAliasLoader的aliases屬性,并進行注冊 AliasLoader::getInstance($app->make("config")->get("app.aliases", []))->register(); } public static function clearResolvedInstances() { static::$resolvedInstance = []; } public static function setFacadeApplication($app) { static::$app = $app; } IlluminateFoundationAliasLoader public static function getInstance(array $aliases = []) { if (is_null(static::$instance)) { return static::$instance = new static($aliases); } $aliases = array_merge(static::$instance->getAliases(), $aliases); static::$instance->setAliases($aliases); return static::$instance; } private function __construct($aliases) { $this->aliases = $aliases; } public function getAliases() { return $this->aliases; } public function setAliases(array $aliases) { $this->aliases = $aliases; } public function register() { if (! $this->registered) { $this->prependToLoaderStack(); $this->registered = true; } } protected function prependToLoaderStack() { // 將$this->load注冊到自動加載器的最前面,失敗時拋異常 spl_autoload_register([$this, "load"], true, true); } public function load($alias) { // $facadeNamespace = "Facades",估計是框架內部使用的,以后再看吧 if (static::$facadeNamespace && strpos($alias, static::$facadeNamespace) === 0) { $this->loadFacade($alias); return true; } if (isset($this->aliases[$alias])) { return class_alias($this->aliases[$alias], $alias); } }
Facade的本質
實際上是通過$app->make("config")->get("app.aliases", [])取出config/app.php文件里面的aliases數組并實例化AliasLoader,再將AliasLoader->load方法放到spl自動加載器最前面,最后通過class_alias($this->aliases[$alias], $alias)。當調用Cache::Method時,會觸發Facdes的__callStatic魔術方法,此方法會調用相應對象里面的方法。
注入配置項的服務提供者(IlluminateFoundationBootstrapRegisterProviders)
public function bootstrap(Application $app) { $app->registerConfiguredProviders(); } public function registerConfiguredProviders() { (new ProviderRepository($this, new Filesystem, $this->getCachedServicesPath())) ->load($this->config["app.providers"]); } public function getCachedServicesPath() { return $this->bootstrapPath()."/cache/services.php"; } // 先取services緩存文件,再對IlluminateFoundationProviderRepository進行實例化,隨后加載系統配置文件(./config/app.php)里面的providers數組 (new ProviderRepository($this, new Filesystem, $this->getCachedServicesPath())) ->load($this->config["app.providers"]) public function __construct(ApplicationContract $app, Filesystem $files, $manifestPath) { $this->app = $app; $this->files = $files; $this->manifestPath = $manifestPath; } public function load(array $providers) { $manifest = $this->loadManifest(); if ($this->shouldRecompile($manifest, $providers)) { $manifest = $this->compileManifest($providers); } foreach ($manifest["when"] as $provider => $events) { $this->registerLoadEvents($provider, $events); } foreach ($manifest["eager"] as $provider) { // 直接注冊服務(將直接調用服務的register方法) $this->app->register($provider); } $this->app->addDeferredServices($manifest["deferred"]); } public function loadManifest() { if ($this->files->exists($this->manifestPath)) { $manifest = $this->files->getRequire($this->manifestPath); if ($manifest) { return array_merge(["when" => []], $manifest); } } } public function shouldRecompile($manifest, $providers) { return is_null($manifest) || $manifest["providers"] != $providers; } protected function compileManifest($providers) { $manifest = $this->freshManifest($providers); foreach ($providers as $provider) { $instance = $this->createProvider($provider); // 延遲加載的服務 if ($instance->isDeferred()) { foreach ($instance->provides() as $service) { $manifest["deferred"][$service] = $provider; } // 注冊延遲的事件 $manifest["when"][$provider] = $instance->when(); } // 即時加載的服務 else { $manifest["eager"][] = $provider; } } return $this->writeManifest($manifest); } protected function freshManifest(array $providers) { return ["providers" => $providers, "eager" => [], "deferred" => []]; } public function createProvider($provider) { return new $provider($this->app); } public function isDeferred() { return $this->defer; } public function writeManifest($manifest) { if (! is_writable(dirname($this->manifestPath))) { throw new Exception("The bootstrap/cache directory must be present and writable."); } $this->files->put( $this->manifestPath, " []], $manifest); } protected function registerLoadEvents($provider, array $events) { if (count($events) < 1) { return; } $this->app->make("events")->listen($events, function () use ($provider) { $this->app->register($provider); }); } public function addDeferredServices(array $services) { $this->deferredServices = array_merge($this->deferredServices, $services); }
大致流程
通過/var/www/laravel/bootstrap/cache/services.php等實例化IlluminateFoundationProviderRepository,并加載$this->config["app.providers"]數組。實例化app.providers各服務提供者,根據其defer屬性將服務進行分類(延遲服務|即時服務),從而得到一個$manifest數組(格式如services.php,延遲處理:deferred=>注冊延遲的服務,以后再進行調用;when=>注冊延遲的事件;即時處理:eager=>直接進行注冊調用等),并重新寫入到services.php,然后根據此文件進行相應的處理。
啟動服務提供者的boot方法等操作(IlluminateFoundationBootstrapBootProviders)
public function bootstrap(Application $app) { $app->boot(); } public function boot() { if ($this->booted) { return; } // 可以通過應用的booting方法來注冊服務啟動前的事件監聽者 $this->fireAppCallbacks($this->bootingCallbacks); // 嘗試調用所有的服務提供者的boot方法 array_walk($this->serviceProviders, function ($p) { $this->bootProvider($p); }); $this->booted = true; // 可以通過應用的booted方法來注冊服務啟動后的事件監聽者,若已經啟用了,則直接出發事件 $this->fireAppCallbacks($this->bootedCallbacks); } protected function fireAppCallbacks(array $callbacks) { foreach ($callbacks as $callback) { call_user_func($callback, $this); } } protected function bootProvider(ServiceProvider $provider) { if (method_exists($provider, "boot")) { return $this->call([$provider, "boot"]); } }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/22606.html
摘要:請求周期加載自動加載器獲取應用對象實例化應用解析此對象貫穿全文主要過程設置基礎路徑基礎綁定注冊全局基礎服務核心容器別名設置注冊三個單例獲取對象實例化此對象為應用的樞紐,將會協調各部分之間的工作,完成請求主要過程注入應用對象注入事件對象注入 Laravel 請求周期 加載 composer 自動加載器 require __DIR__./../bootstrap/autoload.php;...
摘要:應用實例所依賴的服務提供者可以在配置文件中的節點找到。完成所有服務提供者注冊到應用實例后,應用實例執行啟動方法引導項目啟動。或內核接收到請求,加載服務提供者,同時,將請求分發給路由器執行。 這是一篇翻譯文章,原文 Request Life Cycle of Laravel,譯文 Laravel 請求生命周期 首發于個人博客,轉載請注明出處。 當需要使用一個框架、工具或者服務時,在使用前...
摘要:直到所有中間件都執行完畢,最后在執行最后的即上述的方法如果上述有地方難懂的,可以參考這邊文章內置函數在中的使用以上是在通過全局中間件時的大致流程,通過中間件和路由中間件也是一樣的,都是采用管道流操作,詳情可翻閱源碼 簡介 Laravel 中間件提供了一種方便的機制來過濾進入應用的 HTTP 請求, 如ValidatePostSize用來驗證POST請求體大小、ThrottleReque...
摘要:所以就是對象其他的都是類似的。和一樣,既可以數組形式訪問,也可以按對象方式訪問。大體流程是創建獲取請求對象包括請求頭和請求體注入請求對象到服務容器配置系統運行環境發送請求 Laravel Kernel實例化后的處理 $response = $kernel->handle( $request = IlluminateHttpRequest::capture() ); 創建并獲取...
摘要:調用了的可以看出,所有服務提供器都在配置文件文件的數組中。啟動的啟動由類負責引導應用的屬性中記錄的所有服務提供器,就是依次調用這些服務提供器的方法,引導完成后就代表應用正式啟動了,可以開始處理請求了。 服務提供器是所有 Laravel 應用程序引導中心。你的應用程序自定義的服務、第三方資源包提供的服務以及 Laravel 的所有核心服務都是通過服務提供器進行注冊(register)和引...
閱讀 1266·2021-11-24 09:39
閱讀 1515·2021-09-07 09:59
閱讀 3479·2019-08-30 15:54
閱讀 2474·2019-08-30 11:00
閱讀 2669·2019-08-29 15:06
閱讀 2159·2019-08-26 13:52
閱讀 427·2019-08-26 13:24
閱讀 2489·2019-08-26 12:20