Skip to content

Commit

Permalink
bug #30718 [Mime] Fix serialization of Message instances (fabpot)
Browse files Browse the repository at this point in the history
This PR was merged into the 4.3-dev branch.

Discussion
----------

[Mime] Fix serialization of Message instances

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | yes
| New feature?  | no <!-- don't forget to update src/**/CHANGELOG.md files -->
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | n/a

Commits
-------

f5386ff [Mime] fixed serialization of Message instances
  • Loading branch information
fabpot committed Mar 27, 2019
2 parents 0c02a40 + f5386ff commit 19c6639
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 27 deletions.
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;
}
}

0 comments on commit 19c6639

Please sign in to comment.