23 private static $mimeTypes;
33 public function __construct($body,
string $filename =
null,
string $contentType =
null,
string $encoding =
null)
35 if (
null === $contentType) {
36 $contentType =
'application/octet-stream';
38 list($this->mediaType, $subtype) = explode(
'/', $contentType);
40 parent::__construct($body,
null, $subtype, $encoding);
42 $this->filename = $filename;
47 public static function fromPath(
string $path,
string $name =
null,
string $contentType =
null): self
51 if (
null === $contentType) {
52 $ext = strtolower(substr($path, strrpos($path,
'.') + 1));
53 if (
null === self::$mimeTypes) {
56 $contentType = self::$mimeTypes->getMimeTypes($ext)[0] ??
'application/octet-stream';
59 if (
false === $handle = @fopen($path,
'r',
false)) {
62 $p =
new self($handle, $name ?: basename($path), $contentType);
78 return $this->cid ?: $this->cid = $this->generateContentId();
83 return null !== $this->cid;
88 return $this->mediaType;
93 $headers = parent::getPreparedHeaders();
95 if (
null !== $this->cid) {
96 $headers->setHeaderBody(
'Id',
'Content-ID', $this->cid);
99 if (
null !== $this->filename) {
100 $headers->setHeaderParameter(
'Content-Disposition',
'filename', $this->filename);
108 $str = parent::asDebugString();
109 if (
null !== $this->filename) {
110 $str .=
' filename: '.$this->filename;
116 private function generateContentId(): string
118 return bin2hex(random_bytes(16)).
'@symfony';
123 if (
null !== $this->handle && \is_resource($this->handle)) {
124 fclose($this->handle);
137 foreach ([
'body',
'charset',
'subtype',
'disposition',
'name',
'encoding'] as $name) {
138 $r = new \ReflectionProperty(TextPart::class, $name);
139 $r->setAccessible(
true);
140 $this->_parent[$name] = $r->getValue($this);
144 return [
'_headers',
'_parent',
'filename',
'mediaType'];
149 $r = new \ReflectionProperty(AbstractPart::class,
'headers');
150 $r->setAccessible(
true);
151 $r->setValue($this, $this->_headers);
152 unset($this->_headers);
154 foreach ([
'body',
'charset',
'subtype',
'disposition',
'name',
'encoding'] as $name) {
155 $r = new \ReflectionProperty(TextPart::class, $name);
156 $r->setAccessible(
true);
157 $r->setValue($this, $this->_parent[$name]);
159 unset($this->_parent);