diff --git a/framework/yii/mail/BaseMailer.php b/framework/yii/mail/BaseMailer.php index ea7690b..e5a1dde 100644 --- a/framework/yii/mail/BaseMailer.php +++ b/framework/yii/mail/BaseMailer.php @@ -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 * @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. @@ -76,29 +77,6 @@ abstract class BaseMailer extends Component implements MailerInterface } /** - * @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. * @return \yii\base\View view instance. @@ -112,19 +90,6 @@ abstract class BaseMailer extends Component implements MailerInterface } /** - * 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]]. * If 'class' parameter is omitted [[messageClass]], will be used. @@ -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; } } \ No newline at end of file diff --git a/framework/yii/mail/ViewResolver.php b/framework/yii/mail/ViewResolver.php deleted file mode 100644 index 31f57cb..0000000 --- a/framework/yii/mail/ViewResolver.php +++ /dev/null @@ -1,57 +0,0 @@ - - * @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; - } -} \ No newline at end of file diff --git a/tests/unit/framework/mail/BaseMailerTest.php b/tests/unit/framework/mail/BaseMailerTest.php index ff37da2..cded1e4 100644 --- a/tests/unit/framework/mail/BaseMailerTest.php +++ b/tests/unit/framework/mail/BaseMailerTest.php @@ -85,33 +85,6 @@ class BaseMailerTest extends TestCase $this->assertTrue(is_object($view), 'Unable to get default view!'); } - public function testSetupViewResolver() - { - $mailer = new Mailer(); - - $viewResolver = new ViewResolver(); - $mailer->setViewResolver($viewResolver); - $this->assertEquals($viewResolver, $mailer->getViewResolver(), 'Unable to setup view resolver!'); - - $viewResolverConfig = [ - 'viewPath' => '/test/view/path', - ]; - $mailer->setViewResolver($viewResolverConfig); - $viewResolver = $mailer->getViewResolver(); - $this->assertTrue(is_object($viewResolver), 'Unable to setup view resolver via config!'); - $this->assertEquals($viewResolverConfig['viewPath'], $viewResolver->viewPath, 'Unable to configure view resolver via config array!'); - } - - /** - * @depends testSetupViewResolver - */ - public function testGetDefaultViewResolver() - { - $mailer = new Mailer(); - $viewResolver = $mailer->getViewResolver(); - $this->assertTrue(is_object($viewResolver), 'Unable to get default view resolver!'); - } - public function testCreateMessage() { $mailer = new Mailer(); @@ -152,14 +125,13 @@ class BaseMailerTest extends TestCase /** * @depends testGetDefaultView - * @depends testGetDefaultViewResolver */ public function testRender() { $mailer = new Mailer(); $filePath = $this->getTestFilePath(); - $mailer->getViewResolver()->viewPath = $filePath; + $mailer->viewPath = $filePath; $viewName = 'test_view'; $fileName = $filePath . DIRECTORY_SEPARATOR . $viewName . '.php'; diff --git a/tests/unit/framework/mail/ViewResolverTest.php b/tests/unit/framework/mail/ViewResolverTest.php deleted file mode 100644 index 5caa947..0000000 --- a/tests/unit/framework/mail/ViewResolverTest.php +++ /dev/null @@ -1,61 +0,0 @@ -testViewPath); - return [ - [ - $alias . '/test', - $aliasPath . '/test.php', - ], - [ - $alias . '/test.tpl', - $aliasPath . '/test.tpl', - ], - [ - 'contact/html', - $viewPath . '/contact/html.php', - ], - [ - 'contact/html.tpl', - $viewPath . '/contact/html.tpl', - ], - ]; - } - - /** - * @dataProvider dataProviderFindViewFile - * - * @param string $view - * @param string $expectedFileName - */ - public function testFindViewFile($view, $expectedFileName) - { - $viewResolver = new ViewResolver(); - $viewResolver->viewPath = $this->testViewPath; - $fileName = $viewResolver->findViewFile($view); - $this->assertEquals($expectedFileName, $fileName); - } -} \ No newline at end of file