Skip to content

Commit

Permalink
Use object cloning instead of serialization.
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Aug 27, 2015
1 parent 6d12516 commit 2b714fb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
10 changes: 10 additions & 0 deletions src/Mailer/Email.php
Expand Up @@ -356,6 +356,16 @@ public function __construct($config = null)
}
}

/**
* Clone ViewBuilder instance when email object is cloned.
*
* @return void
*/
public function __clone()
{
$this->_viewBuilder = clone $this->viewBuilder();
}

/**
* From
*
Expand Down
20 changes: 15 additions & 5 deletions src/Mailer/Mailer.php
Expand Up @@ -100,11 +100,12 @@ abstract class Mailer implements EventListenerInterface
protected $_email;

/**
* Serialized Email instance config.
* Cloned Email instance for restoring instance after email is sent by
* mailer action.
*
* @var string
*/
protected $_emailConfig;
protected $_clonedEmail;

/**
* Constructor.
Expand All @@ -118,7 +119,7 @@ public function __construct(Email $email = null)
}

$this->_email = $email;
$this->_emailConfig = $this->_email->serialize();
$this->_clonedEmail = clone $email;
}

/**
Expand Down Expand Up @@ -213,13 +214,22 @@ public function send($action, $args = [], $headers = [])
call_user_func_array([$this, $action], $args);

$result = $this->_email->send();

$this->reset();
$this->_email->unserialize($this->_emailConfig);

return $result;
}

/**
* Reset email instance.
*
* @return $this
*/
protected function reset()
{
$this->_email = clone $this->_clonedEmail;
return $this;
}

/**
* Implemented events.
*
Expand Down
8 changes: 3 additions & 5 deletions tests/test_app/TestApp/Mailer/TestMailer.php
Expand Up @@ -28,12 +28,10 @@ public function getEmailForAssertion()
return $this->_email;
}

public function __call($method, $args)
public function reset()
{
if ($method === 'reset') {
$this->template = $this->viewBuilder()->template();
}
$this->template = $this->viewBuilder()->template();

return parent::__call($method, $args);
return parent::reset();
}
}

0 comments on commit 2b714fb

Please sign in to comment.