本篇文章给大家带来了关于laravel的相关知识,其中主要介绍了关于容器、控制反转以及依赖注入的相关问题,下面就一起来看一下什么相关的内容,希望对大家有帮助。
推荐学习:laravel入门
随着现在应用的规模越来越庞大,对象之间的依赖关系也越来越复杂,耦合程度越来越高,经常会出现对象之间多重依赖的情况。对于如此庞大复杂的应用,任何修改都可能会牵一发而动全身,这就为应用的后期维护造成了很多困扰。
为了解决对象之间耦合度高的问题,控制反转(ioc)的思想也随之诞生。所谓控制反转,是面向对象编程中的一种设计原则,其目的是为了降低代码之间的耦合程度。在 laravel 中,控制反转是通过依赖注入(di)的方式实现的。
控制反转的基本思想是借助 ioc 容器实现对象之间的依赖关系的解耦。引入 ioc 容器之后,所有对象的控制权都上交给 ioc 容器,ioc 容器成了整个系统的核心,把所有对象粘合在一起发挥作用。laravel 中的容器即起到了这个作用。
⒈ 容器 所谓容器,在 laravel 中指的是 \illuminate\foundation\application 对象,laravel 框架在启动时即创建了该对象。
# public/index.php$app = require_once __dir__.'/../bootstrap/app.php';# bootstrap/app.php$app = new illuminate\foundation\application( $_env['app_base_path'] dirname(__dir__));
在创建容器的过程中,laravel 还会对容器进行一些基础的绑定和服务注册。laravel 首先会将容器实例与 app 和 illuminate\container\container 进行绑定;之后,laravel 会将基础的服务提供者注册到容器实例中,包括事件、日志、路由服务提供者;最后,laravel 会将框架核心 class 与其相对应的别名一起注册到容器实例当中。
// namespace illuminate\foundation\applicationpublic function __construct($basepath = null){ if ($basepath) { $this->setbasepath($basepath); } $this->registerbasebindings(); $this->registerbaseserviceproviders(); $this->registercorecontaineraliases();} protected function registerbasebindings(){ static::setinstance($this); $this->instance('app', $this); $this->instance(container::class, $this); /* ... ... */}protected function registerbaseserviceproviders(){ $this->register(new eventserviceprovider($this)); $this->register(new logserviceprovider($this)); $this->register(new routingserviceprovider($this));}public function registercorecontaineraliases(){ foreach ([ 'app' => [self::class, \illuminate\contracts\container\container::class, \illuminate\contracts\foundation\application::class, \psr\container\containerinterface::class], /* ... ...*/ 'db' => [\illuminate\database\databasemanager::class, \illuminate\database\connectionresolverinterface::class], 'db.connection' => [\illuminate\database\connection::class, \illuminate\database\connectioninterface::class], /* ... ... */ 'request' => [\illuminate\http\request::class, \symfony\component\httpfoundation\request::class], 'router' => [\illuminate\routing\router::class, \illuminate\contracts\routing\registrar::class, \illuminate\contracts\routing\bindingregistrar::class], /* ... ... */ ] as $key => $aliases) { foreach ($aliases as $alias) { $this->alias($key, $alias); } }}// namespace illuminate\container\containerpublic function alias($abstract, $alias){ if ($alias === $abstract) { throw new logicexception("[{$abstract}] is aliased to itself."); } $this->aliases[$alias] = $abstract; $this->abstractaliases[$abstract][] = $alias;}
在完成这三步基本的注册之后,我们可以很方便的访问已经注册到容器中的对象实例。例如,可以直接通过 $app['app'] 或 $app['illuminate\container\container'] 访问容器本身,还可以通过 $app['db'] 直接访问数据库连接。
⒉ 服务提供者的注册以及服务的访问注册服务提供者
在容器创建的过程中会注册基础服务提供者,其注册过程通过调用 register() 方法完成。
// namespace illuminate\foundation\applicationpublic function register($provider, $force = false){ if (($registered = $this->getprovider($provider)) && ! $force) { return $registered; } if (is_string($provider)) { $provider = $this->resolveprovider($provider); } $provider->register(); if (property_exists($provider, 'bindings')) { foreach ($provider->bindings as $key => $value) { $this->bind($key, $value); } } if (property_exists($provider, 'singletons')) { foreach ($provider->singletons as $key => $value) { $this->singleton($key, $value); } } $this->markasregistered($provider); if ($this->isbooted()) { $this->bootprovider($provider); } return $provider;}
laravel 首先会判断指定的服务提供者是否已经在容器中注册(通过调用 getprovider() 方法实现),如果指定的服务提供者已经在容器中注册,并且本次注册操作并非强制执行,那么直接返回已经注册好的服务提供者。
如果不满足上述条件,那么 laravel 就会开始注册服务提供者。此时,如果传参为字符串,那么 laravel 会默认参数为服务提供者的 class 名称并进行实例化(通过 resolveprovider() 方法实现)。之后,就会调用服务提供者定义的 register() 方法进行注册。以日志服务提供者为例,其 register() 方法的方法体如下
// namespace illuminate\log\logserviceproviderpublic function register(){ $this->app->singleton('log', function ($app) { return new logmanager($app); });}
register() 方法的作用就是将 illuminate\log\logmanager 对象以单例的模式注册到容器当中,注册完成之后,容器的 $bindings 属性中会增加一项
$app->bindings['log'] = [ 'concrete' => 'illuminate\log\logmanager {#162}', 'shared' => true,];
如果服务提供者自身还定义了 $bindings 属性以及 $singletons 属性,那么 laravel 还会调用相应的 bind() 方法和 singleton() 方法完成这些服务提供者自定义的绑定的注册。
这之后 laravel 会将服务提供者标记为已经注册的状态,随后会调用服务提供者定义的 boot() 方法启动服务提供者(前提是应用已经启动)。
在向容器中注册绑定时,有 bind() 和 singleton() 两种方法,其区别仅在于注册的绑定是否为单例模式,即 shared 属性是否为 true 。
// namespace illuminate\container\containerpublic function singleton($abstract, $concrete = null){ $this->bind($abstract, $concrete, true);}public function bind($abstract, $concrete = null, $shared = false){ // 删除旧的绑定 $this->dropstaleinstances($abstract); if (is_null($concrete)) { $concrete = $abstract; } if (! $concrete instanceof closure) { if (! is_string($concrete)) { throw new typeerror(self::class.'::bind(): argument #2 ($concrete) must be of type closure|string|null'); } $concrete = $this->getclosure($abstract, $concrete); } $this->bindings[$abstract] = compact('concrete', 'shared'); if ($this->resolved($abstract)) { $this->rebound($abstract); }}protected function getclosure($abstract, $concrete){ return function ($container, $parameters = []) use ($abstract, $concrete) { if ($abstract == $concrete) { return $container->build($concrete); } return $container->resolve( $concrete, $parameters, $raiseevents = false ); };}
仍然以日志服务提供者为例,日志服务提供者在注册时以单例模式进行注册,并且 $concrete 参数为闭包。在绑定开始之前,laravel 首先会删除旧的绑定。由于此时 $concrete 为闭包,所以 laravel 并不会进行什么操作,只是将绑定信息存入 $bindings 属性当中。
访问服务
在服务提供者注册完成之后,我们可以用上文提到的类似访问数据库连接的方式那样访问服务。仍然以日志服务为例,我们可以通过 $app['log'] 的方式访问日志服务。另外,在 laravel 中,我们还可以使用 facade 的方式访问服务,例如,我们可以调用 illuminate\support\facades\log::info() 来记录日志。
// namespace illuminate\support\facades\logclass log extends facade{ protected static function getfacadeaccessor() { return 'log'; }}// namespace illuminate\support\facades\facadepublic static function __callstatic($method, $args){ $instance = static::getfacaderoot(); /* ... ... */ return $instance->$method(...$args);}public static function getfacaderoot(){ return static::resolvefacadeinstance(static::getfacadeaccessor());}protected static function resolvefacadeinstance($name){ if (is_object($name)) { return $name; } if (isset(static::$resolvedinstance[$name])) { return static::$resolvedinstance[$name]; } if (static::$app) { return static::$resolvedinstance[$name] = static::$app[$name]; }}
在通过静态调用的方式进行日志记录时,首先会访问 facade 中的魔术方法 __callstatic() ,该方法的首先进行的就是解析出 facade 对应的服务实例,然后调用该服务实例下的方法来执行相应的功能。每个 facade 中都会定义一个 getfacadeaccessor() 方法,这个方法会返回一个 tag,在日志服务中,这个 tag 就是日志服务提供者的闭包在容器的 $bindings 属性中的 key。也就是说,通过 facade 方式最终得到的是 $app['log']。
那么为什么可以通过关联数组的方式访问容器中注册的对象/服务?illuminate\container\container 实现了 arrayaccess 并且定义了 offsetget() 方法,而 illuminate\foundation\application 继承了 container ,$app 为 application 实例化的对象,所以通过关联数组的方式访问容器中注册的对象时会访问 container 的 offsetget() 方法。在 offsetget() 方法中会调用 container 的 make() 方法,而 make() 方法中又会调用 resolve() 方法。resolve() 方法最终会解析并返回相应的对象。
// namespace illuminate\containerpublic function offsetget($key){ return $this->make($key);}public function make($abstract, array $parameters = []){ return $this->resolve($abstract, $parameters);}protected function resolve($abstract, $parameters = [], $raiseevents = true){ /* ... ... */ $this->with[] = $parameters; if (is_null($concrete)) { $concrete = $this->getconcrete($abstract); } if ($this->isbuildable($concrete, $abstract)) { $object = $this->build($concrete); } else { $object = $this->make($concrete); } /* ... ... */ $this->resolved[$abstract] = true; array_pop($this->with); return $object;}protected function getconcrete($abstract){ if (isset($this->bindings[$abstract])) { return $this->bindings[$abstract]['concrete']; } return $abstract;}protected function isbuildable($concrete, $abstract){ return $concrete === $abstract || $concrete instanceof closure;}public function build($concrete){ if ($concrete instanceof closure) { return $concrete($this, $this->getlastparameteroverride()); } /* ... ... */}protected function getlastparameteroverride(){ return count($this->with) ? end($this->with) : [];}
这里需要说明,在通过 $app['log'] 的方式解析日志服务实例时,resolve() 方法中的 $concrete 解析得到的是一个闭包,导致 isbuildable() 方法返回结果为 true,所以 laravel 会直接调用 build() 方法。而由于此时 $concrete 是一个闭包,所以在 build() 方法中会直接执行这个闭包函数,最终返回 logmanager 实例。
⒊ 请求处理 在基础的绑定和服务注册完成之后,容器创建成功并返回 $app 。之后 laravel 会将内核(包括 http 内核和 console 内核)和异常处理注册到容器当中。然后 laravel 开始处理请求。
// namespace bootstrap/app.php$app->singleton( illuminate\contracts\http\kernel::class, app\http\kernel::class);$app->singleton( illuminate\contracts\console\kernel::class, app\console\kernel::class);$app->singleton( illuminate\contracts\debug\exceptionhandler::class, app\exceptions\handler::class);// public/index.php$kernel = $app->make(illuminate\contracts\http\kernel::class);$response = $kernel->handle( $request = request::capture())->send();$kernel->terminate($request, $response);
在开始处理请求之前,laravel 首先会解析出 http 内核对象 $kernel,即 app\http\kernel 实例化的对象。而 app\http\kernel 继承了 illuminate\foundation\kernel,所以 $kernel 实际调用的是 illuminate\foundation\kernel 中的 handle() 方法。
namespace illuminate\foundation\httpuse illuminate\contracts\debug\exceptionhandlerpublic function handle($request){ try { $request->enablehttpmethodparameteroverride(); $response = $this->sendrequestthroughrouter($request); } catch (throwable $e) { $this->reportexception($e); $response = $this->renderexception($request, $e); } $this->app['events']->dispatch( new requesthandled($request, $response) ); return $response;}// 上报错误protected function reportexception(throwable $e){ $this->app[exceptionhandler::class]->report($e);}// 渲染错误信息 protected function renderexception($request, throwable $e){ return $this->app[exceptionhandler::class]->render($request, $e);}
handle() 方法在处理请求的过程中如果出现任何异常或错误,laravel 都会调用容器中已经注册好的异常处理对象来上报异常并且渲染返回信息。
在容器创建成功以后,laravel 会将 illuminate\contracts\debug\exceptionhandler 和 app\exceptions\handler 之间的绑定注册到容器当中,所以 laravel 处理异常实际调用的都是 app\exceptions\handler 中的方法。在实际开发过程中,开发者可以根据自身需要在 app\exceptions\handler 中自定义 report() 和 render() 方法。
在 php 7 中,`exception` 和 `error` 是两种不同的类型,但它们同时都继承了 `throwable` ,所以 `handler()` 方法中捕获的是 `throwable` 对象。
在正式开始处理请求之前,laravel 会进行一些引导启动,包括加载环境变量、配置信息等,这些引导启动在 laravel 运行过程中起到了非常重要的作用。
// namespace illuminate\foundation\http\kernelprotected $bootstrappers = [ \illuminate\foundation\bootstrap\loadenvironmentvariables::class, \illuminate\foundation\bootstrap\loadconfiguration::class, \illuminate\foundation\bootstrap\handleexceptions::class, \illuminate\foundation\bootstrap\registerfacades::class, \illuminate\foundation\bootstrap\registerproviders::class, \illuminate\foundation\bootstrap\bootproviders::class,];protected function sendrequestthroughrouter($request){ /* ... ... */ $this->bootstrap(); /* ... ... */}public function bootstrap(){ if (! $this->app->hasbeenbootstrapped()) { $this->app->bootstrapwith($this->bootstrappers()); }}// namespace illuminate\foundation\applicationpublic function bootstrapwith(array $bootstrappers){ $this->hasbeenbootstrapped = true; foreach ($bootstrappers as $bootstrapper) { $this['events']->dispatch('bootstrapping: '.$bootstrapper, [$this]); $this->make($bootstrapper)->bootstrap($this); $this['events']->dispatch('bootstrapped: '.$bootstrapper, [$this]); }}
从代码中可以看出,引导启动的过程实际就是调用各个 class 中的 bootstrap() 方法。其中:
loadenvironmentvariables 用来加载环境变量
loadconfiguration 用来加载 config 目录下的配置文件
handleexceptions 用来设置 php 的错误报告级别以及相应的异常和错误处理函数,另外还会设置 php 的程序终止执行函数
// namespace illuminate\foundation\bootstrap\handleexceptionspublic 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']); /* ... ... */}public function handleerror($level, $message, $file = '', $line = 0, $context = []){ if (error_reporting() & $level) { /* ... ... */ throw new errorexception($message, 0, $level, $file, $line); }}public function handleexception(throwable $e){ /* ... ... */ $this->getexceptionhandler()->report($e); /* ... ... */}public function handleshutdown(){ if (! is_null($error = error_get_last()) && $this->isfatal($error['type'])) { $this->handleexception($this->fatalerrorfromphperror($error, 0)); }}protected function getexceptionhandler(){ return $this->app->make(\illuminate\contracts\debug\exceptionhandler::class);}
从以上代码中可以看出,虽然 handleexceptions 中定义了异常、错误、程序终止的处理函数,但无论是哪种情况,最终还是调用 app\exceptions\handler 中的方法来处理异常或错误。
registerfacades 的作用一个是注册配置文件以及第三方包中自定义的 alias 类,还有一个非常重要的作用就是为 illuminate\support\facades\facade 类设置 $app 属性。
// namespace illuminate\foundation\bootstrap\registerfacadespublic function bootstrap(application $app){ facade::clearresolvedinstances(); facade::setfacadeapplication($app); aliasloader::getinstance(array_merge( $app->make('config')->get('app.aliases', []), $app->make(packagemanifest::class)->aliases() ))->register();}
&emsp 我们在通过 facade 方式反问容器中注册的服务时,facade 在解析容器中的服务实例时用到的 static::$app 即是在这个时候设置的。
registerproviders 的作用是注册配置文件以及第三方包中定义的服务提供者
// namespace illuminate\foundation\bootstrap\registerproviderspublic function bootstrap(application $app){ $app->registerconfiguredproviders();}public function registerconfiguredproviders(){ $providers = collection::make($this->make('config')->get('app.providers')) ->partition(function ($provider) { return strpos($provider, 'illuminate\\') === 0; }); $providers->splice(1, 0, [$this->make(packagemanifest::class)->providers()]); (new providerrepository($this, new filesystem, $this->getcachedservicespath())) ->load($providers->collapse()->toarray());}
在实际注册的过程中,laravel 会按照 laravel 框架的服务提供者 > 第三方包的服务提供者 > 开发者自定义的服务提供者 的顺序进行注册
bootproviders 则是按顺序调用已经注册到容器中的服务提供者的 boot() 方法(前提是服务提供者定义的 boot() 方法)
在引导启动完成之后,laravel 开始处理请求,首先要做的就是将全局的中间件应用于 request 。这之后 laravel 会将请求分发到相应的路由进行处理,处理之前需要先根据 request 找到相应的路由对象 illuminate\routing\route。在 laravel 中,除了全局中间件,还有一些中间件只作用于特定的路由或路由分组,此时这些中间件就会被作用于 request 。这些工作都完成之后,路由对象开始执行代码,完成请求。
// namespace illuminate\foundation\http\kernelprotected function sendrequestthroughrouter($request){ /* ... ... */ return (new pipeline($this->app)) ->send($request) ->through($this->app->shouldskipmiddleware() ? [] : $this->middleware) ->then($this->dispatchtorouter());}protected function dispatchtorouter(){ return function ($request) { $this->app->instance('request', $request); return $this->router->dispatch($request); };}// namespace illuminate\routing\routerpublic function dispatch(request $request){ $this->currentrequest = $request; return $this->dispatchtoroute($request);}public function dispatchtoroute(request $request){ return $this->runroute($request, $this->findroute($request));}protected function runroute(request $request, route $route){ /* ... ... */ return $this->prepareresponse($request, $this->runroutewithinstack($route, $request) );}protected function runroutewithinstack(route $route, request $request){ /* ... ... */ return (new pipeline($this->container)) ->send($request) ->through($middleware) ->then(function ($request) use ($route) { return $this->prepareresponse( $request, $route->run() ); });}
⒋ 依赖注入 laravel 中的路由在注册时,action 可以是控制器方法,也可以是闭包。但无论是那种形式,都需要传参,而传参就会遇到需要依赖注入的情况。
route 对象在执行 run() 方法时会根据 action 的类型分别进行控制器方法调用或闭包函数的调用。但两种方法最终都需要解析参数,而如果参数中用到了 class ,就需要进行依赖注入。
// namespace illuminate\routing\routerpublic function run(){ $this->container = $this->container ?: new container; try { if ($this->iscontrolleraction()) { return $this->runcontroller(); } return $this->runcallable(); } catch (httpresponseexception $e) { return $e->getresponse(); }}protected function runcontroller(){ return $this->controllerdispatcher()->dispatch( $this, $this->getcontroller(), $this->getcontrollermethod() );}protected function runcallable(){ /* ... ... */ return $callable(...array_values($this->resolvemethoddependencies( $this->parameterswithoutnulls(), new reflectionfunction($callable) )));}// namespace illuminate\routing\controllerdispatcherpublic function dispatch(route $route, $controller, $method){ $parameters = $this->resolveclassmethoddependencies( $route->parameterswithoutnulls(), $controller, $method ); /* ... ... */}// namespace illuminate\routing\routedependencyresolvertraitprotected function resolveclassmethoddependencies(array $parameters, $instance, $method){ /* ... ... */ return $this->resolvemethoddependencies( $parameters, new reflectionmethod($instance, $method) );}public function resolvemethoddependencies(array $parameters, reflectionfunctionabstract $reflector){ /* ... ... */ foreach ($reflector->getparameters() as $key => $parameter) { $instance = $this->transformdependency($parameter, $parameters, $skippablevalue); /* ... ... */ } return $parameters;}protected function transformdependency(reflectionparameter $parameter, $parameters, $skippablevalue){ $classname = reflector::getparameterclassname($parameter); if ($classname && ! $this->alreadyinparameters($classname, $parameters)) { return $parameter->isdefaultvalueavailable() ? null : $this->container->make($classname); } return $skippablevalue;}
在执行过程中,laravel 首先通过反射取得参数列表(对于控制器方法,使用 reflectionmethod ,对于闭包函数,则使用 reflectionfunction )。在得到参数列表后,laravel 仍然是利用反射,逐个判断参数类型。如果参数类型为 php 的内置类型,那么不需要什么特殊处理;但如果参数不是 php 内置类型,则需要利用反射解析出参数的具体类型。在解析出参数的具体类型之后,紧接着会判断该类型的对象是不是已经存在于参数列表中,如果不存在并且该类型也没有设置默认值,那么就需要通过容器创建出该类型的实例。
要通过容器创建指定 class 的实例,仍然需要用到 resolve() 方法。前文已经叙述过使用 resolve() 方法解析闭包函数的情况,所以这里值叙述实例化 class 的情况。
// namespace illuminate\container\containerpublic function build($concrete){ /* ... ... */ try { $reflector = new reflectionclass($concrete); } catch (reflectionexception $e) { throw new bindingresolutionexception("target class [$concrete] does not exist.", 0, $e); } if (! $reflector->isinstantiable()) { return $this->notinstantiable($concrete); } $this->buildstack[] = $concrete; $constructor = $reflector->getconstructor(); if (is_null($constructor)) { array_pop($this->buildstack); return new $concrete; } $dependencies = $constructor->getparameters(); try { $instances = $this->resolvedependencies($dependencies); } catch (bindingresolutionexception $e) { array_pop($this->buildstack); throw $e; } array_pop($this->buildstack); return $reflector->newinstanceargs($instances);}protected function resolvedependencies(array $dependencies){ $results = []; foreach ($dependencies as $dependency) { if ($this->hasparameteroverride($dependency)) { $results[] = $this->getparameteroverride($dependency); continue; } $result = is_null(util::getparameterclassname($dependency)) ? $this->resolveprimitive($dependency) : $this->resolveclass($dependency); if ($dependency->isvariadic()) { $results = array_merge($results, $result); } else { $results[] = $result; } } return $results;}
容器在实例化 class 的时候,仍然是通过反射获取 class 基本信息。对于一些无法进行实例化的 class (例如 interface 、abstract class ),laravel 会抛出异常;否则 laravel 会继续获取 class 的构造函数的信息。对于不存在构造函数的 class ,意味着这些 class 在实例化的时候不需要额外的依赖,可以直接通过 new 来实例化;否则仍然是通过反射解析出构造函数的参数列表信息,然后逐个实例化这些参数列表中用到的 class 。在这些参数列表中的 class 都实例化完成之后,通过容器创建 class 的准备工作也已经完成,此时容器可以顺利创建出指定 class 的实例,然后注入到控制器方法或闭包中。
推荐学习:laravel入门
以上就是laravel实例详解之容器、控制反转和依赖注入的详细内容。
