ResourceGenerator.php 775 B

12345678910111213141516171819202122232425262728293031323334
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Utils;
  12. class ResourceGenerator
  13. {
  14. /**
  15. * TODO: Swoole file hook does not support `php://temp` and `php://memory`.
  16. */
  17. public static function from(string $body, string $filename = 'php://temp')
  18. {
  19. $resource = fopen($filename, 'r+');
  20. if ($body !== '') {
  21. fwrite($resource, $body);
  22. fseek($resource, 0);
  23. }
  24. return $resource;
  25. }
  26. public static function fromMemory(string $body)
  27. {
  28. return static::from($body, 'php://memory');
  29. }
  30. }