11.2 依赖注入和服务
注入服务
上文定义服务时映射了服务名和服务类,这个服务也是完全独立的。如果我们想创建一个使用其他服务的服务,那么必须注入这些依赖(在 services.yml 文件里定义这些依赖)。我们更新下之前定义的 di_example.services.yml 文件,创建一个使用 current_user
和 di_example.mood_ring
服务 的 di_example.talk 服务。
services:
# A service that will let us get a mood.
di_example.mood_ring:
class: Drupal\di_example\DIMoodRing
# A service that will let us talk to users.
di_example.talk:
class: Drupal\di_example\DITalk
# We can inject the current user and mood ring services into this service.
arguments: ['@current_user', '@di_example.mood_ring']
这个服务提供一个 getResponseToMood() 方法,返回一个字符串信息。这个服务也会首先创建 __construct() 方法,把注入的服务存储为类属性。像这样:
public function __construct(AccountProxy $CurrentUser, DIMoodRing $DIMoodRing) {
src/DITalk.php(见辅助内容区)
当 PhpStorm 识别出类名时,如果需要它会自动添加 use 语句。AccountProxy 需要,因为它使用了不同名字空间,但 DIMoodRing 不需要,因为它正使用与我们模块同样的名字空间。
完成 __construct() 方法后,用 /** 添加一个注释。PhpStorm 会自动提供参数文档,链接到它们的完整类路径。如果你定义类属性,同样添加注释的话,PhpStorm 会提供属性类型的文档。
<?php
/**
* @file Contains \Drupal\di_example\DITalk
*/
namespace Drupal\di_example;
use Drupal\Core\Session\AccountProxy;
/**
* A service that provides a system for getting response message.
*/
class DITalk {
protected $responsesToMood = [
'Very Sad' => 'I hope you feel better.',
'Sad' => 'are you ok?',
'So-so' => 'good morning.',
'Happy' => 'what\'s Up?',
'Very Happy' => 'you seem happy today!',
];
/**
* @var \Drupal\di_example\DIMoodRing
*/
protected $dIMoodRing;
/**
* @var \Drupal\Core\Session\AccountProxy
*/
protected $currentUser;
/**
* We will inject our two services and store them for use in our service methods.
*
* @param \Drupal\Core\Session\AccountProxy $CurrentUser
* @param \Drupal\di_example\DIMoodRing $DIMoodRing
*/
public function __construct(AccountProxy $CurrentUser, DIMoodRing $DIMoodRing) {
$this->currentUser = $CurrentUser;
$this->dIMoodRing = $DIMoodRing;
}
/**
* Returns a string that is a message to a user.
*
* @return string
*/
public function getResponseToMood() {
// We can user our services and their defined methods.
$username = $this->currentUser->getUsername();
$mood = $this->dIMoodRing->getMood();
// We build a message to return.
return $username . ', ' . $this->responsesToMood[$mood];
}
}