You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.3 KiB

3 years ago
  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. }