Skip to content

[Mime] rename Part/BodyFile to Part/File #48060

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
Nov 1, 2022
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
6 changes: 3 additions & 3 deletions src/Symfony/Bridge/Twig/Mime/WrappedTemplatedEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
namespace Symfony\Bridge\Twig\Mime;

use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Part\BodyFile;
use Symfony\Component\Mime\Part\DataPart;
use Symfony\Component\Mime\Part\File;
use Twig\Environment;

/**
Expand All @@ -40,7 +40,7 @@ public function toName(): string
public function image(string $image, string $contentType = null): string
{
$file = $this->twig->getLoader()->getSourceContext($image);
$body = $file->getPath() ? new BodyFile($file->getPath()) : $file->getCode();
$body = $file->getPath() ? new File($file->getPath()) : $file->getCode();
$this->message->addPart((new DataPart($body, $image, $contentType))->asInline());

return 'cid:'.$image;
Expand All @@ -49,7 +49,7 @@ public function image(string $image, string $contentType = null): string
public function attach(string $file, string $name = null, string $contentType = null): void
{
$file = $this->twig->getLoader()->getSourceContext($file);
$body = $file->getPath() ? new BodyFile($file->getPath()) : $file->getCode();
$body = $file->getPath() ? new File($file->getPath()) : $file->getCode();
$this->message->addPart(new DataPart($body, $name, $contentType));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Exception\InvalidArgumentException;
use Symfony\Component\Mime\Part\BodyFile;
use Symfony\Component\Mime\Part\DataPart;
use Symfony\Component\Mime\Part\File;
use Symfony\Component\Mime\RawMessage;

/**
Expand Down Expand Up @@ -105,7 +105,7 @@ public function testSendInvalidMessage()
$message = new Email();
$message->to('recipient@example.org');
$message->from('sender@example.org');
$message->addPart(new DataPart(new BodyFile('/does_not_exists')));
$message->addPart(new DataPart(new File('/does_not_exists')));

try {
$transport->send($message);
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Mime/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

use Symfony\Component\Mime\Exception\LogicException;
use Symfony\Component\Mime\Part\AbstractPart;
use Symfony\Component\Mime\Part\BodyFile;
use Symfony\Component\Mime\Part\DataPart;
use Symfony\Component\Mime\Part\File;
use Symfony\Component\Mime\Part\Multipart\AlternativePart;
use Symfony\Component\Mime\Part\Multipart\MixedPart;
use Symfony\Component\Mime\Part\Multipart\RelatedPart;
Expand Down Expand Up @@ -335,7 +335,7 @@ public function attach($body, string $name = null, string $contentType = null):
*/
public function attachFromPath(string $path, string $name = null, string $contentType = null): static
{
return $this->addPart(new DataPart(new BodyFile($path), $name, $contentType));
return $this->addPart(new DataPart(new File($path), $name, $contentType));
}

