NEW Aliases can now be defined for DataObject endpoints (#80)

* 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>
This commit is contained in:
Sander Hagenaars 2019-07-15 00:03:51 +02:00 committed by Guy Marriott
parent e0f4e5684f
commit 0b734c21c6
2 changed files with 34 additions and 7 deletions

View File

@ -8,11 +8,11 @@ env:
matrix:
include:
- php: 5.6
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.3.x-dev PHPCS_TEST=1 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

View File

@ -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
@ -173,7 +185,7 @@ class RestfulServer extends Controller
*/
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;
@ -384,7 +396,7 @@ class RestfulServer extends Controller
$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
@ -654,7 +666,7 @@ class RestfulServer extends Controller
$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) {
@ -885,4 +897,19 @@ class RestfulServer extends Controller
{
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);
}
}