站長(zhǎng)資訊網(wǎng)
        最全最豐富的資訊網(wǎng)站

        Laravel的依賴注入實(shí)現(xiàn)原理是什么

        在Laravel中,依賴注入的實(shí)現(xiàn)原理是利用類方法反射,取得參數(shù)類型,然后利用容器構(gòu)造好實(shí)例,再使用回調(diào)函數(shù)調(diào)起;注入對(duì)象構(gòu)造函數(shù)不能有參數(shù),否則會(huì)報(bào)錯(cuò),依賴注入必須要由Router類調(diào)起,否則直接用new方式是無(wú)法實(shí)現(xiàn)注入的。

        Laravel的依賴注入實(shí)現(xiàn)原理是什么

        本文操作環(huán)境:Windows10系統(tǒng)、Laravel6版、Dell G3電腦。

        Laravel的依賴注入實(shí)現(xiàn)原理是什么

        laravel容器包含控制反轉(zhuǎn)和依賴注入,使用起來(lái)就是,先把對(duì)象bind好,需要時(shí)可以直接使用make來(lái)取就好。

        具體分析參照:http://laravelacademy.org/post/769.html

        通常我們的調(diào)用如下。

        $config = $container->make('config'); $connection = new Connection($this->config);

        比較好理解,這樣的好處就是不用直接 new 一個(gè)實(shí)例了,方法傳值沒啥改變,還可以多處共享此實(shí)例。

        但這跟依賴注入有什么關(guān)系,真正的依賴注入是不需給方法傳遞任何參數(shù)值,只需要指明方法參數(shù)類型,代碼自動(dòng)查找關(guān)系依賴自動(dòng)注入。

        這個(gè)特性在 laravel 的 Controller、Job 等處可以體現(xiàn),如下:

        class TestController extends Controller {     public function anyConsole(Request $request, Auth $input)     {         //todo     } }

        我們來(lái)看下他是怎么實(shí)現(xiàn)自動(dòng)依賴注入的:

        由 index.PHP 調(diào)用 Kernel ,經(jīng)過多層 Kernel 管道調(diào)用,再到 Router ,經(jīng)過多層中間件管道調(diào)用。最終定位到

        Illuminate/Routing/Route.php 第124行。

        public function run(Request $request) {     $this->container = $this->container ?: new Container;     try {         if (! is_string($this->action['uses'])) {             return $this->runCallable($request);         }         if ($this->customDispatcherIsBound()) {             return $this->runWithCustomDispatcher($request);         }         return $this->runController($request);     } catch (HttpResponseException $e) {         return $e->getResponse();     } }

        判斷 $this->action[‘uses’](格式行如:AppHttpControllerDatacenterRealTimeController@anyConsole)是否字符串, $this->customDispatcherIsBound判斷是否綁定了用戶自定義路由。然后跳轉(zhuǎn)到 $this->runController($request)。

        protected function runController(Request $request) {     list($class, $method) = explode('@', $this->action['uses']);     $parameters = $this->resolveClassMethodDependencies(         $this->parametersWithoutNulls(), $class, $method     );     if (! method_exists($instance = $this->container->make($class), $method)) {         throw new NotFoundHttpException;     }     return call_user_func_array([$instance, $method], $parameters); }

        $this->resolveClassMethodDependencies 這個(gè)方法一看名字就知道是我們要找的方法。$this->parametersWithoutNulls()是過濾空字符,$class、$method分別行如:AppHttpControllerDatacenterRealTimeController 與 anyConsole。

        protected function resolveClassMethodDependencies(array $parameters, $instance, $method) {     if (! method_exists($instance, $method)) {         return $parameters;     }     return $this->resolveMethodDependencies(         $parameters, new ReflectionMethod($instance, $method)     ); }

        new ReflectionMethod($instance, $method) 是拿到類方法的反射對(duì)象,參見文檔:http://www.php.net/manual/zh/class.reflectionmethod.php

        下面跳轉(zhuǎn)到Illuminate/Routing/RouteDependencyResolverTrait.php 第54行。

        public function resolveMethodDependencies(array $parameters, ReflectionFunctionAbstract $reflector) {     $originalParameters = $parameters;     foreach ($reflector->getParameters() as $key => $parameter) {         $instance = $this->transformDependency(             $parameter, $parameters, $originalParameters         );         if (! is_null($instance)) {             $this->spliceIntoParameters($parameters, $key, $instance);         }     }     return $parameters; }

        通過反射類方法得到類參數(shù)數(shù)組,然后遍歷傳遞給 $this->transformDependency 方法。如果實(shí)例獲取不到則調(diào)用 $this->spliceIntoParameters 清楚該參數(shù)。

        protected function transformDependency(ReflectionParameter $parameter, $parameters, $originalParameters) {     $class = $parameter->getClass();     if ($class && ! $this->alreadyInParameters($class->name, $parameters)) {         return $this->container->make($class->name);     } }

        終于看到了容器的影子,沒錯(cuò)最終對(duì)象還是通過容器的 make 方法取出來(lái)的。至此參數(shù)就構(gòu)造好了,然后最終會(huì)被 runController 方法的 call_user_func_array 回調(diào)。

        總結(jié):

        依賴注入原理其實(shí)就是利用類方法反射,取得參數(shù)類型,然后利用容器構(gòu)造好實(shí)例。然后再使用回調(diào)函數(shù)調(diào)起。

        注入對(duì)象構(gòu)造函數(shù)不能有參數(shù)。否則會(huì)報(bào)錯(cuò)。Missing argument 1

        依賴注入故然好,但它必須要由 Router 類調(diào)起,否則直接用 new方式是無(wú)法實(shí)現(xiàn)注入的。所以這就為什么只有 Controller 、Job 類才能用這個(gè)特性了。

        贊(0)
        分享到: 更多 (0)
        網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)
        主站蜘蛛池模板: 无码精品人妻一区二区三区AV| 欧洲精品99毛片免费高清观看| 亚洲精品一品区二品区三品区| 国产亚洲欧洲精品| 亚洲国产主播精品极品网红| 91精品国产91热久久久久福利 | 婷婷精品国产亚洲AV麻豆不片| 亚洲精品国产精品乱码不99 | 污污网站国产精品白丝袜| 国产精品嫩草视频永久网址| 亚洲AV第一页国产精品| 免费精品视频在线| 国产精品日韩深夜福利久久| 国产亚洲精品a在线观看app| 久久精品a亚洲国产v高清不卡| 亚洲av无码成人精品区在线播放| 国内精品久久国产大陆| 97久久久久人妻精品专区| 日韩精品无码一区二区三区免费| 亚洲国产精品毛片av不卡在线| 久99久无码精品视频免费播放| 国产精品无码不卡一区二区三区| 亚洲国产精品久久66| 久草视频在线这里精品| 99久久人妻无码精品系列蜜桃| 亚洲AV无码久久精品狠狠爱浪潮| 中文字幕精品亚洲无线码一区应用| 亚欧乱色国产精品免费视频| 久久精品99无色码中文字幕| 国产精品自在线拍国产手机版| 99亚洲精品视频| 国产高清在线精品一本大道国产 | www夜片内射视频日韩精品成人| 精品久久777| 欧美成人精品一区二区三区| 久久精品成人国产午夜| 国产亚洲精品a在线观看app| 精品成人免费自拍视频| 青青草原综合久久大伊人精品| 91精品国产福利在线观看麻豆| 99九九精品免费视频观看|