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

Skip to content

Commit 19c6639

Browse files
committed
bug #30718 [Mime] Fix serialization of Message instances (fabpot)
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
2 parents 0c02a40 + f5386ff commit 19c6639

File tree

5 files changed

+92
-27
lines changed

5 files changed

+92
-27
lines changed

src/Symfony/Bridge/Twig/Mime/TemplatedEmail.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,22 @@ public function getContext(): array
8484
{
8585
return $this->context;
8686
}
87+
88+
/**
89+
* @internal
90+
*/
91+
public function __serialize(): array
92+
{
93+
return [$this->template, $this->htmlTemplate, $this->textTemplate, $this->context, parent::__serialize()];
94+
}
95+
96+
/**
97+
* @internal
98+
*/
99+
public function __unserialize(array $data): void
100+
{
101+
[$this->template, $this->htmlTemplate, $this->textTemplate, $this->context, $parentData] = $data;
102+
103+
parent::__unserialize($parentData);
104+
}
87105
}

src/Symfony/Bridge/Twig/Tests/Mime/TemplatedEmailTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,18 @@ public function test()
2222
$email->htmlTemplate($template = 'html');
2323
$this->assertEquals($template, $email->getHtmlTemplate());
2424
}
25+
26+
public function testSerialize()
27+
{
28+
$email = (new TemplatedEmail())
29+
->textTemplate('text.txt.twig')
30+
->htmlTemplate('text.html.twig')
31+
->context($context = ['a' => 'b'])
32+
;
33+
34+
$email = unserialize(serialize($email));
35+
$this->assertEquals('text.txt.twig', $email->getTextTemplate());
36+
$this->assertEquals('text.html.twig', $email->getHtmlTemplate());
37+
$this->assertEquals($context, $email->getContext());
38+
}
2539
}

src/Symfony/Component/Mime/Email.php

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -543,20 +543,11 @@ private function setListAddressHeaderBody($name, array $addresses)
543543
return $this;
544544
}
545545

546-
public function __sleep()
546+
/**
547+
* @internal
548+
*/
549+
public function __serialize(): array
547550
{
548-
$this->_headers = $this->getHeaders();
549-
$this->_raw = false;
550-
551-
if (null !== $body = parent::getBody()) {
552-
$r = new \ReflectionProperty(Message::class, 'body');
553-
$r->setAccessible(true);
554-
$this->_body = $r->getValue($this);
555-
$this->_raw = true;
556-
557-
return ['_raw', '_headers', '_body'];
558-
}
559-
560551
if (\is_resource($this->text)) {
561552
if (stream_get_meta_data($this->text)['seekable'] ?? false) {
562553
rewind($this->text);
@@ -583,22 +574,16 @@ public function __sleep()
583574
}
584575
}
585576

586-
return ['_raw', '_headers', 'text', 'textCharset', 'html', 'htmlCharset', 'attachments'];
577+
return [$this->text, $this->textCharset, $this->html, $this->htmlCharset, $this->attachments, parent::__serialize()];
587578
}
588579

589-
public function __wakeup()
580+
/**
581+
* @internal
582+
*/
583+
public function __unserialize(array $data): void
590584
{
591-
$r = new \ReflectionProperty(Message::class, 'headers');
592-
$r->setAccessible(true);
593-
$r->setValue($this, $this->_headers);
594-
unset($this->_headers);
585+
[$this->text, $this->textCharset, $this->html, $this->htmlCharset, $this->attachments, $parentData] = $data;
595586

596-
if ($this->_raw) {
597-
$r = new \ReflectionProperty(Message::class, 'body');
598-
$r->setAccessible(true);
599-
$r->setValue($this, $this->_body);
600-
unset($this->_body);
601-
}
602-
unset($this->_raw);
587+
parent::__unserialize($parentData);
603588
}
604589
}

src/Symfony/Component/Mime/Message.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,20 @@ private function generateMessageId(string $email): string
129129
{
130130
return bin2hex(random_bytes(16)).strstr($email, '@');
131131
}
132+
133+
/**
134+
* @internal
135+
*/
136+
public function __serialize(): array
137+
{
138+
return [$this->headers, $this->body];
139+
}
140+
141+
/**
142+
* @internal
143+
*/
144+
public function __unserialize(array $data): void
145+
{
146+
[$this->headers, $this->body] = $data;
147+
}
132148
}

src/Symfony/Component/Mime/RawMessage.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
* @experimental in 4.3
1818
*/
19-
class RawMessage
19+
class RawMessage implements \Serializable
2020
{
2121
private $message;
2222

@@ -52,4 +52,36 @@ public function toIterable(): iterable
5252
}
5353
$this->message = $message;
5454
}
55+
56+
/**
57+
* @internal
58+
*/
59+
final public function serialize()
60+
{
61+
return serialize($this->__serialize());
62+
}
63+
64+
/**
65+
* @internal
66+
*/
67+
final public function unserialize($serialized)
68+
{
69+
$this->__unserialize(unserialize($serialized));
70+
}
71+
72+
/**
73+
* @internal
74+
*/
75+
public function __serialize(): array
76+
{
77+
return [$this->message];
78+
}
79+
80+
/**
81+
* @internal
82+
*/
83+
public function __unserialize(array $data): void
84+
{
85+
[$this->message] = $data;
86+
}
5587
}

0 commit comments

Comments
 (0)