WiseLoop PHP REST Services 3.3.1
RESTful web service API framework for PHP
wlRestService Class Reference

Public Member Functions

 __construct ()
 getRequest ()
 getData ()
 setData ($data)
 registerController ($restController)
 registerTrigger ($restTrigger, $event)
 registerOutputHandler ($restOutputHandler)
 setDefaultOutputHandlerByExtension ($extension)
 loadSettings ($iniSettingsFilePath)
 setLogDir ($dir)
 setCacheDir ($dir, $cacheTime)
 getSettings ()
 run ()

Private Member Functions

 log ($message)
 applySettings ()
 setRoute ($route)
 runTriggers ($eventType)
 serve ($data, $statusCode=200, $contentType= 'text/html')
 serveError ($ex)
 computeOutputHandler ()

Private Attributes

 $_registeredControllers
 $_registeredTriggers
 $_registeredOutputHandlers
 $_defaultOutputHandlerExtension
 $_request
 $_data
 $_settings
 $_logger
 $_cache

Detailed Description

WiseLoop RestService class definition
This class represents the actual service, it is responsible for parsing the request and serving the data back to the requester.
In order to obtain a RESTful service the developer must instantiate this class, registers some controllers and call the wlRestService::run method.

$service = new wlRestService();
$service->registerController(new sampleHelloRestControllerV1());
$service->run();

wlRestService class instantiation has to be done inside the endpoint PHP script file without other outputs generated.

Author:
WiseLoop

Constructor & Destructor Documentation

__construct ( )

Constructor.
Creates a wlRestService object, loads rest-settings.ini settings file and registers by default JSON, XML, PHP, ARR and PLAIN output handlers.


Member Function Documentation

applySettings ( ) [private]
computeOutputHandler ( ) [private]

Lookup for the proper output handler considering the request extension and the registered output handlers.

Returns:
wlRestOutputHandler|null
getData ( )

Returns the service data as it was computed by the corresponding service controller.
This method can be used by triggers registered with wlRestTrigger::ON_DATA_EVENT in order to do additional general processing of data before sending.

Returns:
mixed
getRequest ( )

Returns the current request.

Returns:
wlRestRequest
getSettings ( )

Returns the service settings.

Returns:
wlSettings
loadSettings ( iniSettingsFilePath)

Parses and loads an .ini settings file. The .ini file must store the settings as key/value pairs (key=value) just like php.ini file does.
The new loaded data is merged into the existing settings data, the new keys will overwrite the pre-existing ones.

Parameters:
string$iniSettingsFilePath
log ( message) [private]

Logs a message.

Parameters:
string | array$message
registerController ( restController)

Registers a service controller.

Parameters:
wlRestController$restController
Exceptions:
wlRestException
registerOutputHandler ( restOutputHandler)

Register an output handler.

Parameters:
wlRestOutputHandler$restOutputHandler
Exceptions:
wlRestException
registerTrigger ( restTrigger,
event 
)

Registers a trigger.

Parameters:
wlRestTrigger$restTrigger
int$event
Exceptions:
wlRestException
run ( )

Runs the service.
This is the most important method and each service should call it inside the endpoint PHP script.

Returns:
void
runTriggers ( eventType) [private]

Runs all the registered triggers linked with the specified event.

Parameters:
string$eventType
Returns:
array
See also:
wlRestTrigger, wlRestTrigger::ON_REQUEST_EVENT, wlRestTrigger::ON_DATA_EVENT, wlRestTrigger::ON_RENDER_EVENT, wlRestTrigger::ON_ERROR_EVENT
serve ( data,
statusCode = 200,
contentType = 'text/html' 
) [private]

Serves the data returned by the service controller by setting the proper headers and echoing it out.

Parameters:
mixed$datathe data to be served
int$statusCodeheader status code
string$contentTypeheader content type
serveError ( ex) [private]

Serves an exception by setting the proper headers and echoing it out.
The service data is set to the exception and if there are any wlRestTrigger::ON_ERROR_EVENT registered triggers, they will be used to format the exception; otherwise a default exception formatting will be provided.

Parameters:
wlRestException$ex
setCacheDir ( dir,
cacheTime 
)

Activates the cache feature.

Parameters:
string$dirthe full directory path where the cache will be stored
int$cacheTimecaching time expressed in seconds
setData ( data)

Injects data into the service to be served.
This method can be used by triggers registered with wlRestTrigger::ON_DATA_EVENT in order to inject back the processed data before sending.

Parameters:
mixed$data
setDefaultOutputHandlerByExtension ( extension)

Sets the extension that will be used to get the default output handler in case if no extension is provided to the request URL.

Parameters:
string$extension
setLogDir ( dir)

Activates the logger and sets its directory.

Parameters:
string$dirthe full directory path where log will be generated
setRoute ( route) [private]

Sets the URL route format.
This method is used by wlRestService::applySettings to parse the route specified in the settings file.

Parameters:
array | string$routethe route format

Field Documentation

$_cache [private]

Type: wlRestCache cache handler; it uses file system to manage cache for various data returned by GET in an associative structure pair (key, data).

$_data [private]

Type: mixed data returned by the service

$_defaultOutputHandlerExtension [private]

Type: string the extension of the default output handler

$_logger [private]

Type: wlRestLogger current logger; if not null all the exceptions will be logged

$_registeredControllers [private]

Type: array of wlRestController associative array of registered service controllers having as key the name and version of the controller

$_registeredOutputHandlers [private]

Type: array of wlRestOutputHandler associative array of registered output handlers having as key the extension of the handler

$_registeredTriggers [private]

Type: array of wlRestTrigger list of registered triggers

$_request [private]

Type: wlRestRequest the current request object

$_settings [private]

Type: wlSettings service settings
File rest-settings.ini is loaded by default when creating the service.

 All Data Structures Functions Variables