362 lines
12 KiB
PHP
362 lines
12 KiB
PHP
<?php
|
|
|
|
namespace WellRESTed\Message;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
use Psr\Http\Message\StreamableInterface;
|
|
use WellRESTed\Stream\Stream;
|
|
|
|
class ServerRequest extends Request implements ServerRequestInterface
|
|
{
|
|
/** @var array */
|
|
private $attributes;
|
|
/** @var array */
|
|
private $cookieParams;
|
|
/** @var array */
|
|
private $fileParams;
|
|
/** @var array */
|
|
private $queryParams;
|
|
/** @var array */
|
|
private $serverParams;
|
|
/** @var mixed */
|
|
private $parsedBody;
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->attributes = [];
|
|
}
|
|
|
|
// ------------------------------------------------------------------------
|
|
// Psr\Http\Message\ServerRequestInterface
|
|
|
|
/**
|
|
* Retrieve server parameters.
|
|
*
|
|
* Retrieves data related to the incoming request environment,
|
|
* typically derived from PHP's $_SERVER superglobal. The data IS NOT
|
|
* REQUIRED to originate from $_SERVER.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function getServerParams()
|
|
{
|
|
return $this->serverParams;
|
|
}
|
|
|
|
/**
|
|
* Retrieve cookies.
|
|
*
|
|
* Retrieves cookies sent by the client to the server.
|
|
*
|
|
* The data MUST be compatible with the structure of the $_COOKIE
|
|
* superglobal.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function getCookieParams()
|
|
{
|
|
return $this->cookieParams;
|
|
}
|
|
|
|
/**
|
|
* Create a new instance with the specified cookies.
|
|
*
|
|
* The data IS NOT REQUIRED to come from the $_COOKIE superglobal, but MUST
|
|
* be compatible with the structure of $_COOKIE. Typically, this data will
|
|
* be injected at instantiation.
|
|
*
|
|
* This method MUST be implemented in such a way as to retain the
|
|
* immutability of the message, and MUST return a new instance that has the
|
|
* updated cookie values.
|
|
*
|
|
* @param array $cookies Array of key/value pairs representing cookies.
|
|
* @return self
|
|
*/
|
|
public function withCookieParams(array $cookies)
|
|
{
|
|
$request = clone $this;
|
|
$request->cookieParams = $cookies;
|
|
return $request;
|
|
}
|
|
|
|
/**
|
|
* Retrieve query string arguments.
|
|
*
|
|
* Retrieves the deserialized query string arguments, if any.
|
|
*
|
|
* Note: the query params might not be in sync with the URL or server
|
|
* params. If you need to ensure you are only getting the original
|
|
* values, you may need to parse the composed URL or the `QUERY_STRING`
|
|
* composed in the server params.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function getQueryParams()
|
|
{
|
|
return $this->queryParams;
|
|
}
|
|
|
|
/**
|
|
* Create a new instance with the specified query string arguments.
|
|
*
|
|
* These values SHOULD remain immutable over the course of the incoming
|
|
* request. They MAY be injected during instantiation, such as from PHP's
|
|
* $_GET superglobal, or MAY be derived from some other value such as the
|
|
* URI. In cases where the arguments are parsed from the URI, the data
|
|
* MUST be compatible with what PHP's parse_str() would return for
|
|
* purposes of how duplicate query parameters are handled, and how nested
|
|
* sets are handled.
|
|
*
|
|
* Setting query string arguments MUST NOT change the URL stored by the
|
|
* request, nor the values in the server params.
|
|
*
|
|
* This method MUST be implemented in such a way as to retain the
|
|
* immutability of the message, and MUST return a new instance that has the
|
|
* updated query string arguments.
|
|
*
|
|
* @param array $query Array of query string arguments, typically from
|
|
* $_GET.
|
|
* @return self
|
|
*/
|
|
public function withQueryParams(array $query)
|
|
{
|
|
$request = clone $this;
|
|
$request->queryParams = $query;
|
|
return $request;
|
|
}
|
|
|
|
/**
|
|
* Retrieve the upload file metadata.
|
|
*
|
|
* This method MUST return file upload metadata in the same structure
|
|
* as PHP's $_FILES superglobal.
|
|
*
|
|
* These values MUST remain immutable over the course of the incoming
|
|
* request. They SHOULD be injected during instantiation, such as from PHP's
|
|
* $_FILES superglobal, but MAY be derived from other sources.
|
|
*
|
|
* @return array Upload file(s) metadata, if any.
|
|
*/
|
|
public function getFileParams()
|
|
{
|
|
return $this->fileParams;
|
|
}
|
|
|
|
/**
|
|
* Retrieve any parameters provided in the request body.
|
|
*
|
|
* If the request Content-Type is either application/x-www-form-urlencoded
|
|
* or multipart/form-data, and the request method is POST, this method MUST
|
|
* return the contents of $_POST.
|
|
*
|
|
* Otherwise, this method may return any results of deserializing
|
|
* the request body content; as parsing returns structured content, the
|
|
* potential types MUST be arrays or objects only. A null value indicates
|
|
* the absence of body content.
|
|
*
|
|
* @return null|array|object The deserialized body parameters, if any.
|
|
* These will typically be an array or object.
|
|
*/
|
|
public function getParsedBody()
|
|
{
|
|
return $this->parsedBody;
|
|
}
|
|
|
|
/**
|
|
* Create a new instance with the specified body parameters.
|
|
*
|
|
* These MAY be injected during instantiation.
|
|
*
|
|
* If the request Content-Type is either application/x-www-form-urlencoded
|
|
* or multipart/form-data, and the request method is POST, use this method
|
|
* ONLY to inject the contents of $_POST.
|
|
*
|
|
* The data IS NOT REQUIRED to come from $_POST, but MUST be the results of
|
|
* deserializing the request body content. Deserialization/parsing returns
|
|
* structured data, and, as such, this method ONLY accepts arrays or objects,
|
|
* or a null value if nothing was available to parse.
|
|
*
|
|
* As an example, if content negotiation determines that the request data
|
|
* is a JSON payload, this method could be used to create a request
|
|
* instance with the deserialized parameters.
|
|
*
|
|
* This method MUST be implemented in such a way as to retain the
|
|
* immutability of the message, and MUST return a new instance that has the
|
|
* updated body parameters.
|
|
*
|
|
* @param null|array|object $data The deserialized body data. This will
|
|
* typically be in an array or object.
|
|
* @return self
|
|
*/
|
|
public function withParsedBody($data)
|
|
{
|
|
$request = clone $this;
|
|
$request->parsedBody = $data;
|
|
return $request;
|
|
}
|
|
|
|
/**
|
|
* Retrieve attributes derived from the request.
|
|
*
|
|
* The request "attributes" may be used to allow injection of any
|
|
* parameters derived from the request: e.g., the results of path
|
|
* match operations; the results of decrypting cookies; the results of
|
|
* deserializing non-form-encoded message bodies; etc. Attributes
|
|
* will be application and request specific, and CAN be mutable.
|
|
*
|
|
* @return array Attributes derived from the request.
|
|
*/
|
|
public function getAttributes()
|
|
{
|
|
return $this->attributes;
|
|
}
|
|
|
|
/**
|
|
* Retrieve a single derived request attribute.
|
|
*
|
|
* Retrieves a single derived request attribute as described in
|
|
* getAttributes(). If the attribute has not been previously set, returns
|
|
* the default value as provided.
|
|
*
|
|
* This method obviates the need for a hasAttribute() method, as it allows
|
|
* specifying a default value to return if the attribute is not found.
|
|
*
|
|
* @see getAttributes()
|
|
* @param string $name The attribute name.
|
|
* @param mixed $default Default value to return if the attribute does not exist.
|
|
* @return mixed
|
|
*/
|
|
public function getAttribute($name, $default = null)
|
|
{
|
|
if (isset($this->attributes[$name])) {
|
|
return $this->attributes[$name];
|
|
}
|
|
return $default;
|
|
}
|
|
|
|
/**
|
|
* Create a new instance with the specified derived request attribute.
|
|
*
|
|
* This method allows setting a single derived request attribute as
|
|
* described in getAttributes().
|
|
*
|
|
* This method MUST be implemented in such a way as to retain the
|
|
* immutability of the message, and MUST return a new instance that has the
|
|
* updated attribute.
|
|
*
|
|
* @see getAttributes()
|
|
* @param string $name The attribute name.
|
|
* @param mixed $value The value of the attribute.
|
|
* @return self
|
|
*/
|
|
public function withAttribute($name, $value)
|
|
{
|
|
$request = clone $this;
|
|
$request->attributes[$name] = $value;
|
|
return $request;
|
|
}
|
|
|
|
/**
|
|
* Create a new instance that removes the specified derived request
|
|
* attribute.
|
|
*
|
|
* This method allows removing a single derived request attribute as
|
|
* described in getAttributes().
|
|
*
|
|
* This method MUST be implemented in such a way as to retain the
|
|
* immutability of the message, and MUST return a new instance that removes
|
|
* the attribute.
|
|
*
|
|
* @see getAttributes()
|
|
* @param string $name The attribute name.
|
|
* @return self
|
|
*/
|
|
public function withoutAttribute($name)
|
|
{
|
|
$request = clone $this;
|
|
unset($request->attributes[$name]);
|
|
return $request;
|
|
}
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
public function __clone()
|
|
{
|
|
if (is_object($this->parsedBody)) {
|
|
$this->parsedBody = clone $this->parsedBody;
|
|
}
|
|
parent::__clone();
|
|
}
|
|
|
|
protected function readFromServerRequest(array $attributes = null)
|
|
{
|
|
$this->attributes = $attributes ?: [];
|
|
$this->serverParams = $_SERVER;
|
|
$this->cookieParams = $_COOKIE;
|
|
$this->fileParams = $_FILES;
|
|
$this->queryParams = [];
|
|
if (isset($_SERVER["QUERY_STRING"])) {
|
|
parse_str($_SERVER["QUERY_STRING"], $this->queryParams);
|
|
}
|
|
$headers = $this->getServerRequestHeaders();
|
|
foreach ($headers as $key => $value) {
|
|
$this->headers[$key] = $value;
|
|
}
|
|
$this->body = $this->getStreamForBody();
|
|
|
|
$contentType = $this->getHeader("Content-type");
|
|
if ($contentType === "application/x-www-form-urlencoded" || $contentType === "multipart/form-data") {
|
|
$this->parsedBody = $_POST;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Return a reference to the singleton instance of the Request derived
|
|
* from the server's information about the request sent to the server.
|
|
*
|
|
* @return self
|
|
* @static
|
|
*/
|
|
public static function getServerRequest(array $attributes = null)
|
|
{
|
|
$request = new static();
|
|
$request->readFromServerRequest($attributes);
|
|
return $request;
|
|
}
|
|
|
|
/**
|
|
* Return a stream representing the request's body.
|
|
*
|
|
* Override this method to use a specific StreamableInterface implementation.
|
|
*
|
|
* @return StreamableInterface
|
|
*/
|
|
protected function getStreamForBody()
|
|
{
|
|
return new Stream(fopen("php://input", "r"));
|
|
}
|
|
|
|
/**
|
|
* Read and return all request headers from the request issued to the server.
|
|
*
|
|
* @return array Associative array of headers
|
|
*/
|
|
protected function getServerRequestHeaders()
|
|
{
|
|
// Prefer apache_request_headers is available.
|
|
if (function_exists("apache_request_headers")) {
|
|
return apache_request_headers();
|
|
}
|
|
|
|
// http://www.php.net/manual/en/function.getallheaders.php#84262
|
|
$headers = array();
|
|
foreach ($_SERVER as $name => $value) {
|
|
if (substr($name, 0, 5) === "HTTP_") {
|
|
$headers[str_replace(" ", "-", ucwords(strtolower(str_replace("_", " ", substr($name, 5)))))] = $value;
|
|
}
|
|
}
|
|
return $headers;
|
|
}
|
|
}
|