ResponseEmitter.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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\HttpServer;
  12. use Hyperf\Contract\ResponseEmitterInterface;
  13. use Hyperf\Contract\StdoutLoggerInterface;
  14. use Hyperf\HttpMessage\Stream\FileInterface;
  15. use Psr\Http\Message\ResponseInterface;
  16. use Swoole\Http\Response;
  17. use Throwable;
  18. class ResponseEmitter implements ResponseEmitterInterface
  19. {
  20. public function __construct(protected ?StdoutLoggerInterface $logger)
  21. {
  22. }
  23. /**
  24. * @param Response $connection
  25. */
  26. public function emit(ResponseInterface $response, mixed $connection, bool $withContent = true): void
  27. {
  28. try {
  29. if (strtolower($connection->header['Upgrade'] ?? '') === 'websocket') {
  30. return;
  31. }
  32. $this->buildSwooleResponse($connection, $response);
  33. $content = $response->getBody();
  34. if ($content instanceof FileInterface) {
  35. $connection->sendfile($content->getFilename());
  36. return;
  37. }
  38. if ($withContent) {
  39. $connection->end((string) $content);
  40. } else {
  41. $connection->end();
  42. }
  43. } catch (Throwable $exception) {
  44. $this->logger?->critical((string) $exception);
  45. }
  46. }
  47. protected function buildSwooleResponse(Response $swooleResponse, ResponseInterface $response): void
  48. {
  49. // Headers
  50. foreach ($response->getHeaders() as $key => $value) {
  51. $swooleResponse->header($key, $value);
  52. }
  53. // Cookies
  54. // This part maybe only supports of hyperf/http-message component.
  55. if (method_exists($response, 'getCookies')) {
  56. foreach ((array) $response->getCookies() as $domain => $paths) {
  57. foreach ($paths ?? [] as $path => $item) {
  58. foreach ($item ?? [] as $name => $cookie) {
  59. if ($this->isMethodsExists($cookie, [
  60. 'isRaw', 'getValue', 'getName', 'getExpiresTime', 'getPath', 'getDomain', 'isSecure', 'isHttpOnly', 'getSameSite',
  61. ])) {
  62. $value = $cookie->isRaw() ? $cookie->getValue() : rawurlencode($cookie->getValue());
  63. $swooleResponse->rawcookie($cookie->getName(), $value, $cookie->getExpiresTime(), $cookie->getPath(), $cookie->getDomain(), $cookie->isSecure(), $cookie->isHttpOnly(), (string) $cookie->getSameSite());
  64. }
  65. }
  66. }
  67. }
  68. }
  69. // Trailers
  70. if (method_exists($response, 'getTrailers') && method_exists($swooleResponse, 'trailer')) {
  71. foreach ($response->getTrailers() ?? [] as $key => $value) {
  72. $swooleResponse->trailer($key, $value);
  73. }
  74. }
  75. // Status code
  76. $swooleResponse->status($response->getStatusCode(), $response->getReasonPhrase());
  77. }
  78. protected function isMethodsExists(object $object, array $methods): bool
  79. {
  80. foreach ($methods as $method) {
  81. if (! method_exists($object, $method)) {
  82. return false;
  83. }
  84. }
  85. return true;
  86. }
  87. }