Skip to content

Commit

Permalink
[FEATURE] Allow getting resource name from resource manager (#940)
Browse files Browse the repository at this point in the history
The ResourceManager class provides methods to get version information and even create a new
instance of a plugin based on the current request. However, the methods that help in getting
the resource id (such as ::getMenuItem and getPageArguments) are protected.

Since we are already able to get information on version, we should also be able to get the
resource id using the resource manager class. This commit adds a method to do that.
  • Loading branch information
hussainweb authored and e0ipso committed Sep 4, 2016
1 parent 8bac122 commit 101acd6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Resource/ResourceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
Expand Down
8 changes: 8 additions & 0 deletions src/Resource/ResourceManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
6 changes: 6 additions & 0 deletions tests/RestfulHookMenuTestCase.test
Original file line number Diff line number Diff line change
Expand Up @@ -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',
),
);

Expand All @@ -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']));
}

}
Expand Down

0 comments on commit 101acd6

Please sign in to comment.