/**
Expand All @@ -353,7 +353,7 @@ public function embed($body, string $name = null, string $contentType = null): s
*/
public function embedFromPath(string $path, string $name = null, string $contentType = null): static
{
return $this->addPart((new DataPart(new BodyFile($path), $name, $contentType))->asInline());
return $this->addPart((new DataPart(new File($path), $name, $contentType))->asInline());
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Symfony/Component/Mime/Part/DataPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ class DataPart extends TextPart
private $cid;

/**
* @param resource|string|BodyFile $body Use a BodyFile instance to defer loading the file until rendering
* @param resource|string|File $body Use a File instance to defer loading the file until rendering
*/
public function __construct($body, string $filename = null, string $contentType = null, string $encoding = null)
{
unset($this->_parent);

if ($body instanceof BodyFile && !$filename) {
if ($body instanceof File && !$filename) {
$filename = basename($body->getPath());
}

if (null === $contentType) {
$contentType = $body instanceof BodyFile ? $body->getContentType() : 'application/octet-stream';
$contentType = $body instanceof File ? $body->getContentType() : 'application/octet-stream';
}
[$this->mediaType, $subtype] = explode('/', $contentType);

Expand All @@ -53,7 +53,7 @@ public function __construct($body, string $filename = null, string $contentType

public static function fromPath(string $path, string $name = null, string $contentType = null): self
{
return new self(new BodyFile($path), $name, $contentType);
return new self(new File($path), $name, $contentType);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/**
* @author Fabien Potencier <fabien@symfony.com>
*/
class BodyFile
class File
{
private static $mimeTypes;

Expand Down
12 changes: 6 additions & 6 deletions src/Symfony/Component/Mime/Part/TextPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@ class TextPart extends AbstractPart
private $seekable;

/**
* @param resource|string|BodyFile $body Use a BodyFile instance to defer loading the file until rendering
* @param resource|string|File $body Use a File instance to defer loading the file until rendering
*/
public function __construct($body, ?string $charset = 'utf-8', string $subtype = 'plain', string $encoding = null)
{
unset($this->_headers);

parent::__construct();

if (!\is_string($body) && !\is_resource($body) && !$body instanceof BodyFile) {
throw new \TypeError(sprintf('The body of "%s" must be a string, a resource, or an instance of "%s" (got "%s").', self::class, BodyFile::class, get_debug_type($body)));
if (!\is_string($body) && !\is_resource($body) && !$body instanceof File) {
throw new \TypeError(sprintf('The body of "%s" must be a string, a resource, or an instance of "%s" (got "%s").', self::class, File::class, get_debug_type($body)));
}

if ($body instanceof BodyFile) {
if ($body instanceof File) {
$path = $body->getPath();
if ((is_file($path) && !is_readable($path)) || is_dir($path)) {
throw new InvalidArgumentException(sprintf('Path "%s" is not readable.', $path));
Expand Down Expand Up @@ -118,7 +118,7 @@ public function getName(): ?string

public function getBody(): string
{
if ($this->body instanceof BodyFile) {
if ($this->body instanceof File) {
return file_get_contents($this->body->getPath());
}

Expand All @@ -140,7 +140,7 @@ public function bodyToString(): string

public function bodyToIterable(): iterable
{
if ($this->body instanceof BodyFile) {
if ($this->body instanceof File) {
$path = $this->body->getPath();
if (false === $handle = @fopen($path, 'r', false)) {
throw new InvalidArgumentException(sprintf('Unable to open path "%s".', $path));
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Mime/Tests/EmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Part\BodyFile;
use Symfony\Component\Mime\Part\DataPart;
use Symfony\Component\Mime\Part\File;
use Symfony\Component\Mime\Part\Multipart\AlternativePart;
use Symfony\Component\Mime\Part\Multipart\MixedPart;
use Symfony\Component\Mime\Part\Multipart\RelatedPart;
Expand Down Expand Up @@ -463,8 +463,8 @@ public function testAttachments()
$att = DataPart::fromPath($name, 'test');
$inline = DataPart::fromPath($name, 'test')->asInline();
$e = new Email();
$e->addPart(new DataPart(new BodyFile($name)));
$e->addPart((new DataPart(new BodyFile($name)))->asInline());
$e->addPart(new DataPart(new File($name)));
$e->addPart((new DataPart(new File($name)))->asInline());
$this->assertEquals([$att->bodyToString(), $inline->bodyToString()], array_map(function (DataPart $a) { return $a->bodyToString(); }, $e->getAttachments()));
$this->assertEquals([$att->getPreparedHeaders(), $inline->getPreparedHeaders()], array_map(function (DataPart $a) { return $a->getPreparedHeaders(); }, $e->getAttachments()));
}
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Mime/Tests/Part/TextPartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Symfony\Component\Mime\Header\Headers;
use Symfony\Component\Mime\Header\ParameterizedHeader;
use Symfony\Component\Mime\Header\UnstructuredHeader;
use Symfony\Component\Mime\Part\BodyFile;
use Symfony\Component\Mime\Part\File;
use Symfony\Component\Mime\Part\TextPart;

class TextPartTest extends TestCase
Expand Down Expand Up @@ -47,9 +47,9 @@ public function testConstructorWithResource()
fclose($f);
}

public function testConstructorWithBodyFile()
public function testConstructorWithFile()
{
$p = new TextPart(new BodyFile(\dirname(__DIR__).'/Fixtures/content.txt'));
$p = new TextPart(new File(\dirname(__DIR__).'/Fixtures/content.txt'));
$this->assertSame('content', $p->getBody());
$this->assertSame('content', $p->bodyToString());
$this->assertSame('content', implode('', iterator_to_array($p->bodyToIterable())));
Expand Down