Skip to content

Commit

Permalink
Update Email class to use ViewBuilder.
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Aug 16, 2015
1 parent b73e3f6 commit cae86b6
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 111 deletions.
163 changes: 60 additions & 103 deletions src/Mailer/Email.php
Expand Up @@ -23,6 +23,7 @@
use Cake\Network\Http\FormData\Part;
use Cake\Utility\Hash;
use Cake\Utility\Text;
use Cake\View\ViewVarsTrait;
use Closure;
use Exception;
use InvalidArgumentException;
Expand Down Expand Up @@ -51,6 +52,7 @@ class Email implements JsonSerializable, Serializable
{

use StaticConfigTrait;
use ViewVarsTrait;

/**
* Line length - no should more - RFC 2822 - 2.1.1
Expand Down Expand Up @@ -182,48 +184,6 @@ class Email implements JsonSerializable, Serializable
*/
protected $_headers = [];

/**
* Layout for the View
*
* @var string
*/
protected $_layout = 'default';

/**
* Template for the view
*
* @var string
*/
protected $_template = '';

/**
* View for render
*
* @var string
*/
protected $_viewRender = 'Cake\View\View';

/**
* Vars to sent to render
*
* @var array
*/
protected $_viewVars = [];

/**
* Theme for the View
*
* @var string
*/
protected $_theme = null;

/**
* Helpers to be used in the render
*
* @var array
*/
protected $_helpers = ['Html'];

/**
* Text message
*
Expand Down Expand Up @@ -379,6 +339,12 @@ public function __construct($config = null)
$this->_domain = php_uname('n');
}

$this->viewBuilder()
->className('Cake\View\View')
->template('')
->layout('default')
->helpers(['Html']);

if ($config) {
$this->profile($config);
}
Expand Down Expand Up @@ -879,13 +845,13 @@ public function template($template = false, $layout = false)
{
if ($template === false) {
return [
'template' => $this->_template,
'layout' => $this->_layout
'template' => $this->viewBuilder()->template(),
'layout' => $this->viewBuilder()->layout()
];
}
$this->_template = $template;
$this->viewBuilder()->template($template ?: '');
if ($layout !== false) {
$this->_layout = $layout;
$this->viewBuilder()->layout($layout ?: false);
}
return $this;
}
Expand All @@ -899,9 +865,9 @@ public function template($template = false, $layout = false)
public function viewRender($viewClass = null)
{
if ($viewClass === null) {
return $this->_viewRender;
return $this->viewBuilder()->className();
}
$this->_viewRender = $viewClass;
$this->viewBuilder()->className($viewClass);
return $this;
}

Expand All @@ -914,9 +880,9 @@ public function viewRender($viewClass = null)
public function viewVars($viewVars = null)
{
if ($viewVars === null) {
return $this->_viewVars;
return $this->viewVars;
}
$this->_viewVars = array_merge($this->_viewVars, (array)$viewVars);
$this->set((array)$viewVars);
return $this;
}

Expand All @@ -929,9 +895,9 @@ public function viewVars($viewVars = null)
public function theme($theme = null)
{
if ($theme === null) {
return $this->_theme;
return $this->viewBuilder()->theme();
}
$this->_theme = $theme;
$this->viewBuilder()->theme($theme);
return $this;
}

Expand All @@ -944,9 +910,9 @@ public function theme($theme = null)
public function helpers($helpers = null)
{
if ($helpers === null) {
return $this->_helpers;
return $this->viewBuilder()->helpers();
}
$this->_helpers = (array)$helpers;
$this->viewBuilder()->helpers((array)$helpers, false);
return $this;
}

Expand Down Expand Up @@ -1407,37 +1373,44 @@ protected function _applyConfig($config)
}
unset($name);
}

$this->_profile = array_merge($this->_profile, $config);
if (!empty($config['charset'])) {
$this->charset = $config['charset'];
}
if (!empty($config['headerCharset'])) {
$this->headerCharset = $config['headerCharset'];
}
if (empty($this->headerCharset)) {
$this->headerCharset = $this->charset;
}

$simpleMethods = [
'from', 'sender', 'to', 'replyTo', 'readReceipt', 'returnPath', 'cc', 'bcc',
'messageId', 'domain', 'subject', 'viewRender', 'viewVars', 'attachments',
'transport', 'emailFormat', 'theme', 'helpers', 'emailPattern'
'from', 'sender', 'to', 'replyTo', 'readReceipt', 'returnPath',
'cc', 'bcc', 'messageId', 'domain', 'subject', 'attachments',
'transport', 'emailFormat', 'emailPattern', 'charset', 'headerCharset'
];
foreach ($simpleMethods as $method) {
if (isset($config[$method])) {
$this->$method($config[$method]);
unset($config[$method]);
}
}

if (empty($this->headerCharset)) {
$this->headerCharset = $this->charset;
}
if (isset($config['headers'])) {
$this->setHeaders($config['headers']);
unset($config['headers']);
}

if (array_key_exists('template', $config)) {
$this->_template = $config['template'];
$viewBuilderMethods = [
'template', 'layout', 'theme'
];
foreach ($viewBuilderMethods as $method) {
if (array_key_exists($method, $config)) {
$this->viewBuilder()->$method($config[$method]);
}
}

if (array_key_exists('helpers', $config)) {
$this->viewBuilder()->helpers($config['helpers'], false);
}
if (array_key_exists('layout', $config)) {
$this->_layout = $config['layout'];
if (array_key_exists('viewRender', $config)) {
$this->viewBuilder()->className($config['viewRender']);
}
if (array_key_exists('viewVars', $config)) {
$this->set($config['viewVars']);
}
}

Expand All @@ -1459,12 +1432,6 @@ public function reset()
$this->_messageId = true;
$this->_subject = '';
$this->_headers = [];
$this->_layout = 'default';
$this->_template = '';
$this->_viewRender = 'Cake\View\View';
$this->_viewVars = [];
$this->_theme = null;
$this->_helpers = ['Html'];
$this->_textMessage = '';
$this->_htmlMessage = '';
$this->_message = '';
Expand All @@ -1475,6 +1442,14 @@ public function reset()
$this->_attachments = [];
$this->_profile = [];
$this->_emailPattern = self::EMAIL_PATTERN;

$this->viewBuilder()->layout('default');
$this->viewBuilder()->template('');
$this->viewBuilder()->classname('Cake\View\View');
$this->viewVars = [];
$this->viewBuilder()->theme(false);
$this->viewBuilder()->helpers(['Html'], false);

return $this;
}

Expand Down Expand Up @@ -1828,31 +1803,18 @@ protected function _renderTemplates($content)
{
$types = $this->_getTypes();
$rendered = [];
if (empty($this->_template)) {
if (empty($this->viewBuilder()->template())) {
foreach ($types as $type) {
$rendered[$type] = $this->_encodeString($content, $this->charset);
}
return $rendered;
}
$viewClass = $this->_viewRender;
if ($viewClass === 'View') {
$viewClass = App::className('View', 'View');
} else {
$viewClass = App::className($viewClass, 'View', 'View');
}

$View = new $viewClass(null);
$View->viewVars = $this->_viewVars;
$View->helpers = $this->_helpers;

if ($this->_theme) {
$View->theme = $this->_theme;
}

$View = $this->createView();
$View->loadHelpers();

list($templatePlugin) = pluginSplit($this->_template);
list($layoutPlugin) = pluginSplit($this->_layout);
list($templatePlugin) = pluginSplit($View->view());
list($layoutPlugin) = pluginSplit($View->layout());
if ($templatePlugin) {
$View->plugin = $templatePlugin;
} elseif ($layoutPlugin) {
Expand All @@ -1863,17 +1825,12 @@ protected function _renderTemplates($content)
$View->set('content', $content);
}

// Convert null to false, as View needs false to disable
// the layout.
if ($this->_layout === null) {
$this->_layout = false;
}

foreach ($types as $type) {
$View->hasRendered = false;
$View->viewPath = $View->layoutPath = 'Email/' . $type;
$View->viewPath('Email/' . $type);
$View->layoutPath('Email/' . $type);

$render = $View->render($this->_template, $this->_layout);
$render = $View->render();
$render = str_replace(["\r\n", "\r"], "\n", $render);
$rendered[$type] = $this->_encodeString($render, $this->charset);
}
Expand Down
7 changes: 4 additions & 3 deletions src/View/ViewVarsTrait.php
Expand Up @@ -14,6 +14,7 @@
namespace Cake\View;

use Cake\Core\App;
use Cake\Event\EventManagerTrait;
use Cake\View\ViewBuilder;

/**
Expand Down Expand Up @@ -106,9 +107,9 @@ public function createView($viewClass = null)

return $builder->build(
$this->viewVars,
$this->request,
$this->response,
$this->eventManager(),
isset($this->request) ? $this->request : null,
isset($this->response) ? $this->response : null,
$this instanceof EventManagerTrait ? $this->eventManager() : null,
$viewOptions
);
}
Expand Down
11 changes: 6 additions & 5 deletions tests/TestCase/Mailer/EmailTest.php
Expand Up @@ -682,11 +682,11 @@ public function testTemplate()
$this->assertSame($expected, $this->CakeEmail->template());

$this->CakeEmail->template('template', null);
$expected = ['template' => 'template', 'layout' => null];
$expected = ['template' => 'template', 'layout' => false];
$this->assertSame($expected, $this->CakeEmail->template());

$this->CakeEmail->template(null, null);
$expected = ['template' => null, 'layout' => null];
$expected = ['template' => '', 'layout' => false];
$this->assertSame($expected, $this->CakeEmail->template());
}

Expand Down Expand Up @@ -717,7 +717,7 @@ public function testViewVars()
$this->assertSame(['value' => 12345], $this->CakeEmail->viewVars());

$this->CakeEmail->viewVars(['name' => 'CakePHP']);
$this->assertSame(['value' => 12345, 'name' => 'CakePHP'], $this->CakeEmail->viewVars());
$this->assertEquals(['value' => 12345, 'name' => 'CakePHP'], $this->CakeEmail->viewVars());

$this->CakeEmail->viewVars(['value' => 4567]);
$this->assertSame(['value' => 4567, 'name' => 'CakePHP'], $this->CakeEmail->viewVars());
Expand Down Expand Up @@ -1875,7 +1875,7 @@ public function testDeliver()
$this->assertSame($instance->to(), ['debug@cakephp.org' => 'debug@cakephp.org']);
$this->assertSame($instance->subject(), 'Update ok');
$this->assertSame($instance->template(), ['template' => 'custom', 'layout' => 'custom_layout']);
$this->assertSame($instance->viewVars(), ['value' => 123, 'name' => 'CakePHP']);
$this->assertEquals($instance->viewVars(), ['value' => 123, 'name' => 'CakePHP']);
$this->assertSame($instance->cc(), ['cake@cakephp.org' => 'Myself']);

$configs = ['from' => 'root@cakephp.org', 'message' => 'Message from configs', 'transport' => 'debug'];
Expand Down Expand Up @@ -1938,7 +1938,7 @@ public function testReset()

$this->CakeEmail->reset();
$this->assertSame([], $this->CakeEmail->to());
$this->assertNull($this->CakeEmail->theme());
$this->assertFalse($this->CakeEmail->theme());
$this->assertSame(Email::EMAIL_PATTERN, $this->CakeEmail->emailPattern());
}

Expand Down Expand Up @@ -2712,6 +2712,7 @@ public function testJsonSerialize()
];
$this->assertEquals($expected, $result);

debug(unserialize(serialize($this->CakeEmail)));
$result = json_decode(json_encode(unserialize(serialize($this->CakeEmail))), true);
$this->assertContains('test', $result['_viewVars']['exception']);
unset($result['_viewVars']['exception']);
Expand Down

0 comments on commit cae86b6

Please sign in to comment.