Skip to content

Commit

Permalink
Add ViewBuilder serializer.
Browse files Browse the repository at this point in the history
This helps serialiazing Email objects along with config for view.
  • Loading branch information
ADmad committed Aug 17, 2015
1 parent cae86b6 commit 4eecd3a
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 17 deletions.
15 changes: 10 additions & 5 deletions src/Mailer/Email.php
Expand Up @@ -1891,12 +1891,12 @@ protected function _getContentTypeCharset()
public function jsonSerialize()
{
$properties = [
'_to', '_from', '_sender', '_replyTo', '_cc', '_bcc', '_subject', '_returnPath', '_readReceipt',
'_template', '_layout', '_viewRender', '_viewVars', '_theme', '_helpers', '_emailFormat',
'_emailPattern', '_attachments', '_domain', '_messageId', '_headers', 'charset', 'headerCharset',
'_to', '_from', '_sender', '_replyTo', '_cc', '_bcc', '_subject',
'_returnPath', '_readReceipt', '_emailFormat', '_emailPattern', '_domain',
'_attachments', '_messageId', '_headers', 'viewVars', 'charset', 'headerCharset'
];

$array = [];
$array = ['viewConfig' => $this->viewBuilder()->jsonSerialize()];

foreach ($properties as $property) {
$array[$property] = $this->{$property};
Expand All @@ -1909,7 +1909,7 @@ public function jsonSerialize()
}
});

array_walk_recursive($array['_viewVars'], [$this, '_checkViewVars']);
array_walk_recursive($array['viewVars'], [$this, '_checkViewVars']);

return array_filter($array, function ($i) {
return !is_array($i) && strlen($i) || !empty($i);
Expand Down Expand Up @@ -1950,6 +1950,11 @@ protected function _checkViewVars(&$item, $key)
*/
public function createFromArray($config)
{
if (isset($config['viewConfig'])) {
$this->viewBuilder()->createFromArray($config['viewConfig']);
unset($config['viewConfig']);
}

foreach ($config as $property => $value) {
$this->{$property} = $value;
}
Expand Down
65 changes: 64 additions & 1 deletion src/View/ViewBuilder.php
Expand Up @@ -20,14 +20,16 @@
use Cake\Network\Response;
use Cake\View\Exception\MissingViewException;
use Cake\View\View;
use JsonSerializable;
use Serializable;

/**
* Provides an API for iteratively building a view up.
*
* Once you have configured the view and established all the context
* you can create a view instance with `build()`.
*/
class ViewBuilder
class ViewBuilder implements JsonSerializable, Serializable
{
/**
* The subdirectory to the template.
Expand Down Expand Up @@ -338,4 +340,65 @@ public function build($vars = [], Request $request = null, Response $response =
$data += $this->_options;
return new $className($request, $response, $events, $data);
}

/**
* Serializes the view builder object to a value that can be natively
* serialized and re-used to clone this builder instance.
*
* @return array Serializable array of configuration properties.
*/
public function jsonSerialize()
{
$properties = [
'_templatePath', '_template', '_plugin', '_theme', '_layout', '_autoLayout',
'_layoutPath', '_name', '_className', '_options', '_helpers'
];

$array = [];

foreach ($properties as $property) {
$array[$property] = $this->{$property};
}

return array_filter($array, function ($i) {
return !is_array($i) && strlen($i) || !empty($i);
});
}

/**
* Configures a view builder instance from serialized config.
*
* @param array $config View builder configuration array.
* @return $this Configured view builder instance.
*/
public function createFromArray($config)
{
foreach ($config as $property => $value) {
$this->{$property} = $value;
}

return $this;
}

/**
* Serializes the view builder object.
*
* @return void
*/
public function serialize()
{
$array = $this->jsonSerialize();
return serialize($array);
}

/**
* Unserializes the view builder object.
*
* @param string $data Serialized string.
* @return $this Configured view builder instance.
*/
public function unserialize($data)
{
return $this->createFromArray(unserialize($data));
}
}
26 changes: 15 additions & 11 deletions tests/TestCase/Mailer/EmailTest.php
Expand Up @@ -2648,7 +2648,6 @@ public function testJsonSerialize()
->cc(['mark@cakephp.org', 'juan@cakephp.org' => 'Juan Basso'])
->bcc('phpnut@cakephp.org')
->subject('Test Serialize')
->template('default', 'test')
->messageId('<uuid@server.com>')
->domain('foo.bar')
->viewVars([
Expand All @@ -2664,9 +2663,13 @@ public function testJsonSerialize()
]
]);

$this->CakeEmail->viewBuilder()
->template('default')
->layout('test');

$result = json_decode(json_encode($this->CakeEmail), true);
$this->assertContains('test', $result['_viewVars']['exception']);
unset($result['_viewVars']['exception']);
$this->assertContains('test', $result['viewVars']['exception']);
unset($result['viewVars']['exception']);

$encode = function ($path) {
return chunk_split(base64_encode(file_get_contents($path)), 76, "\r\n");
Expand All @@ -2679,16 +2682,18 @@ public function testJsonSerialize()
'_cc' => ['mark@cakephp.org' => 'mark@cakephp.org', 'juan@cakephp.org' => 'Juan Basso'],
'_bcc' => ['phpnut@cakephp.org' => 'phpnut@cakephp.org'],
'_subject' => 'Test Serialize',
'_template' => 'default',
'_layout' => 'test',
'_viewRender' => 'Cake\View\View',
'_helpers' => ['Html'],
'_emailFormat' => 'text',
'_messageId' => '<uuid@server.com>',
'_domain' => 'foo.bar',
'charset' => 'utf-8',
'headerCharset' => 'utf-8',
'_viewVars' => [
'viewConfig' => [
'_template' => 'default',
'_layout' => 'test',
'_helpers' => ['Html'],
'_className' => 'Cake\View\View',
],
'viewVars' => [
'users' => [
'id' => 1,
'username' => 'mariano'
Expand All @@ -2712,10 +2717,9 @@ 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']);
$this->assertContains('test', $result['viewVars']['exception']);
unset($result['viewVars']['exception']);
$this->assertEquals($expected, $result);
}

Expand Down
29 changes: 29 additions & 0 deletions tests/TestCase/View/ViewBuilderTest.php
Expand Up @@ -183,4 +183,33 @@ public function testBuildMissingViewClass()
$builder->className('Foo');
$builder->build();
}

/**
* testJsonSerialize()
*
* @return void
*/
public function testJsonSerialize()
{
$builder = new ViewBuilder();

$builder
->template('default')
->layout('test')
->helpers(['Html'])
->className('JsonView');

$result = json_decode(json_encode($builder), true);

$expected = [
'_template' => 'default',
'_layout' => 'test',
'_helpers' => ['Html'],
'_className' => 'JsonView',
];
$this->assertEquals($expected, $result);

$result = json_decode(json_encode(unserialize(serialize($builder))), true);
$this->assertEquals($expected, $result);
}
}

0 comments on commit 4eecd3a

Please sign in to comment.