Skip to content

Commit

Permalink
NEW Aliases can now be defined for DataObject endpoints (#80)
Browse files Browse the repository at this point in the history
* set endpoint_aliases on RestfulServer to specify fixed aliases for exposed dataobjects

* removed wrong use statements from older project

* better docblock

* use correct obj ClassName in RestfulServer::updateDataObject

* added findClassNameEndpoint method, replaced getEndpointAlias()

* getDataFormatter() - find correct endpoint instead of request param ClassName

* Better docblock

Co-Authored-By: Guy Marriott <guy@scopey.co.nz>

* Better docblock

Co-Authored-By: Guy Marriott <guy@scopey.co.nz>

* Return type hint in findClassNameEndpoint method

Co-Authored-By: Guy Marriott <guy@scopey.co.nz>

* renamed endpoint method to resolveEndpoint

* unsanitiseClassName in resolveEndpoint method

* renamed resolveEndpoint to resolveClassName. Take $request as param instead of string

* better docbloc

* Remove unneccesary unsanitiseClassName call

Co-Authored-By: Guy Marriott <guy@scopey.co.nz>

* changed docblocks to satisfy codesniffer

* Drop PHP 5.6 and 7.0 from travis


Co-authored-by: Guy Marriott <guy@scopey.co.nz>
  • Loading branch information
sanderha and ScopeyNZ committed Jul 14, 2019
1 parent e0f4e56 commit 0b734c2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ env:

matrix:
include:
- php: 5.6
- php: 7.1
env: DB=MYSQL RECIPE_VERSION=4.3.x-dev PHPCS_TEST=1 PHPUNIT_TEST=1
- php: 7.0
env: DB=MYSQL RECIPE_VERSION=4.4.x-dev PHPUNIT_TEST=1
- php: 7.1
env: DB=MYSQL RECIPE_VERSION=4.4.x-dev PHPUNIT_TEST=1
- php: 7.2
env: DB=PGSQL RECIPE_VERSION=4.3.x-dev PHPUNIT_COVERAGE_TEST=1
- php: 7.2
env: DB=MYSQL RECIPE_VERSION=4.4.x-dev PHPUNIT_TEST=1
Expand Down
33 changes: 30 additions & 3 deletions src/RestfulServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ class RestfulServer extends Controller
*/
private static $default_extension = "xml";

/**
* Custom endpoints that map to a specific class.
* This is done to make the API have fixed endpoints,
* instead of using fully namespaced classnames, as the module does by default
* The fully namespaced classnames can also still be used though
* Example:
* ['mydataobject' => MyDataObject::class]
*
* @config array
*/
private static $endpoint_aliases = [];

/**
* Whether or not to send an additional "Location" header for POST requests
* to satisfy HTTP 1.1: https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
Expand Down Expand Up @@ -173,7 +185,7 @@ public static function parseRelationClass($class)
*/
public function index(HTTPRequest $request)
{
$className = $this->unsanitiseClassName($request->param('ClassName'));
$className = $this->resolveClassName($request);
$id = $request->param('ID') ?: null;
$relation = $request->param('Relation') ?: null;

Expand Down Expand Up @@ -384,7 +396,7 @@ protected function getDataFormatter($includeAcceptHeader = false, $className = n
$accept = $this->request->getHeader('Accept');
$mimetypes = $this->request->getAcceptMimetypes();
if (!$className) {
$className = $this->unsanitiseClassName($this->request->param('ClassName'));
$className = $this->resolveClassName($this->request);
}

// get formatter
Expand Down Expand Up @@ -654,7 +666,7 @@ protected function updateDataObject($obj, $formatter)
$rawdata = $this->request->postVars();
}

$className = $this->unsanitiseClassName($this->request->param('ClassName'));
$className = $obj->ClassName;
// update any aliased field names
$data = [];
foreach ($rawdata as $key => $value) {
Expand Down Expand Up @@ -885,4 +897,19 @@ protected function getMember()
{
return Security::getCurrentUser();
}

/**
* Checks if given param ClassName maps to an object in endpoint_aliases,
* else simply return the unsanitised version of ClassName
*
* @param HTTPRequest $request
* @return string
*/
protected function resolveClassName(HTTPRequest $request): string
{
$className = $request->param('ClassName');
$aliases = self::config()->get('endpoint_aliases');

return $aliases[$className] ?? $this->unsanitiseClassName($className);
}
}

0 comments on commit 0b734c2

Please sign in to comment.