8000 [Mime] Forbid messages that are generators to be used more than once · symfony/symfony@3c5fe51 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3c5fe51

Browse files
committed
[Mime] Forbid messages that are generators to be used more than once
1 parent ef1197d commit 3c5fe51

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/Symfony/Component/Mime/RawMessage.php

Lines changed: 18 additions & 0 deletions
< 10000 /tr>
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
class RawMessage
2020
{
2121
private iterable|string|null $message = null;
22+
private bool $isGeneratorClosed;
2223

2324
public function __construct(iterable|string $message)
2425
{
@@ -41,12 +42,29 @@ public function toString(): string
4142

4243
public function toIterable(): iterable
4344
{
45+
if ($this->isGeneratorClosed ?? false) {
46+
trigger_deprecation('symfony/mime', '6.4', 'Sending an email with a closed generator is deprecated and will throw in 7.0.');
47+
// throw new LogicException('Unable to send the email as its generator is already closed.');
48+
}
49+
4450
if (\is_string($this->message)) {
4551
yield $this->message;
4652

4753
return;
4854
}
4955

56+
if ($this->message instanceof \Generator) {
57+
$message = '';
58+
foreach ($this->message as $chunk) {
59+
$message .= $chunk;
60+
yield $chunk;
61+
}
62+
$this->isGeneratorClosed = !$this->message->valid();
63+
$this->message = $message;
64+
65+
return;
66+
}
67+
5068
foreach ($this->message as $chunk) {
5169
yield $chunk;
5270
}

src/Symfony/Component/Mime/Tests/RawMessageTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@
1212
namespace Symfony\Component\Mime\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1516
use Symfony\Component\Mime\RawMessage;
1617

1718
class RawMessageTest extends TestCase
1819
{
20+
use ExpectDeprecationTrait;
21+
1922
/**
2023
* @dataProvider provideMessages
2124
*/
@@ -46,6 +49,37 @@ public function testSerialization(mixed $messageParameter, bool $supportReuse)
4649
}
4750
}
4851

52+
/**
53+
* @dataProvider provideMessages
54+
*/
55+
public function testToIterable(mixed $messageParameter, bool $supportReuse)
56+
{
57+
$message = new RawMessage($messageParameter);
58+
$this->assertEquals('some string', implode('', iterator_to_array($message->toIterable())));
59+
60+
if ($supportReuse) {
61+
// calling methods more than once work
62+
$this->assertEquals('some string', implode('', iterator_to_array($message->toIterable())));
63+
}
64+
}
65+
66+
/**
67+
* @dataProvider provideMessages
68+
*
69+
* @group legacy
70+
*/
71+
public function testToIterableLegacy(mixed $messageParameter, bool $supportReuse)
72+
{
73+
$message = new RawMessage($messageParameter);
74+
$this->assertEquals('some string', implode('', iterator_to_array($message->toIterable())));
75+
76+
if (!$supportReuse) {
77+
// in 7.0, the test with a generator will throw an exception
78+
$this->expectDeprecation('Since symfony/mime 6.4: Sending an email with a closed generator is deprecated and will throw in 7.0.');
79+
$this->assertEquals('some string', implode('', iterator_to_array($message->toIterable())));
80+
}
81+
}
82+
4983
public static function provideMessages(): array
5084
{
5185
return [

0 commit comments

Comments
 (0)
0