摘要:本文使用版本為使用實際調用的是這個映射是如何做的將里的數組里面的類設置別名來自于中數組為一個類創建別名這個文件沒有,只有這里為是容器對象,實現了接口,最終調用的還是容器的方法
本文使用版本為laravel5.5
cache getpublic function cache() { $c=Cache::get("app"); if(!$c) { Cache::put("app", "cache", 1); } dump($c);//cache }config/app.php
"aliases" => [ "App" => IlluminateSupportFacadesApp::class, "Artisan" => IlluminateSupportFacadesArtisan::class, "Auth" => IlluminateSupportFacadesAuth::class, "Blade" => IlluminateSupportFacadesBlade::class, "Broadcast" => IlluminateSupportFacadesBroadcast::class, "Bus" => IlluminateSupportFacadesBus::class, "Cache" => IlluminateSupportFacadesCache::class, ]
使用cache實際調用的是IlluminateSupportFacadesCache,這個映射是如何做的?
public/index.php$response = $kernel->handle( $request = IlluminateHttpRequest::capture() );bootstarp/app.php
$app->singleton( IlluminateContractsHttpKernel::class, AppHttpKernel::class );app/http/kernel.php
use IlluminateFoundationHttpKernel as HttpKernel; class Kernel extends HttpKernel { }Illuminate/Foundation/Http/Kernel.php
public function handle($request) { try { $request->enableHttpMethodParameterOverride(); $response = $this->sendRequestThroughRouter($request); } catch (Exception $e) { $this->reportException($e); $response = $this->renderException($request, $e); } catch (Throwable $e) { $this->reportException($e = new FatalThrowableError($e)); $response = $this->renderException($request, $e); } $this->app["events"]->dispatch( new EventsRequestHandled($request, $response) ); return $response; } 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()); } }Illuminate/Foundation/Application.php
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]); } }Illuminate/Foundation/Bootstrap/RegisterFacades.php
public function bootstrap(Application $app) { Facade::clearResolvedInstances(); Facade::setFacadeApplication($app); //將config/app.php 里的aliases數組里面的Facades類設置別名 AliasLoader::getInstance(array_merge( $app->make("config")->get("app.aliases", []), $app->make(PackageManifest::class)->aliases() ))->register(); }Illuminate/Foundation/AliasLoader.php
public function load($alias) { if (static::$facadeNamespace && strpos($alias, static::$facadeNamespace) === 0) { $this->loadFacade($alias); return true; } // $alias來自于config/app.php中aliases數組 if (isset($this->aliases[$alias])) { //"Route" => IlluminateSupportFacadesRoute::class, // class_alias 為一個類創建別名 return class_alias($this->aliases[$alias], $alias); } }Illuminate/Support/Facades/Cache.php
class Cache extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return "cache"; } }Illuminate/Support/Facades/Facade.php
這個文件沒有get set ,只有__callStatic
public static function __callStatic($method, $args) { $instance = static::getFacadeRoot(); if (! $instance) { throw new RuntimeException("A facade root has not been set."); } return $instance->$method(...$args); } public static function getFacadeRoot() { return static::resolveFacadeInstance(static::getFacadeAccessor()); } protected static function resolveFacadeInstance($name) { //這里$name為cache if (is_object($name)) { return $name; } if (isset(static::$resolvedInstance[$name])) { return static::$resolvedInstance[$name]; } //$app是容器對象,實現了ArrayAccess接口,最終調用的還是容器的make方法 return static::$resolvedInstance[$name] = static::$app[$name]; }IlluminateContainerContainer.php
public function make($abstract, array $parameters = []) { return $this->resolve($abstract, $parameters); } protected function resolve($abstract, $parameters = []) { $abstract = $this->getAlias($abstract); $needsContextualBuild = ! empty($parameters) || ! is_null( $this->getContextualConcrete($abstract) ); // If an instance of the type is currently being managed as a singleton we"ll // just return an existing instance instead of instantiating new instances // so the developer can keep using the same objects instance every time. if (isset($this->instances[$abstract]) && ! $needsContextualBuild) { return $this->instances[$abstract]; } $this->with[] = $parameters; $concrete = $this->getConcrete($abstract); // We"re ready to instantiate an instance of the concrete type registered for // the binding. This will instantiate the types, as well as resolve any of // its "nested" dependencies recursively until all have gotten resolved. if ($this->isBuildable($concrete, $abstract)) { $object = $this->build($concrete); } else { $object = $this->make($concrete); } // If we defined any extenders for this type, we"ll need to spin through them // and apply them to the object being built. This allows for the extension // of services, such as changing configuration or decorating the object. foreach ($this->getExtenders($abstract) as $extender) { $object = $extender($object, $this); } // If the requested type is registered as a singleton we"ll want to cache off // the instances in "memory" so we can return it later without creating an // entirely new instance of an object on each subsequent request for it. if ($this->isShared($abstract) && ! $needsContextualBuild) { $this->instances[$abstract] = $object; } $this->fireResolvingCallbacks($abstract, $object); // Before returning, we will also set the resolved flag to "true" and pop off // the parameter overrides for this build. After those two things are done // we will be ready to return back the fully constructed class instance. $this->resolved[$abstract] = true; array_pop($this->with); return $object; }Illuminate/Cache/CacheServiceProvider.php
public function register() { $this->app->singleton("cache", function ($app) { return new CacheManager($app); }); $this->app->singleton("cache.store", function ($app) { return $app["cache"]->driver(); }); $this->app->singleton("memcached.connector", function () { return new MemcachedConnector; }); }get set
$instance->$method(...$args)
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/29907.html
摘要:然后中間件使用方法來啟動獲取實例,使用類來管理主要分為兩步獲取實例,主要步驟是通過該實例從存儲介質中讀取該次請求所需要的數據,主要步驟是。 說明:本文主要通過學習Laravel的session源碼學習Laravel是如何設計session的,將自己的學習心得分享出來,希望對別人有所幫助。Laravel在web middleware中定義了session中間件IlluminateSess...
摘要:源碼解析這個類的源碼主要就是文件的操作和文件屬性的操作,而具體的操作是通過每一個實現的,看其構造函數看以上代碼知道對于操作,實際上是通過的實例來實現的。可以看下的使用上文已經說了,使得對各種的操作變得更方便了,不管是還是得。 說明:本文主要學習下LeagueFlysystem這個Filesystem Abstract Layer,學習下這個package的設計思想和編碼技巧,把自己的一...
摘要:原文地址下面是中文翻譯擁有強大的控制反轉依賴注入容器。單例在使用自動綁定和時,每次需要時都會創建一個新的實例或者調用閉包。 原文地址 Laravels Dependency Injection Container in Depth 下面是中文翻譯 Laravel擁有強大的控制反轉(IoC)/依賴注入(DI) 容器。不幸的是官方文檔并沒有涵蓋所有可用的功能,因此,我決定嘗試寫文檔為自...
摘要:上次提到過,模板引擎一般是要做三件事情變量值的輸出條件判斷和循環引入或繼承其他文件現在就來看看的模板引擎是如何來處理這三件事情的。引擎接下來就是本文的重點是如何編譯的。如果有興趣的話,也可以實現一個自己的模板解析引擎。 上次提到過,模板引擎一般是要做三件事情: 變量值的輸出(echo) 條件判斷和循環(if ... else、for、foreach、while) 引入或繼承其他文件 ...
摘要:總結本文主要學習了啟動時做的七步準備工作環境檢測配置加載日志配置異常處理注冊注冊啟動。 說明:Laravel在把Request通過管道Pipeline送入中間件Middleware和路由Router之前,還做了程序的啟動Bootstrap工作,本文主要學習相關源碼,看看Laravel啟動程序做了哪些具體工作,并將個人的研究心得分享出來,希望對別人有所幫助。Laravel在入口index...
閱讀 2482·2021-11-24 09:39
閱讀 3523·2019-08-30 15:53
閱讀 599·2019-08-29 15:15
閱讀 2907·2019-08-26 13:23
閱讀 3220·2019-08-26 10:48
閱讀 647·2019-08-26 10:31
閱讀 771·2019-08-26 10:30
閱讀 2367·2019-08-23 18:32