Browse Source

Use object cloning instead of serialization.

ADmad 10 years ago
parent
commit
2b714fb522
3 changed files with 28 additions and 10 deletions
  1. 10 0
      src/Mailer/Email.php
  2. 15 5
      src/Mailer/Mailer.php
  3. 3 5
      tests/test_app/TestApp/Mailer/TestMailer.php

+ 10 - 0
src/Mailer/Email.php

@@ -357,6 +357,16 @@ class Email implements JsonSerializable, Serializable
     }
 
     /**
+     * Clone ViewBuilder instance when email object is cloned.
+     *
+     * @return void
+     */
+    public function __clone()
+    {
+        $this->_viewBuilder = clone $this->viewBuilder();
+    }
+
+    /**
      * From
      *
      * @param string|array|null $email Null to get, String with email,

+ 15 - 5
src/Mailer/Mailer.php

@@ -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.
@@ -118,7 +119,7 @@ abstract class Mailer implements EventListenerInterface
         }
 
         $this->_email = $email;
-        $this->_emailConfig = $this->_email->serialize();
+        $this->_clonedEmail = clone $email;
     }
 
     /**
@@ -213,14 +214,23 @@ abstract class Mailer implements EventListenerInterface
         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.
      *
      * @return array

+ 3 - 5
tests/test_app/TestApp/Mailer/TestMailer.php

@@ -28,12 +28,10 @@ class TestMailer extends Mailer
         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();
     }
 }