Service
Service is an abstract class which encapsulates most work of normal services (which consists of Start/Stop of programs and Load/Save of configuration files).
New Services have to derive from Service and have to implement the methods loadConfig() and saveConfig(). loadConfig() will be called, when the web interface opens the service. loadConfig() must sent a JSONResult object to the browser. saveConfig() will be called, when the user clicks on the button save. All input widgets (which have an ID) are available as associative array (in $_POST).
The derived class has to set the variable $init to the absolute path of the init script. The init script have to support the parameters start, stop and status (see YourService).
Self-defined actions (e.g. Buttons) have to be registered by calling registerActionHandler($name). The class has to implement the method named $name.
The file Service.php includes ConfigFile.php and JSONResult.php.
Methods
| Name | Parameter | Return value | Description |
|---|---|---|---|
| loadConfig | Loads configuration files | ||
| saveConfig | Saves configuration files | ||
| registerActionHandler | String $name | Bool | Registers a new action handler |
Variables
| Name | Type | Description |
|---|---|---|
| $init | String | Absolute path of the init script |
Examples
<?php // Include Service.php, ConfigFile.php, JSONResult.php require_once('Service.php'); class Showcase extends Service { protected $init = '/etc/init.d/showcase'; public function __construct() { $this->registerActionHandler("button"); } public function loadConfig() { $cf = new ConfigFile(); $cf->loadConfig(); echo $cf; } public function saveConfig() { $cf = new ConfigFile(); $cf->setValues($_POST); $cf->setValue('listbox', array('First', 'Second', 'Third')); $cf->setSectionValue('datarealm', 'listbox', array('First entry', 'Second entry', 'Third entry')); $cf->storeConfig(); $result = new JSONResult(); $result->setResult(true); echo $result; } protected function button() { $result = new JSONResult(); $result->setResult(true); $result->setMessage('Button has been clicked'); $result->setData('label', 'The button has been clicked'); echo $result; } } $service = new Showcase();