Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
e5e0de8c
Commit
e5e0de8c
authored
Oct 23, 2013
by
Paul Klimov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
'\yii\email\ViewResolver' added.
Email message render functionality added.
parent
88126bc4
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
271 additions
and
2 deletions
+271
-2
BaseMailer.php
framework/yii/email/BaseMailer.php
+54
-2
BaseMessage.php
framework/yii/email/BaseMessage.php
+15
-0
ViewResolver.php
framework/yii/email/ViewResolver.php
+58
-0
BaseMailerTest.php
tests/unit/framework/email/BaseMailerTest.php
+86
-0
ViewResolverTest.php
tests/unit/framework/email/ViewResolverTest.php
+58
-0
No files found.
framework/yii/email/BaseMailer.php
View file @
e5e0de8c
...
@@ -18,7 +18,8 @@ use Yii;
...
@@ -18,7 +18,8 @@ use Yii;
*
*
* @see BaseMessage
* @see BaseMessage
*
*
* @property array $view view instance or its array configuration.
* @property \yii\base\View|array $view view instance or its array configuration.
* @property \yii\email\ViewResolver|array $viewResolver view resolver instance or its array configuration.
* @property array $defaultMessageConfig configuration, which should be applied by default to any
* @property array $defaultMessageConfig configuration, which should be applied by default to any
* new created email message instance.
* new created email message instance.
*
*
...
@@ -32,6 +33,10 @@ abstract class BaseMailer extends Component
...
@@ -32,6 +33,10 @@ abstract class BaseMailer extends Component
*/
*/
private
$_view
=
[];
private
$_view
=
[];
/**
/**
* @var \yii\email\ViewResolver|array view resolver instance or its array configuration.
*/
private
$_viewResolver
=
[];
/**
* @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.
* For example:
* For example:
...
@@ -69,6 +74,29 @@ abstract class BaseMailer extends Component
...
@@ -69,6 +74,29 @@ abstract class BaseMailer extends Component
}
}
/**
/**
* @param array|\yii\email\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\email\ViewResolver view resolver.
*/
public
function
getViewResolver
()
{
if
(
!
is_object
(
$this
->
_viewResolver
))
{
$this
->
_viewResolver
=
$this
->
createViewResolver
(
$this
->
_viewResolver
);
}
return
$this
->
_viewResolver
;
}
/**
* @param array $defaultMessageConfig default message config
* @param array $defaultMessageConfig default message config
*/
*/
public
function
setDefaultMessageConfig
(
array
$defaultMessageConfig
)
public
function
setDefaultMessageConfig
(
array
$defaultMessageConfig
)
...
@@ -94,7 +122,19 @@ abstract class BaseMailer extends Component
...
@@ -94,7 +122,19 @@ abstract class BaseMailer extends Component
if
(
!
array_key_exists
(
'class'
,
$config
))
{
if
(
!
array_key_exists
(
'class'
,
$config
))
{
$config
[
'class'
]
=
'\yii\base\View'
;
$config
[
'class'
]
=
'\yii\base\View'
;
}
}
$config
[
'context'
]
=
$this
;
return
Yii
::
createObject
(
$config
);
}
/**
* Creates view resolver instance from given configuration.
* @param array $config view resolver configuration.
* @return \yii\email\ViewResolver view resolver instance.
*/
protected
function
createViewResolver
(
array
$config
)
{
if
(
!
array_key_exists
(
'class'
,
$config
))
{
$config
[
'class'
]
=
'\yii\email\ViewResolver'
;
}
return
Yii
::
createObject
(
$config
);
return
Yii
::
createObject
(
$config
);
}
}
...
@@ -122,4 +162,15 @@ abstract class BaseMailer extends Component
...
@@ -122,4 +162,15 @@ abstract class BaseMailer extends Component
}
}
return
$successCount
;
return
$successCount
;
}
}
/**
* Renders a view.
* @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 string string the rendering result
*/
public
function
render
(
$view
,
$params
=
[])
{
return
$this
->
getView
()
->
renderFile
(
$this
->
getViewResolver
()
->
findViewFile
(
$view
),
$params
,
$this
);
}
}
}
\ No newline at end of file
framework/yii/email/BaseMessage.php
View file @
e5e0de8c
...
@@ -134,4 +134,18 @@ abstract class BaseMessage extends Object
...
@@ -134,4 +134,18 @@ abstract class BaseMessage extends Object
$content
=
file_get_contents
(
$fileName
);
$content
=
file_get_contents
(
$fileName
);
$this
->
createAttachment
(
$content
,
$attachFileName
,
$contentType
);
$this
->
createAttachment
(
$content
,
$attachFileName
,
$contentType
);
}
}
/**
* Renders a view.
* 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.
* @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file.
* @return string string the rendering result
*/
public
function
render
(
$view
,
$params
=
[])
{
return
$this
->
getMailer
()
->
render
(
$view
,
$params
);
}
}
}
\ No newline at end of file
framework/yii/email/ViewResolver.php
0 → 100644
View file @
e5e0de8c
<?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
;
}
}
\ No newline at end of file
tests/unit/framework/email/BaseMailerTest.php
View file @
e5e0de8c
...
@@ -6,6 +6,8 @@ use Yii;
...
@@ -6,6 +6,8 @@ use Yii;
use
yii\base\View
;
use
yii\base\View
;
use
yii\email\BaseMailer
;
use
yii\email\BaseMailer
;
use
yii\email\BaseMessage
;
use
yii\email\BaseMessage
;
use
yii\email\ViewResolver
;
use
yii\helpers\FileHelper
;
use
yiiunit\TestCase
;
use
yiiunit\TestCase
;
/**
/**
...
@@ -17,6 +19,26 @@ class BaseMailerTest extends TestCase
...
@@ -17,6 +19,26 @@ class BaseMailerTest extends TestCase
{
{
$this
->
mockApplication
();
$this
->
mockApplication
();
Yii
::
$app
->
setComponent
(
'email'
,
$this
->
createTestEmailComponent
());
Yii
::
$app
->
setComponent
(
'email'
,
$this
->
createTestEmailComponent
());
$filePath
=
$this
->
getTestFilePath
();
if
(
!
file_exists
(
$filePath
))
{
FileHelper
::
createDirectory
(
$filePath
);
}
}
public
function
tearDown
()
{
$filePath
=
$this
->
getTestFilePath
();
if
(
file_exists
(
$filePath
))
{
FileHelper
::
removeDirectory
(
$filePath
);
}
}
/**
* @return string test file path.
*/
protected
function
getTestFilePath
()
{
return
Yii
::
getAlias
(
'@yiiunit/runtime'
)
.
DIRECTORY_SEPARATOR
.
basename
(
get_class
(
$this
))
.
'_'
.
getmypid
();
}
}
/**
/**
...
@@ -37,8 +59,22 @@ class BaseMailerTest extends TestCase
...
@@ -37,8 +59,22 @@ class BaseMailerTest extends TestCase
$view
=
new
View
();
$view
=
new
View
();
$mailer
->
setView
(
$view
);
$mailer
->
setView
(
$view
);
$this
->
assertEquals
(
$view
,
$mailer
->
getView
(),
'Unable to setup view!'
);
$this
->
assertEquals
(
$view
,
$mailer
->
getView
(),
'Unable to setup view!'
);
$viewConfig
=
[
'params'
=>
[
'param1'
=>
'value1'
,
'param2'
=>
'value2'
,
]
];
$mailer
->
setView
(
$viewConfig
);
$view
=
$mailer
->
getView
();
$this
->
assertTrue
(
is_object
(
$view
),
'Unable to setup view via config!'
);
$this
->
assertEquals
(
$viewConfig
[
'params'
],
$view
->
params
,
'Unable to configure view via config array!'
);
}
}
/**
* @depends testSetupView
*/
public
function
testGetDefaultView
()
public
function
testGetDefaultView
()
{
{
$mailer
=
new
Mailer
();
$mailer
=
new
Mailer
();
...
@@ -46,6 +82,33 @@ class BaseMailerTest extends TestCase
...
@@ -46,6 +82,33 @@ 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
testDefaultMessageConfig
()
public
function
testDefaultMessageConfig
()
{
{
$defaultMessageConfig
=
array
(
$defaultMessageConfig
=
array
(
...
@@ -60,6 +123,29 @@ class BaseMailerTest extends TestCase
...
@@ -60,6 +123,29 @@ class BaseMailerTest extends TestCase
$this
->
assertEquals
(
$value
,
$message
->
$name
);
$this
->
assertEquals
(
$value
,
$message
->
$name
);
}
}
}
}
/**
* @depends testGetDefaultView
* @depends testGetDefaultViewResolver
*/
public
function
testRender
()
{
$mailer
=
new
Mailer
();
$filePath
=
$this
->
getTestFilePath
();
$mailer
->
getViewResolver
()
->
viewPath
=
$filePath
;
$viewName
=
'test_view'
;
$fileName
=
$filePath
.
DIRECTORY_SEPARATOR
.
$viewName
.
'.php'
;
$fileContent
=
'<?php echo $testParam; ?>'
;
file_put_contents
(
$fileName
,
$fileContent
);
$params
=
[
'testParam'
=>
'test output'
];
$renderResult
=
$mailer
->
render
(
$viewName
,
$params
);
$this
->
assertEquals
(
$params
[
'testParam'
],
$renderResult
);
}
}
}
/**
/**
...
...
tests/unit/framework/email/ViewResolverTest.php
0 → 100644
View file @
e5e0de8c
<?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
);
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment