'yii\mail\MessageInterface' updated:

- setter methods renamed to have pure name
- method 'createMessage' renamed to 'compose'
This commit is contained in:
Klimov Paul
2013-11-05 14:57:13 +02:00
parent c7e054789e
commit 87af95f712
9 changed files with 159 additions and 130 deletions

View File

@ -45,10 +45,18 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont
/**
* @var array configuration, which should be applied by default to any new created
* email message instance.
* In addition to normal [[Yii::createObject()]] behavior extra config keys are available:
* - 'from' invokes [[MessageInterface::from()]]
* - 'to' invokes [[MessageInterface::to()]]
* - 'cc' invokes [[MessageInterface::cc()]]
* - 'bcc' invokes [[MessageInterface::bcc()]]
* - 'subject' invokes [[MessageInterface::subject()]]
* - 'text' invokes [[MessageInterface::text()]]
* - 'html' invokes [[MessageInterface::html()]]
* For example:
* ~~~
* array(
* 'encoding' => 'UTF-8',
* 'charset' => 'UTF-8',
* 'from' => 'noreply@mydomain.com',
* 'bcc' => 'email.test@mydomain.com',
* )
@ -100,16 +108,37 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont
* Creates new message instance from given configuration.
* Message configuration will be merged with [[messageConfig]].
* If 'class' parameter is omitted [[messageClass]], will be used.
* @param array $config message configuration.
* @param array $config message configuration. See [[messageConfig]]
* for the configuration format details.
* @return MessageInterface message instance.
*/
public function createMessage(array $config = [])
public function compose(array $config = [])
{
$config = array_merge($this->messageConfig, $config);
if (!array_key_exists('class', $config)) {
$config['class'] = $this->messageClass;
}
return Yii::createObject($config);
$configMethodNames = [
'from',
'to',
'cc',
'bcc',
'subject',
'text',
'html',
];
$methodBasedConfig = [];
foreach ($config as $name => $value) {
if (in_array($name, $configMethodNames, true)) {
$methodBasedConfig[$name] = $value;
unset($config[$name]);
}
}
$message = Yii::createObject($config);
foreach ($methodBasedConfig as $name => $value) {
$message->$name($value);
}
return $message;
}
/**

View File

@ -22,13 +22,6 @@ use Yii;
*
* @property \yii\mail\BaseMailer $mailer mailer component instance. This property is read-only.
* @property string $charset the character set of this message.
* @property string|array $from sender email address.
* @property string|array $to receiver email address.
* @property string|array $cc copy receiver email address.
* @property string|array $bcc hidden copy receiver email address.
* @property string $subject message subject.
* @property string $text message plain text content.
* @property string $html message HTML content.
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0
@ -56,7 +49,7 @@ abstract class BaseMessage extends Object implements MessageInterface
*/
public function renderHtml($view, $params = [])
{
$this->setHtml($this->getMailer()->render($view, $params, $this->getMailer()->htmlLayout));
$this->html($this->getMailer()->render($view, $params, $this->getMailer()->htmlLayout));
return $this;
}
@ -65,7 +58,7 @@ abstract class BaseMessage extends Object implements MessageInterface
*/
public function renderText($view, $params = [])
{
$this->setText($this->getMailer()->render($view, $params, $this->getMailer()->textLayout));
$this->text($this->getMailer()->render($view, $params, $this->getMailer()->textLayout));
return $this;
}
}

View File

@ -22,7 +22,7 @@ interface MailerInterface
* @param array $config message configuration.
* @return MessageInterface message instance.
*/
public function createMessage(array $config = []);
public function compose(array $config = []);
/**
* Sends the given email message.

View File

@ -12,10 +12,10 @@ namespace yii\mail;
* Together with application component, which matches the [[MailerInterface]],
* it introduces following mail sending syntax:
* ~~~php
* Yii::$app->mail->createMessage()
* ->setFrom('from@domain.com')
* ->setTo('to@domain.com')
* ->setSubject('Message Subject')
* Yii::$app->mail->compose()
* ->from('from@domain.com')
* ->to('to@domain.com')
* ->subject('Message Subject')
* ->renderText('text/view')
* ->renderHtml('html/view')
* ->send();
@ -43,7 +43,7 @@ interface MessageInterface
* [email => name].
* @return static self reference.
*/
public function setFrom($from);
public function from($from);
/**
* Sets message receiver.
@ -53,7 +53,7 @@ interface MessageInterface
* [email => name].
* @return static self reference.
*/
public function setTo($to);
public function to($to);
/**
* Set the Cc (additional copy receiver) addresses of this message.
@ -63,7 +63,7 @@ interface MessageInterface
* [email => name].
* @return static self reference.
*/
public function setCc($cc);
public function cc($cc);
/**
* Set the Bcc (hidden copy receiver) addresses of this message.
@ -73,28 +73,28 @@ interface MessageInterface
* [email => name].
* @return static self reference.
*/
public function setBcc($bcc);
public function bcc($bcc);
/**
* Sets message subject.
* @param string $subject message subject
* @return static self reference.
*/
public function setSubject($subject);
public function subject($subject);
/**
* Sets message plain text content.
* @param string $text message plain text content.
* @return static self reference.
*/
public function setText($text);
public function text($text);
/**
* Sets message HTML content.
* @param string $html message HTML content.
* @return static self reference.
*/
public function setHtml($html);
public function html($html);
/**
* Attach specified content as file for the email message.