Dashboard sipadu mbip
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.

Base64ContentEncoder.php 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. * @experimental in 4.3
  16. */
  17. final class Base64ContentEncoder extends Base64Encoder implements ContentEncoderInterface
  18. {
  19. public function encodeByteStream($stream, int $maxLineLength = 0): iterable
  20. {
  21. if (!\is_resource($stream)) {
  22. throw new \TypeError(sprintf('Method "%s" takes a stream as a first argument.', __METHOD__));
  23. }
  24. $filter = stream_filter_append($stream, 'convert.base64-encode', \STREAM_FILTER_READ, [
  25. 'line-length' => 0 >= $maxLineLength || 76 < $maxLineLength ? 76 : $maxLineLength,
  26. 'line-break-chars' => "\r\n",
  27. ]);
  28. if (!\is_resource($filter)) {
  29. throw new RuntimeException('Unable to set the base64 content encoder to the filter.');
  30. }
  31. if (stream_get_meta_data($stream)['seekable'] ?? false) {
  32. rewind($stream);
  33. }
  34. while (!feof($stream)) {
  35. yield fread($stream, 8192);
  36. }
  37. stream_filter_remove($filter);
  38. }
  39. public function getName(): string
  40. {
  41. return 'base64';
  42. }
  43. }