Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Mime] Fix serialization of Message instances #30718

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/Symfony/Bridge/Twig/Mime/TemplatedEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
14 changes: 14 additions & 0 deletions src/Symfony/Bridge/Twig/Tests/Mime/TemplatedEmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
37 changes: 11 additions & 26 deletions src/Symfony/Component/Mime/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
}
16 changes: 16 additions & 0 deletions src/Symfony/Component/Mime/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
34 changes: 33 additions & 1 deletion src/Symfony/Component/Mime/RawMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* @experimental in 4.3
*/
class RawMessage
class RawMessage implements \Serializable
{
private $message;

Expand Down Expand Up @@ -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;
}
}