php+laravel依賴注入淺析

語言: CN / TW / HK

laravel容器包含控制反轉和依賴注入,使用起來就是,先把物件bind好,需要時可以直接使用make來取就好。

通常我們的呼叫如下

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

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

但這跟依賴注入有什麼關係,真正的依賴注入是不需給方法傳遞任何引數值,只需要指明方法引數型別,程式碼自動查詢關係依賴自動注入。

這個特性在 laravel 的 Controller、Job 等處可以體現,如下:

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

我們來看下他是怎麼實現自動依賴注入的:

由 index.php 呼叫 Kernel ,經過多層 Kernel 管道呼叫,再到 Router ,經過多層中介軟體管道呼叫。最終定位到

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'](格式行如:\App\Http\Controller\Datacenter\RealTimeController@anyConsole)是否字串, $this->customDispatcherIsBound判斷是否綁定了使用者自定義路由。然後跳轉到 $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 這個方法一看名字就知道是我們要找的方法。$this->parametersWithoutNulls()是過濾空字元,$class、$method分別行如:\App\Http\Controller\Datacenter\RealTimeController 與 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) 是拿到類方法的反射物件,參見文件:http://www.php.net/manual/zh/class.reflectionmethod.php

下面跳轉到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;
}

通過反射類方法得到類引數陣列,然後遍歷傳遞給 $this->transformDependency 方法。如果例項獲取不到則呼叫 $this->spliceIntoParameters 清楚該引數。

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

終於看到了容器的影子,沒錯最終物件還是通過容器的 make 方法取出來的。至此引數就構造好了,然後最終會被 runController 方法的 call_user_func_array 回撥。

總結:
1. 依賴注入原理其實就是利用類方法反射,取得引數型別,然後利用容器構造好例項。然後再使用回撥函式調起。
2. 注入物件建構函式不能有引數。否則會報錯。Missing argument 1
3. 依賴注入故然好,但它必須要由 Router 類調起,否則直接用 new方式是無法實現注入的。所以這就為什麼只有 Controller 、Job 類才能用這個特性了。

以上內容希望幫助到大家,很多PHPer在進階的時候總會遇到一些問題和瓶頸,業務程式碼寫多了沒有方向感,更多PHP大廠PDF面試文件,PHP進階架構視訊資料,PHP精彩好文免費獲取可以關注公眾號:PHP開源社群,或者訪問:

2021金三銀四大廠面試真題集錦,必看!

四年精華PHP技術文章整理合集——PHP框架篇

四年精華PHP技術文合集——微服務架構篇

四年精華PHP技術文合集——分散式架構篇

四年精華PHP技術文合集——高併發場景篇

四年精華PHP技術文章整理合集——資料庫篇