REST: Rename attributes to path params
authorTim Starling <tstarling@wikimedia.org>
Mon, 10 Jun 2019 21:32:51 +0000 (07:32 +1000)
committerTim Starling <tstarling@wikimedia.org>
Fri, 14 Jun 2019 07:01:15 +0000 (17:01 +1000)
Change-Id: I1cd7297715bf0f9902949a5117ea7ab94b689a37

includes/Rest/RequestBase.php
includes/Rest/RequestData.php
includes/Rest/RequestInterface.php
includes/Rest/Router.php
includes/Rest/SimpleHandler.php

index cacef62..4bed899 100644 (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() {
index 1522c6b..997350c 100644 (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'] ?? '' );
        }
index 65c72f6..eba389a 100644 (file)
@@ -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
index 83cd0f0..39bee89 100644 (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 ] );
index 65bc0f5..85749c6 100644 (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 );
        }
 }