Sen descrición

Base64ContentEncoder.php 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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\Encoder;
  11. use Symfony\Component\Mime\Exception\RuntimeException;
  12. /**
  13. * @author Fabien Potencier <fabien@symfony.com>
  14. */
  15. final class Base64ContentEncoder extends Base64Encoder implements ContentEncoderInterface
  16. {
  17. public function encodeByteStream($stream, int $maxLineLength = 0): iterable
  18. {
  19. if (!\is_resource($stream)) {
  20. throw new \TypeError(sprintf('Method "%s" takes a stream as a first argument.', __METHOD__));
  21. }
  22. $filter = stream_filter_append($stream, 'convert.base64-encode', \STREAM_FILTER_READ, [
  23. 'line-length' => 0 >= $maxLineLength || 76 < $maxLineLength ? 76 : $maxLineLength,
  24. 'line-break-chars' => "\r\n",
  25. ]);
  26. if (!\is_resource($filter)) {
  27. throw new RuntimeException('Unable to set the base64 content encoder to the filter.');
  28. }
  29. while (!feof($stream)) {
  30. yield fread($stream, 16372);
  31. }
  32. stream_filter_remove($filter);
  33. }
  34. public function getName(): string
  35. {
  36. return 'base64';
  37. }
  38. }