.../modules/rest/src/Entity/RestResourceConfig.php | 31 +++++++++++++++++----- .../rest/src/RestResourceConfigInterface.php | 9 +++---- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/core/modules/rest/src/Entity/RestResourceConfig.php b/core/modules/rest/src/Entity/RestResourceConfig.php index 362c061..55ff040 100644 --- a/core/modules/rest/src/Entity/RestResourceConfig.php +++ b/core/modules/rest/src/Entity/RestResourceConfig.php @@ -106,9 +106,26 @@ public function getResourcePlugin() { /** * {@inheritdoc} */ - public function isRequestMethodEnabled($method) { - $method = $this->normalizeRestMethod($method); - return isset($this->configuration[$method]); + public function getMethods() { + $granularity = $this->configuration['granularity']; + if ($granularity === 'method') { + return $this->getMethodsForMethodGranularity(); + } + else { + // @todo Add resource-level granularity support in https://www.drupal.org/node/2721595. + } + } + + /** + * Retrieves a list of supported HTTP methods for this resource. + * + * @return string[] + * A list of supported HTTP methods. + */ + protected function getMethodsForMethodGranularity() { + $methods = array_keys($this->configuration); + $methods = array_filter($methods, function($v) { return $v !== 'granularity'; }); + return array_map([$this, 'normalizeRestMethod'], $methods); } /** @@ -116,7 +133,7 @@ public function isRequestMethodEnabled($method) { */ public function getSupportedAuthenticationProviders($method) { $method = $this->normalizeRestMethod($method); - if ($this->isRequestMethodEnabled($method) && isset($this->configuration[$method]['supported_auth'])) { + if (in_array($method, $this->getMethods()) && isset($this->configuration[$method]['supported_auth'])) { return $this->configuration[$method]['supported_auth']; } return []; @@ -127,7 +144,7 @@ public function getSupportedAuthenticationProviders($method) { */ public function addSupportedAuthenticationProvider($method, $auth) { $method = $this->normalizeRestMethod($method); - if (!$this->isRequestMethodEnabled($method)) { + if (!in_array($method, $this->getMethods())) { $this->configuration[$method] = ['supported_auth' => []]; } if (!isset($this->configuration[$method]['supported_auth'])){ @@ -158,7 +175,7 @@ public function removeSupportedAuthenticationProvider($method, $auth) { */ public function getSupportedFormats($method) { $method = $this->normalizeRestMethod($method); - if ($this->isRequestMethodEnabled($method) && isset($this->configuration[$method]['supported_formats'])) { + if (in_array($method, $this->getMethods()) && isset($this->configuration[$method]['supported_formats'])) { return $this->configuration[$method]['supported_formats']; } return []; @@ -169,7 +186,7 @@ public function getSupportedFormats($method) { */ public function addSupportedFormat($method, $format) { $method = $this->normalizeRestMethod($method); - if (!$this->isRequestMethodEnabled($method)) { + if (!in_array($method, $this->getMethods())) { $this->configuration[$method] = ['supported_formats' => []]; } if (!isset($this->configuration[$method]['supported_formats'])){ diff --git a/core/modules/rest/src/RestResourceConfigInterface.php b/core/modules/rest/src/RestResourceConfigInterface.php index 296a1e6..6fa2e1a 100644 --- a/core/modules/rest/src/RestResourceConfigInterface.php +++ b/core/modules/rest/src/RestResourceConfigInterface.php @@ -19,13 +19,12 @@ public function getResourcePlugin(); /** - * Denotes whether a given request method is enabled for this resource. + * Retrieves a list of supported HTTP methods. * - * @param string $method - * The request method. - * @return bool + * @return string[] + * A list of supported HTTP methods. */ - public function isRequestMethodEnabled($method); + public function getMethods(); /** * Retrieves a list of supported authentication mechanisms