REST: Rename attributes to path params

Change-Id: I1cd7297715bf0f9902949a5117ea7ab94b689a37
This commit is contained in:
Tim Starling 2019-06-11 07:32:51 +10:00
parent 4e0e36397c
commit 4b07863b72
5 changed files with 35 additions and 50 deletions

View file

@ -12,7 +12,7 @@ abstract class RequestBase implements RequestInterface {
private $headerCollection;
/** @var array */
private $attributes = [];
private $pathParams = [];
/** @var string */
private $cookiePrefix;
@ -83,20 +83,16 @@ abstract class RequestBase implements RequestInterface {
return $this->headerCollection->getHeaderLine( $name );
}
public function setAttributes( $attributes ) {
$this->attributes = $attributes;
public function setPathParams( $params ) {
$this->pathParams = $params;
}
public function getAttributes() {
return $this->attributes;
public function getPathParams() {
return $this->pathParams;
}
public function getAttribute( $name, $default = null ) {
if ( array_key_exists( $name, $this->attributes ) ) {
return $this->attributes[$name];
} else {
return $default;
}
public function getPathParam( $name ) {
return $this->pathParams[$name] ?? null;
}
public function getCookiePrefix() {

View file

@ -47,7 +47,7 @@ class RequestData extends RequestBase {
* - queryParams: Equivalent to $_GET
* - uploadedFiles: An array of objects implementing UploadedFileInterface
* - postParams: Equivalent to $_POST
* - attributes: The attributes, usually from path template parameters
* - pathParams: The path template parameters
* - headers: An array with the the key being the header name
* - cookiePrefix: A prefix to add to cookie names in getCookie()
*/
@ -61,7 +61,7 @@ class RequestData extends RequestBase {
$this->queryParams = $params['queryParams'] ?? [];
$this->uploadedFiles = $params['uploadedFiles'] ?? [];
$this->postParams = $params['postParams'] ?? [];
$this->setAttributes( $params['attributes'] ?? [] );
$this->setPathParams( $params['pathParams'] ?? [] );
$this->setHeaders( $params['headers'] ?? [] );
parent::__construct( $params['cookiePrefix'] ?? '' );
}

View file

@ -207,45 +207,34 @@ interface RequestInterface {
*/
function getUploadedFiles();
/**
* 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.
*/
function getAttributes();
/**
* 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|null $default Default value to return if the attribute does not exist.
* @return mixed
*/
function getAttribute( $name, $default = null );
// MediaWiki extensions to PSR-7
/**
* Erase all attributes from the object and set the attribute array to the
* specified value
* Get the parameters derived from the path template match
*
* @param mixed[] $attributes
* @return string[]
*/
function setAttributes( $attributes );
function getPathParams();
/**
* Retrieve a single path parameter.
*
* Retrieves a single path parameter as described in getPathParams(). If
* the attribute has not been previously set, returns null.
*
* @see getPathParams()
* @param string $name The parameter name.
* @return string|null
*/
function getPathParam( $name );
/**
* Erase all path parameters from the object and set the parameter array
* to the one specified.
*
* @param string[] $params
*/
function setPathParams( $params );
/**
* Get the current cookie prefix

View file

@ -233,7 +233,7 @@ class Router {
}
}
$request->setAttributes( $match['params'] );
$request->setPathParams( $match['params'] );
$spec = $match['userData'];
$objectFactorySpec = array_intersect_key( $spec,
[ 'factory' => true, 'class' => true, 'args' => true ] );

View file

@ -3,7 +3,7 @@
namespace MediaWiki\Rest;
/**
* A handler base class which unpacks attributes from the path template and
* A handler base class which unpacks parameters from the path template and
* passes them as formal parameters to run().
*
* run() must be declared in the subclass. It cannot be declared as abstract
@ -13,7 +13,7 @@ namespace MediaWiki\Rest;
*/
class SimpleHandler extends Handler {
public function execute() {
$params = array_values( $this->getRequest()->getAttributes() );
$params = array_values( $this->getRequest()->getPathParams() );
return $this->run( ...$params );
}
}