ResponseContext.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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\Context;
  12. use Psr\Http\Message\ResponseInterface;
  13. use RuntimeException;
  14. use Swow\Psr7\Message\ResponsePlusInterface;
  15. class ResponseContext
  16. {
  17. public static function get(?int $coroutineId = null): ResponsePlusInterface
  18. {
  19. return Context::get(ResponseInterface::class, null, $coroutineId);
  20. }
  21. public static function set(ResponseInterface $response, ?int $coroutineId = null): ResponsePlusInterface
  22. {
  23. if (! $response instanceof ResponsePlusInterface) {
  24. throw new RuntimeException('The response must instanceof ResponsePlusInterface');
  25. }
  26. return Context::set(ResponseInterface::class, $response, $coroutineId);
  27. }
  28. public static function has(?int $coroutineId = null): bool
  29. {
  30. return Context::has(ResponseInterface::class, $coroutineId);
  31. }
  32. public static function getOrNull(?int $coroutineId = null): ?ResponsePlusInterface
  33. {
  34. return Context::get(ResponseInterface::class, null, $coroutineId);
  35. }
  36. }