如何修改上述课程以符合我的标准?
你不能。身份证不见了。除非您没有至少与相关消息相关联的 ID(即幕后发生的事件的消息),否则您将无法创建Class Swift_Plugins_DecoratorPlugin
提供它自己的_Replacements
接口的具体子类型,提供要求更换时的消息。
让我们创建自己的插件,它仍然是替换的装饰器插件:
<?php
interface My_Swift_Plugins_Decorator_Replacements extends Swift_Plugins_Decorator_Replacements
{
public function setMessage(Swift_Mime_Message $message);
}
class My_Swift_Plugins_DecoratorPlugin extends Swift_Plugins_DecoratorPlugin implements My_Swift_Plugins_Decorator_Replacements
{
private $_replacements;
public function __construct(My_Swift_Plugins_Decorator_Replacements $replacements) {
$this->_replacements = $replacements;
}
/**
* Invoked immediately before the Message is sent.
*
* @param Swift_Events_SendEvent $evt
*/
public function beforeSendPerformed(Swift_Events_SendEvent $evt) {
$this->setMessage($evt->getMessage());
parent::beforeSendPerformed($evt);
}
public function setMessage(Swift_Mime_Message $message) {
$this->_replacements->setMessage($message);
}
}
如果您将 ID 分配给插件中的消息,您将在调用之前获得消息集。getReplacementsFor
然后,您可以将其分配给一个属性并在该函数中读取它。
class DbReplacements implements My_Swift_Plugins_Decorator_Replacements {
private $message;
public function setMessage(Swift_Mime_Message $message) {
$this->message = $message;
}
public function getReplacementsFor($address) {
$sql = sprintf(
"SELECT * FROM user WHERE email = '%s' and id = '%d'",
mysql_real_escape_string($address),
$this->message->emailID;
);
$result = mysql_query($sql);
if ($row = mysql_fetch_assoc($result)) {
return array(
'{username}'=>$row['username'],
'{password}'=>$row['password']
);
}
}
}