Class ServerRequestFactory
Factory for making ServerRequest instances.
This adds in CakePHP specific behavior to populate the basePath and webroot attributes. Furthermore the Uri's path is corrected to only contain the 'virtual' path for the request.
Method Summary
-
createServerRequest() public
Create a new server request.
Note that server-params are taken precisely as given - no parsing/processing of the given values is performed, and, in particular, no attempt is made to determine the HTTP method or URI, which must be provided explicitly.
-
createUploadedFiles() protected static
Create uploaded file instances from a (possibly nested)
$_FILESspecification.PHP stores nested uploads as separate trees per attribute, e.g.
['tmp_name' => ['a' => ...], 'size' => ['a' => ...]]. -
fromGlobals() public static
Create a request from the supplied superglobal values.
If any argument is not supplied, the corresponding superglobal value will be used.
-
isFileAttributeTree() protected static
Validate corresponding filename, size, and error leaves before transposing a tree.
-
isFileSpecification() protected static
Recognize parallel PHP upload attributes by their leaf types, not form field names.
-
marshalBodyAndRequestMethod() protected static
Sets the REQUEST_METHOD environment variable based on the simulated _method HTTP override value. The 'ORIGINAL_REQUEST_METHOD' is also preserved, if you want the read the non-simulated HTTP method the client used.
Override values are upper-cased and may only contain the letters A-Z, optionally separated by single hyphens or underscores (e.g.
VERSION-CONTROL).Request body of content type "application/x-www-form-urlencoded" is parsed into array for PUT/PATCH/DELETE requests.
-
marshalFiles() protected static
Process uploaded files and move things onto the parsed body.
-
normalizeOverrideMethod() protected static
Normalize and validate a method override value.
Only letters, optionally separated by single hyphens or underscores, are accepted to prevent arbitrary client supplied strings from ending up in
REQUEST_METHOD, while still allowing custom methods like the WebDAVVERSION-CONTROL. -
normalizeServer() protected static
Populate
HTTP_AUTHORIZATIONfrom the Apache request headers.Some SAPI/httpd combinations don't expose the Authorization header in
$_SERVER. -
normalizeUploadedFiles() protected static
Normalize a
$_FILESstructure into a tree of UploadedFileInterface instances.
Method Detail
createServerRequest() ¶ public
createServerRequest(string $method, UriInterface|string $uri, array $serverParams = []): Cake\Http\ServerRequest
Create a new server request.
Note that server-params are taken precisely as given - no parsing/processing of the given values is performed, and, in particular, no attempt is made to determine the HTTP method or URI, which must be provided explicitly.
Parameters
-
string$method The HTTP method associated with the request.
-
UriInterface|string$uri The URI associated with the request. If the value is a string, the factory MUST create a UriInterface instance based on it.
-
array$serverParams optional Array of SAPI parameters with which to seed the generated request instance.
Returns
Cake\Http\ServerRequestcreateUploadedFiles() ¶ protected static
createUploadedFiles(mixed $tmpName, mixed $size, mixed $error, mixed $name, mixed $type): Psr\Http\Message\UploadedFileInterface|array<array-key, \Psr\Http\Message\UploadedFileInterface|array<array-key, mixed>>
Create uploaded file instances from a (possibly nested) $_FILES specification.
PHP stores nested uploads as separate trees per attribute, e.g.
['tmp_name' => ['a' => ...], 'size' => ['a' => ...]].
Parameters
-
mixed$tmpName The
tmp_namevalue or tree.-
mixed$size The
sizevalue or tree.-
mixed$error The
errorvalue or tree.-
mixed$name The
namevalue or tree.-
mixed$type The
typevalue or tree.
Returns
Psr\Http\Message\UploadedFileInterface|array<array-key, \Psr\Http\Message\UploadedFileInterface|array<array-key, mixed>>Throws
InvalidArgumentExceptionWhen the specification is incomplete.
fromGlobals() ¶ public static
fromGlobals(array|null $server = null, array|null $query = null, array|null $parsedBody = null, array|null $cookies = null, array|null $files = null): Cake\Http\ServerRequest
Create a request from the supplied superglobal values.
If any argument is not supplied, the corresponding superglobal value will be used.
Parameters
-
array|null$server optional $_SERVER superglobal
-
array|null$query optional $_GET superglobal
-
array|null$parsedBody optional $_POST superglobal
-
array|null$cookies optional $_COOKIE superglobal
-
array|null$files optional $_FILES superglobal
Returns
Cake\Http\ServerRequestThrows
InvalidArgumentExceptionfor invalid file values
isFileAttributeTree() ¶ protected static
isFileAttributeTree(mixed $tmpName, mixed $size, mixed $error): bool
Validate corresponding filename, size, and error leaves before transposing a tree.
Parameters
-
mixed$tmpName Filename or filename tree.
-
mixed$size Size or size tree.
-
mixed$error Error code or error tree.
Returns
boolisFileSpecification() ¶ protected static
isFileSpecification(array $files): bool
Recognize parallel PHP upload attributes by their leaf types, not form field names.
Parameters
-
array$files Candidate file specification or nested form group.
Returns
boolmarshalBodyAndRequestMethod() ¶ protected static
marshalBodyAndRequestMethod(array $parsedBody, Cake\Http\ServerRequest $request): Cake\Http\ServerRequest
Sets the REQUEST_METHOD environment variable based on the simulated _method HTTP override value. The 'ORIGINAL_REQUEST_METHOD' is also preserved, if you want the read the non-simulated HTTP method the client used.
Override values are upper-cased and may only contain the letters A-Z, optionally
separated by single hyphens or underscores (e.g. VERSION-CONTROL).
Request body of content type "application/x-www-form-urlencoded" is parsed into array for PUT/PATCH/DELETE requests.
Parameters
-
array$parsedBody Parsed body.
-
Cake\Http\ServerRequest$request Request instance.
Returns
Cake\Http\ServerRequestmarshalFiles() ¶ protected static
marshalFiles(array $files, Cake\Http\ServerRequest $request): Cake\Http\ServerRequest
Process uploaded files and move things onto the parsed body.
Parameters
-
array$files Files array for normalization and merging in parsed body.
-
Cake\Http\ServerRequest$request Request instance.
Returns
Cake\Http\ServerRequestnormalizeOverrideMethod() ¶ protected static
normalizeOverrideMethod(mixed $method): string
Normalize and validate a method override value.
Only letters, optionally separated by single hyphens or underscores, are accepted to prevent
arbitrary client supplied strings from ending up in REQUEST_METHOD, while
still allowing custom methods like the WebDAV VERSION-CONTROL.
Parameters
-
mixed$method The method from the
_methodfield orX-Http-Method-Overrideheader.
Returns
stringThrows
Cake\Http\Exception\BadRequestExceptionWhen the method is not a valid override value.
normalizeServer() ¶ protected static
normalizeServer(array $server): array
Populate HTTP_AUTHORIZATION from the Apache request headers.
Some SAPI/httpd combinations don't expose the Authorization header
in $_SERVER.
Parameters
-
array$server The SERVER data to normalize.
Returns
arraynormalizeUploadedFiles() ¶ protected static
normalizeUploadedFiles(array $files): array<array-key, \Psr\Http\Message\UploadedFileInterface|array<array-key, mixed>>
Normalize a $_FILES structure into a tree of UploadedFileInterface instances.
Parameters
-
array$files The files data to normalize.
Returns
array<array-key, \Psr\Http\Message\UploadedFileInterface|array<array-key, mixed>>Throws
InvalidArgumentExceptionFor invalid file values.