SMimePart.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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\Header\Headers;
  12. /**
  13. * @author Sebastiaan Stok <s.stok@rollerscapes.net>
  14. */
  15. class SMimePart extends AbstractPart
  16. {
  17. /** @internal, to be removed in 8.0 */
  18. protected Headers $_headers;
  19. public function __construct(
  20. private iterable|string $body,
  21. private string $type,
  22. private string $subtype,
  23. private array $parameters,
  24. ) {
  25. parent::__construct();
  26. }
  27. public function getMediaType(): string
  28. {
  29. return $this->type;
  30. }
  31. public function getMediaSubtype(): string
  32. {
  33. return $this->subtype;
  34. }
  35. public function bodyToString(): string
  36. {
  37. if (\is_string($this->body)) {
  38. return $this->body;
  39. }
  40. $body = '';
  41. foreach ($this->body as $chunk) {
  42. $body .= $chunk;
  43. }
  44. $this->body = $body;
  45. return $body;
  46. }
  47. public function bodyToIterable(): iterable
  48. {
  49. if (\is_string($this->body)) {
  50. yield $this->body;
  51. return;
  52. }
  53. $body = '';
  54. foreach ($this->body as $chunk) {
  55. $body .= $chunk;
  56. yield $chunk;
  57. }
  58. $this->body = $body;
  59. }
  60. public function getPreparedHeaders(): Headers
  61. {
  62. $headers = clone parent::getHeaders();
  63. $headers->setHeaderBody('Parameterized', 'Content-Type', $this->getMediaType().'/'.$this->getMediaSubtype());
  64. foreach ($this->parameters as $name => $value) {
  65. $headers->setHeaderParameter('Content-Type', $name, $value);
  66. }
  67. return $headers;
  68. }
  69. public function __serialize(): array
  70. {
  71. if (self::class === (new \ReflectionMethod($this, '__sleep'))->class || self::class !== (new \ReflectionMethod($this, '__serialize'))->class) {
  72. // convert iterables to strings for serialization
  73. if (is_iterable($this->body)) {
  74. $this->body = $this->bodyToString();
  75. }
  76. return [
  77. '_headers' => $this->getHeaders(),
  78. 'body' => $this->body,
  79. 'type' => $this->type,
  80. 'subtype' => $this->subtype,
  81. 'parameters' => $this->parameters,
  82. ];
  83. }
  84. trigger_deprecation('symfony/mime', '7.4', 'Implementing "%s::__sleep()" is deprecated, use "__serialize()" instead.', get_debug_type($this));
  85. $data = [];
  86. foreach ($this->__sleep() as $key) {
  87. try {
  88. if (($r = new \ReflectionProperty($this, $key))->isInitialized($this)) {
  89. $data[$key] = $r->getValue($this);
  90. }
  91. } catch (\ReflectionException) {
  92. $data[$key] = $this->$key;
  93. }
  94. }
  95. return $data;
  96. }
  97. public function __unserialize(array $data): void
  98. {
  99. if ($wakeup = self::class !== (new \ReflectionMethod($this, '__wakeup'))->class && self::class === (new \ReflectionMethod($this, '__unserialize'))->class) {
  100. trigger_deprecation('symfony/mime', '7.4', 'Implementing "%s::__wakeup()" is deprecated, use "__unserialize()" instead.', get_debug_type($this));
  101. }
  102. if (['_headers', 'body', 'type', 'subtype', 'parameters'] === array_keys($data)) {
  103. parent::__unserialize(['headers' => $data['_headers']]);
  104. $this->body = $data['body'];
  105. $this->type = $data['type'];
  106. $this->subtype = $data['subtype'];
  107. $this->parameters = $data['parameters'];
  108. if ($wakeup) {
  109. $this->__wakeup();
  110. }
  111. return;
  112. }
  113. $p = "\0".self::class."\0";
  114. if (["\0*\0_headers", $p.'body', $p.'type', $p.'subtype', $p.'parameters'] === array_keys($data)) {
  115. $r = new \ReflectionProperty(parent::class, 'headers');
  116. $r->setValue($this, $data["\0*\0_headers"]);
  117. $this->body = $data[$p.'body'];
  118. $this->type = $data[$p.'type'];
  119. $this->subtype = $data[$p.'subtype'];
  120. $this->parameters = $data[$p.'parameters'];
  121. if ($wakeup) {
  122. $this->_headers = $data["\0*\0_headers"];
  123. $this->__wakeup();
  124. }
  125. return;
  126. }
  127. trigger_deprecation('symfony/mime', '7.4', 'Passing extra keys to "%s::__unserialize()" is deprecated, populate properties in "%s::__unserialize()" instead.', self::class, get_debug_type($this));
  128. \Closure::bind(function ($data) use ($wakeup) {
  129. foreach ($data as $key => $value) {
  130. $this->{("\0" === $key[0] ?? '') ? substr($key, 1 + strrpos($key, "\0")) : $key} = $value;
  131. }
  132. if ($wakeup) {
  133. $this->__wakeup();
  134. }
  135. }, $this, static::class)($data);
  136. }
  137. /**
  138. * @deprecated since Symfony 7.4, will be replaced by `__serialize()` in 8.0
  139. */
  140. public function __sleep(): array
  141. {
  142. trigger_deprecation('symfony/mime', '7.4', 'Calling "%s::__sleep()" is deprecated, use "__serialize()" instead.', get_debug_type($this));
  143. // convert iterables to strings for serialization
  144. if (is_iterable($this->body)) {
  145. $this->body = $this->bodyToString();
  146. }
  147. $this->_headers = $this->getHeaders();
  148. return ['_headers', 'body', 'type', 'subtype', 'parameters'];
  149. }
  150. /**
  151. * @deprecated since Symfony 7.4, will be replaced by `__unserialize()` in 8.0
  152. */
  153. public function __wakeup(): void
  154. {
  155. trigger_deprecation('symfony/mime', '7.4', 'Calling "%s::__wakeup()" is deprecated, use "__unserialize()" instead.', get_debug_type($this));
  156. $r = new \ReflectionProperty(AbstractPart::class, 'headers');
  157. $r->setValue($this, $this->_headers);
  158. unset($this->_headers);
  159. }
  160. }