Commit 35429fbd by Paul Klimov

'yii\mail\ViewResolve' removed.

Interface 'ViewContextInterface' applied to BaseMailer.
parent 5bc22a07
...@@ -10,6 +10,7 @@ namespace yii\mail; ...@@ -10,6 +10,7 @@ namespace yii\mail;
use yii\base\Component; use yii\base\Component;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use Yii; use Yii;
use yii\base\ViewContextInterface;
/** /**
* BaseMailer provides the basic interface for the email mailer application component. * BaseMailer provides the basic interface for the email mailer application component.
...@@ -24,16 +25,16 @@ use Yii; ...@@ -24,16 +25,16 @@ use Yii;
* @author Paul Klimov <klimov.paul@gmail.com> * @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0 * @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. * @var \yii\base\View|array view instance or its array configuration.
*/ */
private $_view = []; 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 * @var array configuration, which should be applied by default to any new created
* email message instance. * email message instance.
...@@ -76,29 +77,6 @@ abstract class BaseMailer extends Component implements MailerInterface ...@@ -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. * Creates view instance from given configuration.
* @param array $config view configuration. * @param array $config view configuration.
* @return \yii\base\View view instance. * @return \yii\base\View view instance.
...@@ -112,19 +90,6 @@ abstract class BaseMailer extends Component implements MailerInterface ...@@ -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. * Creates new message instance from given configuration.
* Message configuration will be merged with [[messageConfig]]. * Message configuration will be merged with [[messageConfig]].
* If 'class' parameter is omitted [[messageClass]], will be used. * If 'class' parameter is omitted [[messageClass]], will be used.
...@@ -167,6 +132,16 @@ abstract class BaseMailer extends Component implements MailerInterface ...@@ -167,6 +132,16 @@ abstract class BaseMailer extends Component implements MailerInterface
*/ */
public function render($view, $params = []) 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
<?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;
}
}
\ No newline at end of file
...@@ -85,33 +85,6 @@ class BaseMailerTest extends TestCase ...@@ -85,33 +85,6 @@ class BaseMailerTest extends TestCase
$this->assertTrue(is_object($view), 'Unable to get default view!'); $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() public function testCreateMessage()
{ {
$mailer = new Mailer(); $mailer = new Mailer();
...@@ -152,14 +125,13 @@ class BaseMailerTest extends TestCase ...@@ -152,14 +125,13 @@ class BaseMailerTest extends TestCase
/** /**
* @depends testGetDefaultView * @depends testGetDefaultView
* @depends testGetDefaultViewResolver
*/ */
public function testRender() public function testRender()
{ {
$mailer = new Mailer(); $mailer = new Mailer();
$filePath = $this->getTestFilePath(); $filePath = $this->getTestFilePath();
$mailer->getViewResolver()->viewPath = $filePath; $mailer->viewPath = $filePath;
$viewName = 'test_view'; $viewName = 'test_view';
$fileName = $filePath . DIRECTORY_SEPARATOR . $viewName . '.php'; $fileName = $filePath . DIRECTORY_SEPARATOR . $viewName . '.php';
......
<?php
namespace yiiunit\framework\mail;
use yii\mail\ViewResolver;
use Yii;
use yiiunit\TestCase;
/**
* @group mail
*/
class ViewResolverTest extends TestCase
{
/**
* @var string test email view path.
*/
protected $testViewPath = '@yiiunit/emails';
/**
* Data provider for [[testFindViewFile()]]
* @return array test data.
*/
public function dataProviderFindViewFile()
{
$alias = '@yiiunit';
$aliasPath = Yii::getAlias($alias);
$viewPath = Yii::getAlias($this->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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment