File tree Expand file tree Collapse file tree 2 files changed +39
-1
lines changed Expand file tree Collapse file tree 2 files changed +39
-1
lines changed Original file line number Diff line number Diff line change @@ -49,7 +49,7 @@ public function render(Message $message): void
49
49
50
50
$ previousRenderingKey = $ messageContext [__CLASS__ ] ?? null ;
51
51
unset($ messageContext [__CLASS__ ]);
52
- $ currentRenderingKey = md5 ( serialize ([ $ messageContext , $ message -> getTextTemplate (), $ message-> getHtmlTemplate ()]) );
52
+ $ currentRenderingKey = $ this -> getFingerPrint ( $ message );
53
53
if ($ previousRenderingKey === $ currentRenderingKey ) {
54
54
return ;
55
55
}
@@ -77,6 +77,23 @@ public function render(Message $message): void
77
77
$ message ->context ($ message ->getContext () + [__CLASS__ => $ currentRenderingKey ]);
78
78
}
79
79
80
+ private function getFingerPrint (TemplatedEmail $ message ): string
81
+ {
82
+ $ messageContext = $ message ->getContext ();
83
+ unset($ messageContext [__CLASS__ ]);
84
+
85
+ $ payload = [$ messageContext , $ message ->getTextTemplate (), $ message ->getHtmlTemplate ()];
86
+ try {
87
+ $ serialized = serialize ($ payload );
88
+ } catch (\Exception $ e ) {
89
+ // Serialization of 'Closure' is not allowed
90
+ // Happens when context contain a closure, in that case, we assume that context always change.
91
+ $ serialized = random_bytes (8 );
92
+ }
93
+
94
+ return md5 ($ serialized );
95
+ }
96
+
80
97
private function convertHtmlToText (string $ html ): string
81
98
{
82
99
if (null !== $ this ->converter ) {
Original file line number Diff line number Diff line change @@ -100,6 +100,27 @@ public function testRenderedOnce()
100
100
$ this ->assertEquals ('reset ' , $ email ->getTextBody ());
101
101
}
102
102
103
+ public function testRenderedOnceUnserializableContext ()
104
+ {
105
+ $ twig = new Environment (new ArrayLoader ([
106
+ 'text ' => 'Text ' ,
107
+ ]));
108
+ $ renderer = new BodyRenderer ($ twig );
109
+ $ email = (new TemplatedEmail ())
110
+ ->to ('fabien@symfony.com ' )
111
+ ->from ('helene@symfony.com ' )
112
+ ;
113
+ $ email ->textTemplate ('text ' );
114
+ $ email ->context ([
115
+ 'foo ' => static function () {
116
+ return 'bar ' ;
117
+ },
118
+ ]);
119
+
120
+ $ renderer ->render ($ email );
121
+ $ this ->assertEquals ('Text ' , $ email ->getTextBody ());
122
+ }
123
+
103
124
private function prepareEmail (?string $ text , ?string $ html , array $ context = []): TemplatedEmail
104
125
{
105
126
$ twig = new Environment (new ArrayLoader ([
You can’t perform that action at this time.
0 commit comments