diff --git a/docs/guide-zh-CN/concept-service-locator.md b/docs/guide-zh-CN/concept-service-locator.md index c9f278d..e86c791 100644 --- a/docs/guide-zh-CN/concept-service-locator.md +++ b/docs/guide-zh-CN/concept-service-locator.md @@ -1,15 +1,21 @@ 服务定位器 =============== -服务定位器是一个了解如何提供各种应用所需的服务(或组件)的对象。在服务定位器中,每个组件都只有一个单独的实例,并通过ID 唯一地标识。用这个 ID 就能从服务定位器中得到这个组件。 +服务定位器是一个了解如何提供各种应用所需的服务(或组件)的对象。在服务定位器中, +每个组件都只有一个单独的实例,并通过ID 唯一地标识。 +用这个 ID 就能从服务定位器中得到这个组件。 在 Yii 中,服务定位器是 [[yii\di\ServiceLocator]] 或其子类的一个实例。 -最常用的服务定位器是**application(应用)**对象,可以通过 `\Yii::$app` 访问。它所提供的服务被称为**application components(应用组件)**,比如:`request`、`response`、`urlManager` 组件。可以通过服务定位器所提供的功能,非常容易地配置这些组件,或甚至是用你自己的实现替换掉他们。 +最常用的服务定位器是**application(应用)**对象,可以通过 `\Yii::$app` 访问。 +它所提供的服务被称为**application components(应用组件)**, +比如:`request`、`response`、`urlManager` 组件。可以通过服务定位器所提供的功能, +非常容易地配置这些组件,或甚至是用你自己的实现替换掉他们。 除了 application 对象,每个模块对象本身也是一个服务定位器。 -要使用服务定位器,第一步是要注册相关组件。组件可以通过 [[yii\di\ServiceLocator::set()]] 方法进行注册。以下的方法展示了注册组件的不同方法: +要使用服务定位器,第一步是要注册相关组件。组件可以通过 [[yii\di\ServiceLocator::set()]] 方法进行注册。 +以下的方法展示了注册组件的不同方法: ```php use yii\di\ServiceLocator; @@ -45,11 +51,21 @@ $cache = $locator->get('cache'); $cache = $locator->cache; ``` -如上所示, [[yii\di\ServiceLocator]] 允许通过组件 ID 像访问一个属性值那样访问一个组件。当你第一次访问某组件时,[[yii\di\ServiceLocator]] 会通过该组件的注册信息创建一个该组件的实例,并返回它。之后,如果再次访问,则服务定位器会返回同一个实例。 +如上所示, [[yii\di\ServiceLocator]] 允许通过组件 ID 像访问一个属性值那样访问一个组件。 +当你第一次访问某组件时,[[yii\di\ServiceLocator]] +会通过该组件的注册信息创建一个该组件的实例,并返回它。 +之后,如果再次访问,则服务定位器会返回同一个实例。 -你可以通过 [[yii\di\ServiceLocator::has()]] 检查某组件 ID 是否被注册。若你用一个无效的 ID 调用 [[yii\di\ServiceLocator::get()]],则会抛出一个异常。 +你可以通过 [[yii\di\ServiceLocator::has()]] 检查某组件 ID 是否被注册。 +若你用一个无效的 ID 调用 [[yii\di\ServiceLocator::get()]],则会抛出一个异常。 + + +因为服务定位器,经常会在创建时附带[配置信息](concept-configurations.md), +因此我们提供了一个可写的属性,名为 [[yii\di\ServiceLocator::setComponents()|components]], +这样就可以配置该属性,或一次性注册多个组件。 +下面的代码展示了如何用一个配置数组,配置一个应用并注册 +"db","cache" 和 "search" 三个组件: -因为服务定位器,经常会在创建时附带[配置信息](concept-configurations.md),因此我们提供了一个可写的属性,名为 [[yii\di\ServiceLocator::setComponents()|components]],这样就可以配置该属性,或一次性注册多个组件。下面的代码展示了如何用一个配置数组,配置一个应用并注册"db","cache" 和 "search" 三个组件: ```php return [ // ... @@ -61,9 +77,44 @@ return [ 'password' => '', ], 'cache' => 'yii\caching\ApcCache', + 'tz' => function() { + return new \DateTimeZone(Yii::$app->formatter->defaultTimeZone); + }, 'search' => function () { - return new app\components\SolrService; + $solr = new app\components\SolrService('127.0.0.1'); + // ... other initializations ... + return $solr; }, ], ]; ``` + +In the above, there is an alternative way to configure the `search` component. Instead of directly writing a PHP +callback which builds a `SolrService` instance, you can use a static class method to return such a callback, like +shown as below: + +```php +class SolrServiceBuilder +{ + public static function build($ip) + { + return function () use ($ip) { + $solr = new app\components\SolrService($ip); + // ... other initializations ... + return $solr; + }; + } +} + +return [ + // ... + 'components' => [ + // ... + 'search' => SolrServiceBuilder::build('127.0.0.1'), + ], +]; +``` + +This alternative approach is most preferable when you are releasing a Yii component which encapsulates some non-Yii +3rd-party library. You use the static method like shown above to represent the complex logic of building the +3rd-party object, and the user of your component only needs to call the static method to configure the component.