Render methods removed from 'yii\mail\MessageInterface'.

Method 'yii\mail\MailerInterface::compose()' reworked allowing rendering message body.
This commit is contained in:
Paul Klimov
2013-11-07 12:18:07 +02:00
parent ad7761f96a
commit 1aafa73e15
8 changed files with 89 additions and 202 deletions

View File

@@ -33,7 +33,7 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont
/**
* @var string directory containing view files for this email messages.
*/
public $viewPath = '@app/mailviews';
public $viewPath = '@app/mails';
/**
* @var string HTML layout view name.
*/
@@ -54,17 +54,12 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont
* - 'subject' argument for [[MessageInterface::subject()]]
* - 'text' argument for [[MessageInterface::text()]]
* - 'html' argument for [[MessageInterface::html()]]
* - 'body' argument for [[MessageInterface::body()]]
* - 'renderText' list of arguments for [[MessageInterface::renderText()]]
* - 'renderHtml' list of arguments for [[MessageInterface::renderHtml()]]
* - 'renderBody' list of arguments for [[MessageInterface::renderBody()]]
* For example:
* ~~~
* array(
* 'charset' => 'UTF-8',
* 'from' => 'noreply@mydomain.com',
* 'bcc' => 'email.test@mydomain.com',
* 'renderText' => ['default/text', ['companyName' => 'YiiApp']],
* 'bcc' => 'developer@mydomain.com',
* )
* ~~~
*/
@@ -111,16 +106,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. See [[messageConfig]]
* for the configuration format details.
* @inheritdoc
*/
public function compose($view = null, array $params = [])
{
$message = $this->createMessage();
if ($view !== null) {
$params['message'] = $message;
if (is_array($view)) {
if (array_key_exists('html', $view)) {
$message->html($this->render($view['html'], $params, $this->htmlLayout));
}
if (array_key_exists('text', $view)) {
$message->text($this->render($view['text'], $params, $this->textLayout));
}
} else {
$html = $this->render($view, $params, $this->htmlLayout);
$message->html($html);
$message->text(strip_tags($html));
}
}
return $message;
}
/**
* Creates mew message instance using configuration from [[messageConfig]].
* If 'class' parameter is omitted, [[messageClass]] will be used.
* @return MessageInterface message instance.
*/
public function message(array $config = [])
protected function createMessage()
{
$config = array_merge($this->messageConfig, $config);
$config = $this->messageConfig;
if (!array_key_exists('class', $config)) {
$config['class'] = $this->messageClass;
}
@@ -133,32 +149,18 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont
'subject',
'text',
'html',
'body',
];
$setupMethodNames = [
'renderText',
'renderHtml',
'renderBody',
];
$directSetterConfig = [];
$setupMethodConfig = [];
foreach ($config as $name => $value) {
if (in_array($name, $directSetterNames, true)) {
$directSetterConfig[$name] = $value;
unset($config[$name]);
}
if (in_array($name, $setupMethodNames, true)) {
$setupMethodConfig[$name] = $value;
unset($config[$name]);
}
}
$message = Yii::createObject($config);
foreach ($directSetterConfig as $name => $value) {
$message->$name($value);
}
foreach ($setupMethodConfig as $method => $arguments) {
call_user_func_array(array($message, $method), $arguments);
}
return $message;
}

View File

@@ -43,54 +43,6 @@ abstract class BaseMessage extends Object implements MessageInterface
return $this->getMailer()->send($this);
}
/**
* @inheritdoc
*/
public function body($body)
{
if (is_array($body)) {
$this->html($body['html']);
$this->text($body['text']);
} else {
$this->html($body);
$this->text(strip_tags($body));
}
return $this;
}
/**
* @inheritdoc
*/
public function renderHtml($view, $params = [])
{
$this->html($this->getMailer()->render($view, $params, $this->getMailer()->htmlLayout));
return $this;
}
/**
* @inheritdoc
*/
public function renderText($view, $params = [])
{
$this->text($this->getMailer()->render($view, $params, $this->getMailer()->textLayout));
return $this;
}
/**
* @inheritdoc
*/
public function renderBody($view, $params = [])
{
if (is_array($view)) {
$this->renderHtml($view['html'], $params);
$this->renderText($view['text'], $params);
} else {
$html = $this->getMailer()->render($view, $params, $this->getMailer()->htmlLayout);
$this->body($html);
}
return $this;
}
/**
* PHP magic method that returns the string representation of this object.
* @return string the string representation of this object.

View File

@@ -18,11 +18,18 @@ namespace yii\mail;
interface MailerInterface
{
/**
* Creates new message instance from given configuration.
* @param array $config message configuration.
* Creates new message optionally filling up its body via view rendering.
* The view to be rendered can be specified in one of the following formats:
* - path alias (e.g. "@app/mails/contact/body");
* - relative path (e.g. "contact"): the actual view file will be resolved by [[\yii\base\ViewContextInterface]].
* @param string|array $view view, which should be used to render message body
* - if string - the view name or the path alias of the HTML body view file, in this case
* text body will be composed automatically from html one.
* - if array - list of views for each body type in format: ['html' => 'htmlView', 'text' => 'textView']
* @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file.
* @return MessageInterface message instance.
*/
public function message(array $config = []);
public function compose($view = null, array $params = []);
/**
* Sends the given email message.

View File

@@ -96,16 +96,6 @@ interface MessageInterface
*/
public function html($html);
/**
* Sets message HTML and plain text content.
* @param string|array $body varies method behavior depending on type:
* - string - the HTML body content, in this case text body will be composed from
* html one using [[strip_tags()]] function.
* - array - list of body contents for each body type in format: ['html' => 'htmlContent', 'text' => 'textContent']
* @return static self reference.
*/
public function body($body);
/**
* Attaches existing file to the email message.
* @param string $fileName full file name
@@ -154,39 +144,6 @@ interface MessageInterface
*/
public function send();
/**
* Fills up HTML body rendering a view.
* The view to be rendered can be specified in one of the following formats:
* - path alias (e.g. "@app/mails/contact/body");
* - relative path (e.g. "contact"): the actual view file will be resolved by [[\yii\base\ViewContextInterface]].
* @param string $view the view name or the path alias of the view file.
* @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file.
* @return static self reference.
*/
public function renderHtml($view, $params = []);
/**
* Fills up plain text body rendering a view.
* The view to be rendered can be specified in one of the following formats:
* - path alias (e.g. "@app/mails/contact/body");
* - relative path (e.g. "contact"): the actual view file will be resolved by [[\yii\base\ViewContextInterface]].
* @param string $view the view name or the path alias of the view file.
* @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file.
* @return static self reference.
*/
public function renderText($view, $params = []);
/**
* Composes the message HTML and plain text body.
* @param string|array $view varies method behavior depending on type:
* - string - the view name or the path alias of the HTML body view file, in this case
* text body will be composed from html one using [[strip_tags()]] function.
* - array - list of views for each body type in format: ['html' => 'htmlView', 'text' => 'textView']
* @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file.
* @return static self reference.
*/
public function renderBody($view, $params = []);
/**
* Returns string representation of this message.
* @return string the string representation of this message.