PHP Class RESTfulAPI

Show file Open project: colymba/silverstripe-restfulapi Class Usage Examples

Public Properties

Property Type Description
$authenticator RESTfulAPI_Authenticator Current Authenticator instance
$authority RESTfulAPI_PermissionManager Current Permission Manager instance
$queryHandler RESTfulAPI_QueryHandler Current QueryHandler instance
$serializer RESTfulAPI_Serializer Current serializer instance

Protected Properties

Property Type Description
$instance RESTfulAPI Current RESTfulAPI instance

Public Methods

Method Description
__construct ( ) Constructor.
acl ( SS_HTTPRequest $request ) Handles Access Control methods get response from API PermissionManager then passes it on to $answer()
answer ( string $json = null, boolean $corsPreflight = false ) Output the API response to client then exit.
api_access_control ( string | DataObject $model, string $httpMethod = 'GET' ) : boolean Checks a class or model api access depending on access_control_policy and the provided model.
auth ( SS_HTTPRequest $request ) Handles authentications methods get response from API Authenticator then passes it on to $answer()
error ( RESTfulAPI_Error $error ) Handles formatting and output error message then exit.
getqueryHandler ( ) : RESTfulAPI_QueryHandler Returns current query handler instance
getserializer ( ) : RESTfulAPI_Serializer Returns current serializer instance
index ( SS_HTTPRequest $request ) : string Main API hub switch All requests pass through here and are redirected depending on HTTP verb and params
init ( ) Controller inititalisation Catches CORS preflight request marked with HTTPMethod 'OPTIONS'

Private Methods

Method Description
api_access_config_check ( string $className, string $httpMethod = 'GET' ) : boolean Checks a model's api_access config.
model_permission_check ( string | DataObject $model, string $httpMethod = 'GET' ) : boolean Checks a Model's permission for the currently authenticated user via the Permission Manager dependency.
setAnswerCORS ( SS_HTTPResponse $answer ) Apply the proper CORS response heardes to an SS_HTTPResponse

Method Details

__construct() public method

...
public __construct ( )

acl() public method

Handles Access Control methods get response from API PermissionManager then passes it on to $answer()
public acl ( SS_HTTPRequest $request )
$request SS_HTTPRequest HTTP request

answer() public method

Output the API response to client then exit.
public answer ( string $json = null, boolean $corsPreflight = false )
$json string Response body
$corsPreflight boolean Set to true if this is a XHR preflight request answer. CORS shoud be enabled.

api_access_control() public static method

- 1st config check - 2nd permission check if config access passes
public static api_access_control ( string | DataObject $model, string $httpMethod = 'GET' ) : boolean
$model string | DataObject Model's classname or DataObject
$httpMethod string API request HTTP method
return boolean true if access is granted, false otherwise

auth() public method

Handles authentications methods get response from API Authenticator then passes it on to $answer()
public auth ( SS_HTTPRequest $request )
$request SS_HTTPRequest HTTP request

error() public method

Handles formatting and output error message then exit.
public error ( RESTfulAPI_Error $error )
$error RESTfulAPI_Error Error object to return

getqueryHandler() public method

Returns current query handler instance
public getqueryHandler ( ) : RESTfulAPI_QueryHandler
return RESTfulAPI_QueryHandler QueryHandler instance

getserializer() public method

Returns current serializer instance
public getserializer ( ) : RESTfulAPI_Serializer
return RESTfulAPI_Serializer Serializer instance

index() public method

Main API hub switch All requests pass through here and are redirected depending on HTTP verb and params
public index ( SS_HTTPRequest $request ) : string
$request SS_HTTPRequest HTTP request
return string json object of the models found

init() public method

Controller inititalisation Catches CORS preflight request marked with HTTPMethod 'OPTIONS'
public init ( )

Property Details

$authenticator public property

Current Authenticator instance
public RESTfulAPI_Authenticator $authenticator
return RESTfulAPI_Authenticator

$authority public property

Current Permission Manager instance
public RESTfulAPI_PermissionManager $authority
return RESTfulAPI_PermissionManager

$instance protected static property

Current RESTfulAPI instance
protected static RESTfulAPI $instance
return RESTfulAPI

$queryHandler public property

Current QueryHandler instance
public RESTfulAPI_QueryHandler $queryHandler
return RESTfulAPI_QueryHandler

$serializer public property

Current serializer instance
public RESTfulAPI_Serializer $serializer
return RESTfulAPI_Serializer