From 4b07863b72827f83c0b93e60f2a65e8fec33a180 Mon Sep 17 00:00:00 2001 From: Tim Starling Date: Tue, 11 Jun 2019 07:32:51 +1000 Subject: [PATCH] REST: Rename attributes to path params Change-Id: I1cd7297715bf0f9902949a5117ea7ab94b689a37 --- includes/Rest/RequestBase.php | 18 +++++-------- includes/Rest/RequestData.php | 4 +-- includes/Rest/RequestInterface.php | 43 +++++++++++------------------- includes/Rest/Router.php | 2 +- includes/Rest/SimpleHandler.php | 4 +-- 5 files changed, 28 insertions(+), 43 deletions(-) diff --git a/includes/Rest/RequestBase.php b/includes/Rest/RequestBase.php index cacef62d47..4bed8991f0 100644 --- a/includes/Rest/RequestBase.php +++ b/includes/Rest/RequestBase.php @@ -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() { diff --git a/includes/Rest/RequestData.php b/includes/Rest/RequestData.php index 1522c6b088..997350c360 100644 --- a/includes/Rest/RequestData.php +++ b/includes/Rest/RequestData.php @@ -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'] ?? '' ); } diff --git a/includes/Rest/RequestInterface.php b/includes/Rest/RequestInterface.php index 65c72f664e..eba389a781 100644 --- a/includes/Rest/RequestInterface.php +++ b/includes/Rest/RequestInterface.php @@ -207,45 +207,34 @@ interface RequestInterface { */ function getUploadedFiles(); + // MediaWiki extensions to PSR-7 + /** - * 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. + * Get the parameters derived from the path template match * - * @return array Attributes derived from the request. + * @return string[] */ - function getAttributes(); + function getPathParams(); /** - * Retrieve a single derived request attribute. + * Retrieve a single path parameter. * - * Retrieves a single derived request attribute as described in - * getAttributes(). If the attribute has not been previously set, returns - * the default value as provided. + * Retrieves a single path parameter as described in getPathParams(). If + * the attribute has not been previously set, returns null. * - * 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 + * @see getPathParams() + * @param string $name The parameter name. + * @return string|null */ - function getAttribute( $name, $default = null ); - - // MediaWiki extensions to PSR-7 + function getPathParam( $name ); /** - * Erase all attributes from the object and set the attribute array to the - * specified value + * Erase all path parameters from the object and set the parameter array + * to the one specified. * - * @param mixed[] $attributes + * @param string[] $params */ - function setAttributes( $attributes ); + function setPathParams( $params ); /** * Get the current cookie prefix diff --git a/includes/Rest/Router.php b/includes/Rest/Router.php index 83cd0f035e..39bee899c1 100644 --- a/includes/Rest/Router.php +++ b/includes/Rest/Router.php @@ -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 ] ); diff --git a/includes/Rest/SimpleHandler.php b/includes/Rest/SimpleHandler.php index 65bc0f59dd..85749c6229 100644 --- a/includes/Rest/SimpleHandler.php +++ b/includes/Rest/SimpleHandler.php @@ -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 ); } } -- 2.20.1