diff --git a/src/Resource/ResourceManager.php b/src/Resource/ResourceManager.php index 1e203a76..142cd746 100644 --- a/src/Resource/ResourceManager.php +++ b/src/Resource/ResourceManager.php @@ -118,6 +118,19 @@ public function clearPluginCache($instance_id) { $this->plugins->remove($instance_id); } + /** + * {@inheritdoc} + */ + public function getResourceIdFromRequest() { + $resource_name = &drupal_static(__METHOD__); + if (isset($resource_name)) { + return $resource_name; + } + $path = $this->request->getPath(FALSE); + list($resource_name,) = static::getPageArguments($path); + return $resource_name; + } + /** * {@inheritdoc} */ diff --git a/src/Resource/ResourceManagerInterface.php b/src/Resource/ResourceManagerInterface.php index 7b7eab91..00e846bd 100644 --- a/src/Resource/ResourceManagerInterface.php +++ b/src/Resource/ResourceManagerInterface.php @@ -50,6 +50,14 @@ public function getPlugin($instance_id, RequestInterface $request = NULL); */ public function clearPluginCache($instance_id); + /** + * Get the resource name for the current request. + * + * @return string + * The resource ID. + */ + public function getResourceIdFromRequest(); + /** * Gets the major and minor version for the current request. * diff --git a/tests/RestfulHookMenuTestCase.test b/tests/RestfulHookMenuTestCase.test index c3c5396f..5ddf71d4 100644 --- a/tests/RestfulHookMenuTestCase.test +++ b/tests/RestfulHookMenuTestCase.test @@ -139,26 +139,31 @@ class RestfulHookMenuTestCase extends RestfulCurlBaseTestCase { 'path' => 'api/v1.1/articles', 'version_header' => NULL, 'expected_version' => array(1, 1), + 'expected_resource' => 'articles', ), array( 'path' => 'api/v1/articles', 'version_header' => NULL, 'expected_version' => array(1, 7), + 'expected_resource' => 'articles', ), array( 'path' => 'api/articles', 'version_header' => 'v1', 'expected_version' => array(1, 7), + 'expected_resource' => 'articles', ), array( 'path' => 'api/articles', 'version_header' => 'v1.0', 'expected_version' => array(1, 0), + 'expected_resource' => 'articles', ), array( 'path' => 'api/articles', 'version_header' => NULL, 'expected_version' => array(2, 1), + 'expected_resource' => 'articles', ), ); @@ -173,6 +178,7 @@ class RestfulHookMenuTestCase extends RestfulCurlBaseTestCase { $resource_manager = new \Drupal\restful\Resource\ResourceManager($request); drupal_static_reset('Drupal\restful\Resource\ResourceManager::getVersionFromRequest'); $this->assertEqual($resource_manager->getVersionFromRequest(), $test_item['expected_version'], sprintf('%s resolves correctly.', $test_item['path'])); + $this->assertEqual($resource_manager->getResourceIdFromRequest(), $test_item['expected_resource'], sprintf('Resource name obtained correctly from %s.', $test_item['path'])); } }