HttpFactory.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 RequestFactoryInterface, ResponseFactoryInterface, ServerRequestFactoryInterface, StreamFactoryInterface, UploadedFileFactoryInterface, UriFactoryInterface
  23. {
  24. public function createUploadedFile(
  25. StreamInterface $stream,
  26. int $size = null,
  27. int $error = \UPLOAD_ERR_OK,
  28. string $clientFilename = null,
  29. string $clientMediaType = null
  30. ): UploadedFileInterface {
  31. if ($size === null) {
  32. $size = $stream->getSize();
  33. }
  34. return new UploadedFile($stream, $size, $error, $clientFilename, $clientMediaType);
  35. }
  36. public function createStream(string $content = ''): StreamInterface
  37. {
  38. return Utils::streamFor($content);
  39. }
  40. public function createStreamFromFile(string $file, string $mode = 'r'): StreamInterface
  41. {
  42. try {
  43. $resource = Utils::tryFopen($file, $mode);
  44. } catch (\RuntimeException $e) {
  45. if ('' === $mode || false === \in_array($mode[0], ['r', 'w', 'a', 'x', 'c'], true)) {
  46. throw new \InvalidArgumentException(sprintf('Invalid file opening mode "%s"', $mode), 0, $e);
  47. }
  48. throw $e;
  49. }
  50. return Utils::streamFor($resource);
  51. }
  52. public function createStreamFromResource($resource): StreamInterface
  53. {
  54. return Utils::streamFor($resource);
  55. }
  56. public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface
  57. {
  58. if (empty($method)) {
  59. if (!empty($serverParams['REQUEST_METHOD'])) {
  60. $method = $serverParams['REQUEST_METHOD'];
  61. } else {
  62. throw new \InvalidArgumentException('Cannot determine HTTP method');
  63. }
  64. }
  65. return new ServerRequest($method, $uri, [], null, '1.1', $serverParams);
  66. }
  67. public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface
  68. {
  69. return new Response($code, [], null, '1.1', $reasonPhrase);
  70. }
  71. public function createRequest(string $method, $uri): RequestInterface
  72. {
  73. return new Request($method, $uri);
  74. }
  75. public function createUri(string $uri = ''): UriInterface
  76. {
  77. return new Uri($uri);
  78. }
  79. }