The service locator is a solution to dependency injection. it contains dependency injection. After dependency injection is solved, if the service user and service provider are not using one person, therefore, the user must understand the parameters required to provide the service so as to ensure the correctness of the dependency. This is coupled with the user and the provider, and the service locator decouples this part, the service provider registers the service (with dependencies registered at the same time) in ServiceLocator. The service locator is a solution to dependency injection. it contains dependency injection. after resolving dependency injection, if the service user and the service provider are not using one person, the user must understand the parameters required to provide the service so as to ensure the correctness of the dependency, which is coupled with the user and the provider, the service locator decouples this part. the service provider registers the service in ServiceLocator (and registers dependencies at the same time) and only tells the service user the service name or alias, therefore, it is good for both service providers and users. users only need to know what services the provider provides, and do not have to know what services they depend on. service providers do not have to "use" the services indiscriminately. G.
So how does yii2 use ServiceLocator? In fact, it is as follows:
// Magic method, public function _ get ($ name) {// Obtain a registered method if ($ this-> has ($ name )) {return $ this-> get ($ name);} else {return parent ::__ get ($ name) ;}// magic method to check whether a service exists, the source code is called in public function _ isset ($ name) {}// _ isset () to check whether a service exists. the source code is public function has ($ id, $ checkInstance = false) {}// get a public function get ($ id, $ throwException = true) of a service) {if (isset ($ this-> _ components [$ id]) {// if the service has been processed, return $ this-> _ c Omponents [$ id];} if (isset ($ this-> _ definitions [$ id]) {// if the service $ definition = $ this-> _ definitions [$ id] is defined; // get the definition of the service // if the service is a closure, register the closure to the instantiated service and return the closure if (is_object ($ definition )&&! $ Definition instanceof Closure) {return $ this-> _ components [$ id] = $ definition;} else {// In other cases, the object is generated through dependency injection, and registered as handled, return object return $ this-> _ components [$ id] = Yii: createObject ($ definition) ;}} elseif ($ throwException) {// if an exception is thrown, throw new InvalidConfigException ("Unknown component ID: $ id") ;}else {// others return null ;}} // register a public function set ($ id, $ definition) {if ($ definition = null) {// if the definition of the service is null, delete the instantiated service and return an empty value to unset the definition of the instantiated and saved service ($ this-> _ components [$ id], $ this-> _ definitions [$ id]); return;} // clear the instantiated service unset ($ this-> _ components [$ id]); // if the service is defined as an object and is a callable structure if (is_object ($ definition) | is_callable ($ definition, true )) {// an object, a class name, or a PHP callable $ this-> _ definitions [$ id] = $ definition;} elseif (is_array ($ definition )) {// if the service is a configuration array // a configuration array if (isset ($ definition ['class']) {// if a class key value exists, then directly register as a Service definition $ this-> _ definitions [$ id] = $ definition;} else {// is a configuration array, but no class is specified, throw new InvalidConfigException ("The configuration for the \" $ id \ "component must contain a \" class \ "element. ") ;}} else {// Nothing. An exception is thrown. the error message returned when the service throw new InvalidConfigException (" Unexpected configuration type for the \ "$ id \" component :". gettype ($ definition);} // clears the instantiated services and Definitions. the code is slightly public function clear ($ id) {}// gets the instantiated service, or get the available service configuration public function getComponents ($ returnDefinitions = true) {}// register all services. here, $ components, the $ config ['components'] value you wrote in config is public function setComponents ($ components ){}
So when did ServiceLocator intervene? We continue to open our index. php. pay attention to the following sentence:
(new yii\web\Application($config))->run();
View the Application
Class Application extends \ yii \ base \ Application // continue tracing \ yii \ base \ Applicationabstract class Application extends Module // continue tracing Moduleclass Module extends ServiceLocator
Haha, I finally found it !!! Our application is actually a service locator. the components we configure in the configuration file are all services registered by the application service locator. Now I know why it is called the setComponents function.
Yii uses the set [typename] function to ensure the writability of attributes. yii: configure ($ this, $ config); this will call the setComponents function to register the service.
Well, we have all been in touch with each other. here is the dependency injection and service locator used by yii2.
The above is the yii2 article (7) dependency injection -- (4) service locator content. For more information, please follow the PHP Chinese network (www.php1.cn )!