Laravel 依赖注入源码解析
在 Laravel 的控制器的构造方法或者成员方法,都可以通过类型约束的方式使用依赖注入,如: public function store(Request $request) { //TODO } 这里 $request 参数就使用了类型约束,Request 是类型约束的类型,它是一个类:IlluminateHttpRequest. 本文研究 Laravel 的依赖注入原理,为什么这样定义不需要实例化就可以直接使用 Request 的方法呢?只是框架帮我们实例化并传参了,我们看看这个过程。 1.路由定义从源头开始看起,在路由定义文件中定义了这么一个路由: Route::resource('/role','AdminRoleController'); 这是一个资源型的路由,Laravel 会自动生成增删改查的路由入口。 路由方法解析根据路由定义找到控制器和方法,这个过程在 dispatch 方法中实现。 (文件:vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php) public function dispatch(Route $route,$controller,$method) { $parameters = $this->resolveClassMethodDependencies( $route->parametersWithoutNulls(),$method ); if (method_exists($controller,'callAction')) { return $controller->callAction($method,$parameters); } return $controller->{$method}(...array_values($parameters)); } 这里 resolveClassMethodDependencies 方法,“顾名思义”这个方法的作用是从类的方法中获取依赖对象: protected function resolveClassMethodDependencies(array $parameters,$instance,$method) { if (! method_exists($instance,$method)) { return $parameters; } return $this->resolveMethodDependencies( $parameters,new ReflectionMethod($instance,$method) ); } 这里重点就是用到了 PHP 的反射,注意 RelectionMethod 方法,它获取到类的方法参数列表,可以知道参数的类型约束,参数名称等等。 这里的 $instance 参数就是 RoleController 控制器类,$method 参数就是方法名称 strore. 2.获取依赖对象的示例从方法的参数中获取了依赖对象的约束类型,就可以实例化这个依赖的对象。 protected function transformDependency(ReflectionParameter $parameter,$parameters) { $class = $parameter->getClass(); // If the parameter has a type-hinted class,we will check to see if it is already in // the list of parameters. If it is we will just skip it as it is probably a model // binding and we do not want to mess with those; otherwise,we resolve it here. if ($class && ! $this->alreadyInParameters($class->name,$parameters)) { return $parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : $this->container->make($class->name); } } 根据类名从容器中获取对象,这个绑定对象实例的过程在服务提供者中先定义和了。 然后把实例化的对象传入到 store 方法中,就可以使用依赖的对象了。 3.关于 PHP 反射举个使用 ReflectionMethod 的例子。 class Demo { private $request; public function store(Request $request) { } } 打印出 new ReflectionMethod(Demo::class,‘store’) 的内容如图: 可以得出这个方法的参数列表,参数的约束类型,如 typeHint,IlluminateHttpRequest. 根据类名可以从容器中获取一开始通过服务提供者绑定的实例。 (原文地址:https://blog.tanteng.me/2018/...) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |