diff --git a/src/Symfony/Bridge/Twig/Mime/TemplatedEmail.php b/src/Symfony/Bridge/Twig/Mime/TemplatedEmail.php index 14fb14c0a211a..d840e268c4847 100644 --- a/src/Symfony/Bridge/Twig/Mime/TemplatedEmail.php +++ b/src/Symfony/Bridge/Twig/Mime/TemplatedEmail.php @@ -84,4 +84,22 @@ public function getContext(): array { return $this->context; } + + /** + * @internal + */ + public function __serialize(): array + { + return [$this->template, $this->htmlTemplate, $this->textTemplate, $this->context, parent::__serialize()]; + } + + /** + * @internal + */ + public function __unserialize(array $data): void + { + [$this->template, $this->htmlTemplate, $this->textTemplate, $this->context, $parentData] = $data; + + parent::__unserialize($parentData); + } } diff --git a/src/Symfony/Bridge/Twig/Tests/Mime/TemplatedEmailTest.php b/src/Symfony/Bridge/Twig/Tests/Mime/TemplatedEmailTest.php index 8dab46a4c31f2..f5d9235a6d503 100644 --- a/src/Symfony/Bridge/Twig/Tests/Mime/TemplatedEmailTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Mime/TemplatedEmailTest.php @@ -22,4 +22,18 @@ public function test() $email->htmlTemplate($template = 'html'); $this->assertEquals($template, $email->getHtmlTemplate()); } + + public function testSerialize() + { + $email = (new TemplatedEmail()) + ->textTemplate('text.txt.twig') + ->htmlTemplate('text.html.twig') + ->context($context = ['a' => 'b']) + ; + + $email = unserialize(serialize($email)); + $this->assertEquals('text.txt.twig', $email->getTextTemplate()); + $this->assertEquals('text.html.twig', $email->getHtmlTemplate()); + $this->assertEquals($context, $email->getContext()); + } } diff --git a/src/Symfony/Component/Mime/Email.php b/src/Symfony/Component/Mime/Email.php index 5a1e57aa6746f..7812372d5372f 100644 --- a/src/Symfony/Component/Mime/Email.php +++ b/src/Symfony/Component/Mime/Email.php @@ -543,20 +543,11 @@ private function setListAddressHeaderBody($name, array $addresses) return $this; } - public function __sleep() + /** + * @internal + */ + public function __serialize(): array { - $this->_headers = $this->getHeaders(); - $this->_raw = false; - - if (null !== $body = parent::getBody()) { - $r = new \ReflectionProperty(Message::class, 'body'); - $r->setAccessible(true); - $this->_body = $r->getValue($this); - $this->_raw = true; - - return ['_raw', '_headers', '_body']; - } - if (\is_resource($this->text)) { if (stream_get_meta_data($this->text)['seekable'] ?? false) { rewind($this->text); @@ -583,22 +574,16 @@ public function __sleep() } } - return ['_raw', '_headers', 'text', 'textCharset', 'html', 'htmlCharset', 'attachments']; + return [$this->text, $this->textCharset, $this->html, $this->htmlCharset, $this->attachments, parent::__serialize()]; } - public function __wakeup() + /** + * @internal + */ + public function __unserialize(array $data): void { - $r = new \ReflectionProperty(Message::class, 'headers'); - $r->setAccessible(true); - $r->setValue($this, $this->_headers); - unset($this->_headers); + [$this->text, $this->textCharset, $this->html, $this->htmlCharset, $this->attachments, $parentData] = $data; - if ($this->_raw) { - $r = new \ReflectionProperty(Message::class, 'body'); - $r->setAccessible(true); - $r->setValue($this, $this->_body); - unset($this->_body); - } - unset($this->_raw); + parent::__unserialize($parentData); } } diff --git a/src/Symfony/Component/Mime/Message.php b/src/Symfony/Component/Mime/Message.php index 43b5fe40c911a..db6e13fae6a4d 100644 --- a/src/Symfony/Component/Mime/Message.php +++ b/src/Symfony/Component/Mime/Message.php @@ -129,4 +129,20 @@ private function generateMessageId(string $email): string { return bin2hex(random_bytes(16)).strstr($email, '@'); } + + /** + * @internal + */ + public function __serialize(): array + { + return [$this->headers, $this->body]; + } + + /** + * @internal + */ + public function __unserialize(array $data): void + { + [$this->headers, $this->body] = $data; + } } diff --git a/src/Symfony/Component/Mime/RawMessage.php b/src/Symfony/Component/Mime/RawMessage.php index 790c7eea5351c..16b090c95474e 100644 --- a/src/Symfony/Component/Mime/RawMessage.php +++ b/src/Symfony/Component/Mime/RawMessage.php @@ -16,7 +16,7 @@ * * @experimental in 4.3 */ -class RawMessage +class RawMessage implements \Serializable { private $message; @@ -52,4 +52,36 @@ public function toIterable(): iterable } $this->message = $message; } + + /** + * @internal + */ + final public function serialize() + { + return serialize($this->__serialize()); + } + + /** + * @internal + */ + final public function unserialize($serialized) + { + $this->__unserialize(unserialize($serialized)); + } + + /** + * @internal + */ + public function __serialize(): array + { + return [$this->message]; + } + + /** + * @internal + */ + public function __unserialize(array $data): void + { + [$this->message] = $data; + } }