/laradock/vendor/symfony/mime/Part/DataPart.php

https://gitlab.com/hoangduys4k5/laravelproject · PHP · 173 lines · 123 code · 31 blank · 19 comment · 14 complexity · 2f607b0260ce2124c16d1e0476063139 MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Mime\Part;
  11. use Symfony\Component\Mime\Exception\InvalidArgumentException;
  12. use Symfony\Component\Mime\Header\Headers;
  13. use Symfony\Component\Mime\MimeTypes;
  14. /**
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. */
  17. class DataPart extends TextPart
  18. {
  19. /** @internal */
  20. protected $_parent;
  21. private static $mimeTypes;
  22. private $filename;
  23. private $mediaType;
  24. private $cid;
  25. private $handle;
  26. /**
  27. * @param resource|string $body
  28. */
  29. public function __construct($body, string $filename = null, string $contentType = null, string $encoding = null)
  30. {
  31. unset($this->_parent);
  32. if (null === $contentType) {
  33. $contentType = 'application/octet-stream';
  34. }
  35. [$this->mediaType, $subtype] = explode('/', $contentType);
  36. parent::__construct($body, null, $subtype, $encoding);
  37. if (null !== $filename) {
  38. $this->filename = $filename;
  39. $this->setName($filename);
  40. }
  41. $this->setDisposition('attachment');
  42. }
  43. public static function fromPath(string $path, string $name = null, string $contentType = null): self
  44. {
  45. if (null === $contentType) {
  46. $ext = strtolower(substr($path, strrpos($path, '.') + 1));
  47. if (null === self::$mimeTypes) {
  48. self::$mimeTypes = new MimeTypes();
  49. }
  50. $contentType = self::$mimeTypes->getMimeTypes($ext)[0] ?? 'application/octet-stream';
  51. }
  52. if (false === is_readable($path)) {
  53. throw new InvalidArgumentException(sprintf('Path "%s" is not readable.', $path));
  54. }
  55. if (false === $handle = @fopen($path, 'r', false)) {
  56. throw new InvalidArgumentException(sprintf('Unable to open path "%s".', $path));
  57. }
  58. $p = new self($handle, $name ?: basename($path), $contentType);
  59. $p->handle = $handle;
  60. return $p;
  61. }
  62. /**
  63. * @return $this
  64. */
  65. public function asInline(): static
  66. {
  67. return $this->setDisposition('inline');
  68. }
  69. public function getContentId(): string
  70. {
  71. return $this->cid ?: $this->cid = $this->generateContentId();
  72. }
  73. public function hasContentId(): bool
  74. {
  75. return null !== $this->cid;
  76. }
  77. public function getMediaType(): string
  78. {
  79. return $this->mediaType;
  80. }
  81. public function getPreparedHeaders(): Headers
  82. {
  83. $headers = parent::getPreparedHeaders();
  84. if (null !== $this->cid) {
  85. $headers->setHeaderBody('Id', 'Content-ID', $this->cid);
  86. }
  87. if (null !== $this->filename) {
  88. $headers->setHeaderParameter('Content-Disposition', 'filename', $this->filename);
  89. }
  90. return $headers;
  91. }
  92. public function asDebugString(): string
  93. {
  94. $str = parent::asDebugString();
  95. if (null !== $this->filename) {
  96. $str .= ' filename: '.$this->filename;
  97. }
  98. return $str;
  99. }
  100. private function generateContentId(): string
  101. {
  102. return bin2hex(random_bytes(16)).'@symfony';
  103. }
  104. public function __destruct()
  105. {
  106. if (null !== $this->handle && \is_resource($this->handle)) {
  107. fclose($this->handle);
  108. }
  109. }
  110. public function __sleep(): array
  111. {
  112. // converts the body to a string
  113. parent::__sleep();
  114. $this->_parent = [];
  115. foreach (['body', 'charset', 'subtype', 'disposition', 'name', 'encoding'] as $name) {
  116. $r = new \ReflectionProperty(TextPart::class, $name);
  117. $r->setAccessible(true);
  118. $this->_parent[$name] = $r->getValue($this);
  119. }
  120. $this->_headers = $this->getHeaders();
  121. return ['_headers', '_parent', 'filename', 'mediaType'];
  122. }
  123. public function __wakeup()
  124. {
  125. $r = new \ReflectionProperty(AbstractPart::class, 'headers');
  126. $r->setAccessible(true);
  127. $r->setValue($this, $this->_headers);
  128. unset($this->_headers);
  129. if (!\is_array($this->_parent)) {
  130. throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
  131. }
  132. foreach (['body', 'charset', 'subtype', 'disposition', 'name', 'encoding'] as $name) {
  133. if (null !== $this->_parent[$name] && !\is_string($this->_parent[$name])) {
  134. throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
  135. }
  136. $r = new \ReflectionProperty(TextPart::class, $name);
  137. $r->setAccessible(true);
  138. $r->setValue($this, $this->_parent[$name]);
  139. }
  140. unset($this->_parent);
  141. }
  142. }