HttpFactory.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. declare(strict_types=1);
  3. namespace GuzzleHttp\Psr7;
  4. use Psr\Http\Message\RequestFactoryInterface;
  5. use Psr\Http\Message\RequestInterface;
  6. use Psr\Http\Message\ResponseFactoryInterface;
  7. use Psr\Http\Message\ResponseInterface;
  8. use Psr\Http\Message\ServerRequestFactoryInterface;
  9. use Psr\Http\Message\ServerRequestInterface;
  10. use Psr\Http\Message\StreamFactoryInterface;
  11. use Psr\Http\Message\StreamInterface;
  12. use Psr\Http\Message\UploadedFileFactoryInterface;
  13. use Psr\Http\Message\UploadedFileInterface;
  14. use Psr\Http\Message\UriFactoryInterface;
  15. use Psr\Http\Message\UriInterface;
  16. /**
  17. * Implements all of the PSR-17 interfaces.
  18. *
  19. * Note: in consuming code it is recommended to require the implemented interfaces
  20. * and inject the instance of this class multiple times.
  21. */
  22. final class HttpFactory implements
  23. RequestFactoryInterface,
  24. ResponseFactoryInterface,
  25. ServerRequestFactoryInterface,
  26. StreamFactoryInterface,
  27. UploadedFileFactoryInterface,
  28. UriFactoryInterface
  29. {
  30. public function createUploadedFile(
  31. StreamInterface $stream,
  32. int $size = null,
  33. int $error = \UPLOAD_ERR_OK,
  34. string $clientFilename = null,
  35. string $clientMediaType = null
  36. ): UploadedFileInterface {
  37. if ($size === null) {
  38. $size = $stream->getSize();
  39. }
  40. return new UploadedFile($stream, $size, $error, $clientFilename, $clientMediaType);
  41. }
  42. public function createStream(string $content = ''): StreamInterface
  43. {
  44. return Utils::streamFor($content);
  45. }
  46. public function createStreamFromFile(string $file, string $mode = 'r'): StreamInterface
  47. {
  48. try {
  49. $resource = Utils::tryFopen($file, $mode);
  50. } catch (\RuntimeException $e) {
  51. if ('' === $mode || false === \in_array($mode[0], ['r', 'w', 'a', 'x', 'c'], true)) {
  52. throw new \InvalidArgumentException(sprintf('Invalid file opening mode "%s"', $mode), 0, $e);
  53. }
  54. throw $e;
  55. }
  56. return Utils::streamFor($resource);
  57. }
  58. public function createStreamFromResource($resource): StreamInterface
  59. {
  60. return Utils::streamFor($resource);
  61. }
  62. public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface
  63. {
  64. if (empty($method)) {
  65. if (!empty($serverParams['REQUEST_METHOD'])) {
  66. $method = $serverParams['REQUEST_METHOD'];
  67. } else {
  68. throw new \InvalidArgumentException('Cannot determine HTTP method');
  69. }
  70. }
  71. return new ServerRequest($method, $uri, [], null, '1.1', $serverParams);
  72. }
  73. public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface
  74. {
  75. return new Response($code, [], null, '1.1', $reasonPhrase);
  76. }
  77. public function createRequest(string $method, $uri): RequestInterface
  78. {
  79. return new Request($method, $uri);
  80. }
  81. public function createUri(string $uri = ''): UriInterface
  82. {
  83. return new Uri($uri);
  84. }
  85. }