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