'yii\mail\ViewResolve' removed.

Interface 'ViewContextInterface' applied to BaseMailer.
This commit is contained in:
Paul Klimov
2013-10-29 12:34:08 +02:00
parent 5bc22a077f
commit 35429fbd44
4 changed files with 16 additions and 187 deletions

View File

@@ -10,6 +10,7 @@ namespace yii\mail;
use yii\base\Component;
use yii\base\InvalidConfigException;
use Yii;
use yii\base\ViewContextInterface;
/**
* BaseMailer provides the basic interface for the email mailer application component.
@@ -24,16 +25,16 @@ use Yii;
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0
*/
abstract class BaseMailer extends Component implements MailerInterface
abstract class BaseMailer extends Component implements MailerInterface, ViewContextInterface
{
/**
* @var \yii\base\View|array view instance or its array configuration.
*/
private $_view = [];
/**
* @var \yii\mail\ViewResolver|array view resolver instance or its array configuration.
* @var string directory containing view files for this email messages.
*/
private $_viewResolver = [];
public $viewPath = '@app/mailviews';
/**
* @var array configuration, which should be applied by default to any new created
* email message instance.
@@ -75,29 +76,6 @@ abstract class BaseMailer extends Component implements MailerInterface
return $this->_view;
}
/**
* @param array|\yii\mail\ViewResolver $viewResolver view resolver instance or its array configuration.
* @throws \yii\base\InvalidConfigException on invalid argument.
*/
public function setViewResolver($viewResolver)
{
if (!is_array($viewResolver) && !is_object($viewResolver)) {
throw new InvalidConfigException('"' . get_class($this) . '::viewResolver" should be either object or array, "' . gettype($viewResolver) . '" given.');
}
$this->_viewResolver = $viewResolver;
}
/**
* @return \yii\mail\ViewResolver view resolver.
*/
public function getViewResolver()
{
if (!is_object($this->_viewResolver)) {
$this->_viewResolver = $this->createViewResolver($this->_viewResolver);
}
return $this->_viewResolver;
}
/**
* Creates view instance from given configuration.
* @param array $config view configuration.
@@ -111,19 +89,6 @@ abstract class BaseMailer extends Component implements MailerInterface
return Yii::createObject($config);
}
/**
* Creates view resolver instance from given configuration.
* @param array $config view resolver configuration.
* @return \yii\mail\ViewResolver view resolver instance.
*/
protected function createViewResolver(array $config)
{
if (!array_key_exists('class', $config)) {
$config['class'] = '\yii\mail\ViewResolver';
}
return Yii::createObject($config);
}
/**
* Creates new message instance from given configuration.
* Message configuration will be merged with [[messageConfig]].
@@ -167,6 +132,16 @@ abstract class BaseMailer extends Component implements MailerInterface
*/
public function render($view, $params = [])
{
return $this->getView()->renderFile($this->getViewResolver()->findViewFile($view), $params, $this);
return $this->getView()->render($view, $params, $this);
}
/**
* Finds the view file corresponding to the specified relative view name.
* @param string $view a relative view name. The name does NOT start with a slash.
* @return string the view file path. Note that the file may not exist.
*/
public function findViewFile($view)
{
return Yii::getAlias($this->viewPath) . DIRECTORY_SEPARATOR . $view;
}
}

View File

@@ -1,57 +0,0 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\mail;
use yii\base\Component;
use Yii;
/**
* ViewResolver handles the search for the view files, which are rendered
* by email messages.
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0
*/
class ViewResolver extends Component
{
/**
* @var string directory containing view files for this email messages.
*/
public $viewPath = '@app/emails';
/**
* Finds the view file based on the given view name.
* The view to be rendered can be specified in one of the following formats:
* - path alias (e.g. "@app/emails/contact/body");
* - relative path (e.g. "contact"): the actual view file will be resolved by [[resolveView]].
* @param string $view the view name or the path alias of the view file.
* @return string the view file path. Note that the file may not exist.
*/
public function findViewFile($view)
{
if (strncmp($view, '@', 1) === 0) {
// e.g. "@app/views/main"
$file = Yii::getAlias($view);
} else {
$file = $this->resolveView($view);
}
return pathinfo($file, PATHINFO_EXTENSION) === '' ? $file . '.php' : $file;
}
/**
* Composes file name for the view name, appending view name to [[viewPath]].
* Child classes may override this method to provide more sophisticated
* search of the view files or even composition of the view files "on the fly".
* @param string $view the view name.
* @return string the view file path.
*/
protected function resolveView($view)
{
return Yii::getAlias($this->viewPath) . DIRECTORY_SEPARATOR . $view;
}
}