ServerRequest.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace oauth2\components;
  3. use Psr\Http\Message\ServerRequestInterface;
  4. use Yii;
  5. use GuzzleHttp\Psr7\LazyOpenStream;
  6. /**
  7. * Class ServerRequest
  8. * @package oauth2\components
  9. * @author jianyan74 <751393839@qq.com>
  10. */
  11. class ServerRequest extends \GuzzleHttp\Psr7\ServerRequest
  12. {
  13. private $attributes = [];
  14. /**
  15. * @return \GuzzleHttp\Psr7\ServerRequest|ServerRequest|ServerRequestInterface
  16. */
  17. public static function fromGlobals(): ServerRequestInterface
  18. {
  19. $method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET';
  20. $headers = getallheaders();
  21. if ($authorization = Yii::$app->params['Authorization'] ?? '') {
  22. $headers['Authorization'] = [$authorization];
  23. }
  24. $uri = self::getUriFromGlobals();
  25. $body = new LazyOpenStream('php://input', 'r+');
  26. $protocol = isset($_SERVER['SERVER_PROTOCOL']) ? str_replace('HTTP/', '', $_SERVER['SERVER_PROTOCOL']) : '1.1';
  27. $serverRequest = new self($method, $uri, $headers, $body, $protocol, $_SERVER);
  28. return $serverRequest
  29. ->withCookieParams($_COOKIE)
  30. ->withQueryParams($_GET)
  31. ->withParsedBody($_POST)
  32. ->withUploadedFiles(self::normalizeFiles($_FILES));
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function withAttribute($attribute, $value): ServerRequestInterface
  38. {
  39. $this->attributes[$attribute] = $value;
  40. return $this;
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function getAttributes(): array
  46. {
  47. return $this->attributes;
  48. }
  49. }
粤ICP备19079148号