Paul Klimov
11 years ago
5 changed files with 267 additions and 2 deletions
@ -0,0 +1,57 @@ |
|||||||
|
<?php |
||||||
|
/** |
||||||
|
* @link http://www.yiiframework.com/ |
||||||
|
* @copyright Copyright (c) 2008 Yii Software LLC |
||||||
|
* @license http://www.yiiframework.com/license/ |
||||||
|
*/ |
||||||
|
|
||||||
|
namespace yii\email; |
||||||
|
|
||||||
|
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; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,57 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace yiiunit\framework\email; |
||||||
|
|
||||||
|
use yii\email\ViewResolver; |
||||||
|
use Yii; |
||||||
|
use yiiunit\TestCase; |
||||||
|
|
||||||
|
/** |
||||||
|
* @group email |
||||||
|
*/ |
||||||
|
class ViewResolverTest extends TestCase |
||||||
|
{ |
||||||
|
/** |
||||||
|
* @var string test email view path. |
||||||
|
*/ |
||||||
|
protected $testViewPath = '@yiiunit/emails'; |
||||||
|
|
||||||
|
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); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue