Skip to content

[Mime] Forbid messages that are generators to be used more than once #51926

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
Oct 15, 2023
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/Component/Mime/RawMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
class RawMessage
{
private iterable|string|null $message = null;
private bool $isGeneratorClosed;

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

public function toIterable(): iterable
{
if ($this->isGeneratorClosed ?? false) {
trigger_deprecation('symfony/mime', '6.4', 'Sending an email with a closed generator is deprecated and will throw in 7.0.');
// throw new LogicException('Unable to send the email as its generator is already closed.');
}

if (\is_string($this->message)) {
yield $this->message;

return;
}

if ($this->message instanceof \Generator) {
$message = '';
foreach ($this->message as $chunk) {
$message .= $chunk;
yield $chunk;
}
$this->isGeneratorClosed = !$this->message->valid();
$this->message = $message;

return;
}

foreach ($this->message as $chunk) {
yield $chunk;
}
Expand Down
34 changes: 34 additions & 0 deletions src/Symfony/Component/Mime/Tests/RawMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@
namespace Symfony\Component\Mime\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Component\Mime\RawMessage;

class RawMessageTest extends TestCase
{
use ExpectDeprecationTrait;

/**
* @dataProvider provideMessages
*/
Expand Down Expand Up @@ -46,6 +49,37 @@ public function testSerialization(mixed $messageParameter, bool $supportReuse)
}
}

/**
* @dataProvider provideMessages
*/
public function testToIterable(mixed $messageParameter, bool $supportReuse)
{
$message = new RawMessage($messageParameter);
$this->assertEquals('some string', implode('', iterator_to_array($message->toIterable())));

if ($supportReuse) {
// calling methods more than once work
$this->assertEquals('some string', implode('', iterator_to_array($message->toIterable())));
}
}

/**
* @dataProvider provideMessages
*
* @group legacy
*/
public function testToIterableLegacy(mixed $messageParameter, bool $supportReuse)
{
$message = new RawMessage($messageParameter);
$this->assertEquals('some string', implode('', iterator_to_array($message->toIterable())));

if (!$supportReuse) {
// in 7.0, the test with a generator will throw an exception
$this->expectDeprecation('Since symfony/mime 6.4: Sending an email with a closed generator is deprecated and will throw in 7.0.');
$this->assertEquals('some string', implode('', iterator_to_array($message->toIterable())));
}
}

public static function provideMessages(): array
{
return [
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy