Psr17Factory
in package
implements
RequestFactoryInterface, ResponseFactoryInterface, ServerRequestFactoryInterface, StreamFactoryInterface, UploadedFileFactoryInterface, UriFactoryInterface
A generic PSR-17 implementation.
You can create this class with concrete factory instances or let it use discovery to find suitable implementations as needed.
This class also provides two additional methods that are not in PSR17, to help with creating PSR-7 objects from PHP superglobals:
- createServerRequestFromGlobals()
- createUriFromGlobals()
The code in this class is inspired by the "nyholm/psr7", "guzzlehttp/psr7" and "symfony/http-foundation" packages, all licenced under MIT.
Copyright (c) 2004-2023 Fabien Potencier fabien@symfony.com Copyright (c) 2015 Michael Dowling mtdowling@gmail.com Copyright (c) 2015 Márk Sági-Kazár mark.sagikazar@gmail.com Copyright (c) 2015 Graham Campbell hello@gjcampbell.co.uk Copyright (c) 2016 Tobias Schultze webmaster@tubo-world.de Copyright (c) 2016 George Mponos gmponos@gmail.com Copyright (c) 2016-2018 Tobias Nyholm tobias.nyholm@gmail.com
Tags
Table of Contents
Interfaces
- RequestFactoryInterface
- ResponseFactoryInterface
- ServerRequestFactoryInterface
- StreamFactoryInterface
- UploadedFileFactoryInterface
- UriFactoryInterface
Properties
- $requestFactory : mixed
- $responseFactory : mixed
- $serverRequestFactory : mixed
- $streamFactory : mixed
- $uploadedFileFactory : mixed
- $uriFactory : mixed
Methods
- __construct() : mixed
- createRequest() : RequestInterface
- Create a new request.
- createResponse() : ResponseInterface
- Create a new response.
- createServerRequest() : ServerRequestInterface
- Create a new server request.
- createServerRequestFromGlobals() : ServerRequestInterface
- createStream() : StreamInterface
- Create a new stream from a string.
- createStreamFromFile() : StreamInterface
- Create a stream from an existing file.
- createStreamFromResource() : StreamInterface
- Create a new stream from an existing resource.
- createUploadedFile() : UploadedFileInterface
- Create a new uploaded file.
- createUri() : UriInterface
- Create a new URI.
- createUriFromGlobals() : UriInterface
- buildServerRequestFromGlobals() : ServerRequestInterface
- buildUriFromGlobals() : UriInterface
- createUploadedFileFromSpec() : UploadedFileInterface|array<string|int, UploadedFileInterface>
- Create and return an UploadedFile instance from a $_FILES specification.
- normalizeFiles() : array<string|int, mixed>
- setFactory() : mixed
Properties
$requestFactory
private
mixed
$requestFactory
$responseFactory
private
mixed
$responseFactory
$serverRequestFactory
private
mixed
$serverRequestFactory
$streamFactory
private
mixed
$streamFactory
$uploadedFileFactory
private
mixed
$uploadedFileFactory
$uriFactory
private
mixed
$uriFactory
Methods
__construct()
public
__construct([RequestFactoryInterface|null $requestFactory = null ][, ResponseFactoryInterface|null $responseFactory = null ][, ServerRequestFactoryInterface|null $serverRequestFactory = null ][, StreamFactoryInterface|null $streamFactory = null ][, UploadedFileFactoryInterface|null $uploadedFileFactory = null ][, UriFactoryInterface|null $uriFactory = null ]) : mixed
Parameters
- $requestFactory : RequestFactoryInterface|null = null
- $responseFactory : ResponseFactoryInterface|null = null
- $serverRequestFactory : ServerRequestFactoryInterface|null = null
- $streamFactory : StreamFactoryInterface|null = null
- $uploadedFileFactory : UploadedFileFactoryInterface|null = null
- $uriFactory : UriFactoryInterface|null = null
createRequest()
Create a new request.
public
createRequest(string $method, UriInterface|string $uri) : RequestInterface
Parameters
- $method : string
-
The HTTP method associated with the request.
- $uri : UriInterface|string
Return values
RequestInterfacecreateResponse()
Create a new response.
public
createResponse([int $code = 200 ][, string $reasonPhrase = '' ]) : ResponseInterface
Parameters
- $code : int = 200
-
HTTP status code; defaults to 200
- $reasonPhrase : string = ''
-
Reason phrase to associate with status code in generated response; if none is provided implementations MAY use the defaults as suggested in the HTTP specification.
Return values
ResponseInterfacecreateServerRequest()
Create a new server request.
public
createServerRequest(string $method, UriInterface|string $uri[, array<string|int, mixed> $serverParams = [] ]) : ServerRequestInterface
Parameters
- $method : string
-
The HTTP method associated with the request.
- $uri : UriInterface|string
- $serverParams : array<string|int, mixed> = []
-
Array of SAPI parameters with which to seed the generated request instance.
Return values
ServerRequestInterfacecreateServerRequestFromGlobals()
public
createServerRequestFromGlobals([array<string|int, mixed>|null $server = null ][, array<string|int, mixed>|null $get = null ][, array<string|int, mixed>|null $post = null ][, array<string|int, mixed>|null $cookie = null ][, array<string|int, mixed>|null $files = null ][, StreamInterface|null $body = null ]) : ServerRequestInterface
Parameters
- $server : array<string|int, mixed>|null = null
- $get : array<string|int, mixed>|null = null
- $post : array<string|int, mixed>|null = null
- $cookie : array<string|int, mixed>|null = null
- $files : array<string|int, mixed>|null = null
- $body : StreamInterface|null = null
Return values
ServerRequestInterfacecreateStream()
Create a new stream from a string.
public
createStream([string $content = '' ]) : StreamInterface
The stream SHOULD be created with a temporary resource.
Parameters
- $content : string = ''
-
String content with which to populate the stream.
Return values
StreamInterfacecreateStreamFromFile()
Create a stream from an existing file.
public
createStreamFromFile(string $filename[, string $mode = 'r' ]) : StreamInterface
The file MUST be opened using the given mode, which may be any mode
supported by the fopen function.
The $filename MAY be any string supported by fopen().
Parameters
- $filename : string
-
Filename or stream URI to use as basis of stream.
- $mode : string = 'r'
-
Mode with which to open the underlying filename/stream.
Return values
StreamInterfacecreateStreamFromResource()
Create a new stream from an existing resource.
public
createStreamFromResource(resource $resource) : StreamInterface
Parameters
- $resource : resource
Return values
StreamInterfacecreateUploadedFile()
Create a new uploaded file.
public
createUploadedFile(StreamInterface $stream[, int|null $size = null ][, int $error = UPLOAD_ERR_OK ][, string|null $clientFilename = null ][, string|null $clientMediaType = null ]) : UploadedFileInterface
If a size is not provided it will be determined by checking the size of the file.
Parameters
- $stream : StreamInterface
-
Underlying stream representing the uploaded file content.
- $size : int|null = null
-
in bytes
- $error : int = UPLOAD_ERR_OK
-
PHP file upload error
- $clientFilename : string|null = null
-
Filename as provided by the client, if any.
- $clientMediaType : string|null = null
-
Media type as provided by the client, if any.
Return values
UploadedFileInterfacecreateUri()
Create a new URI.
public
createUri([string $uri = '' ]) : UriInterface
Parameters
- $uri : string = ''
Return values
UriInterfacecreateUriFromGlobals()
public
createUriFromGlobals([array<string|int, mixed>|null $server = null ]) : UriInterface
Parameters
- $server : array<string|int, mixed>|null = null
Return values
UriInterfacebuildServerRequestFromGlobals()
private
buildServerRequestFromGlobals(ServerRequestInterface $request, array<string|int, mixed> $server, array<string|int, mixed> $files) : ServerRequestInterface
Parameters
- $request : ServerRequestInterface
- $server : array<string|int, mixed>
- $files : array<string|int, mixed>
Return values
ServerRequestInterfacebuildUriFromGlobals()
private
buildUriFromGlobals(UriInterface $uri, array<string|int, mixed> $server) : UriInterface
Parameters
- $uri : UriInterface
- $server : array<string|int, mixed>
Return values
UriInterfacecreateUploadedFileFromSpec()
Create and return an UploadedFile instance from a $_FILES specification.
private
createUploadedFileFromSpec(array<string|int, mixed> $value) : UploadedFileInterface|array<string|int, UploadedFileInterface>
Parameters
- $value : array<string|int, mixed>
-
$_FILES struct
Return values
UploadedFileInterface|array<string|int, UploadedFileInterface>normalizeFiles()
private
normalizeFiles(array<string|int, mixed> $files) : array<string|int, mixed>
Parameters
- $files : array<string|int, mixed>
Return values
array<string|int, mixed>setFactory()
private
setFactory(mixed $factory) : mixed
Parameters
- $factory : mixed