abstract class Service extends BaseHook implements ServiceInterface

Base class for Webservice Hooks.

Use Service::setupMethods to define the available methods and define a public function for each method.

This function will be called with the defined input parameters.

Example

<?php
// Your module is in <XELOS root>/vendor/example/modules/my_module, e.g. created with:
// > php xf kickstart example/my_module
// The Service implementation is in the folder hook/webservice of your module.
// Register your hook in the module's config.yml (remove the #):

#provided_hooks:
#  hello: {hook: webservice.service, type: object, internal: no, active: yes, title: 'Hello Service', desc: 'Greet the developer'}

namespace XELOS\Vendor\Example\Modules\MyModule\Hook\Webservice;

use XELOS\Framework\XF;
use XELOS\Modules\Webservice\Hook\Base\Service;
use XELOS\Modules\Webservice\Hook\Base\Service\Method;
use XELOS\Modules\Webservice\Hook\Base\Service\Types;

class HelloService extends Service {
    // The implementation of v2 supports the greeting parameter
    public function hello($name = 'World', $greeting = 'Hello') {
        $XF = XF::instance();
        $name = $XF->lib->sec->input($name) ?: 'World';
        $greeting = $XF->lib->sec->input($greeting) ?: 'Hello';
        return "{$greeting}, {$name}";
    }

    public function complexGreeting($greetingHash = []) {
        $XF = XF::instance();

        $parameters = $greetingHash;
        // The WSDL definition of the complex types is interpreted as an array of associative arrays.
        // The first entry and only entry is our defined input type $helloInput from HelloService::init
        if (\is_array($greetingHash) && \count($greetingHash) === 1) {
            $parameters = \array_pop($parameters);
        }
        // Extract the values from the input
        [
            'greeting'    => $greeting,
            'name'        => $name,
            'punctuation' => $punctuation
        ] = $parameters;
        $punctuation = $XF->lib->sec->input($punctuation) ?: '';

        return $this->hello($name, $greeting) . $punctuation;
    }

    public function setupMethods(): array {
        // Just a simple return with no inputs.
        $hiMethod = new Method('hi');
        $hiMethod->addOutputParameter('greeting', Types::STRING);
        $hiMethod->setCallback(function () {
            return 'Hi';
        });

        $helloMethod = new Method('hello');
        $helloMethod->addInputParameter('name', Types::STRING);
        $helloMethod->addOutputParameter('greeting', Types::STRING);

        // Extended the hello method with a greeting parameter
        $helloMethod->addInputParameter('greeting', Types::STRING)
                                ->setCallback([$this, 'hello']);
        // Register version 2, the public function expects a v2 suffix.
        $helloMethodV2 = clone $helloMethod;
        $helloMethodV2->setVersion(2);

        $complexMethod = new Method('another_greeting');
        // You can define objects / hashes as input and output parameters
        $helloInput = new Service\ParameterObject();
        $helloInput->addMember('greeting', Types::STRING);
        $helloInput->addMember('name', Types::STRING);
        $helloInput->addMember('punctuation', Types::STRING);
        $complexMethod->addInputParameter('greetingHash', Types::OBJECT, $helloInput);
        $complexMethod->addOutputParameter('greeting', Types::STRING);
        $complexMethod->setCallback([$this, 'complexGreeting']);

        return [$helloMethod, $helloMethodV2, $complexMethod];
    }
}

Properties

Controller $mod

Instance providing the hook

from  BaseHook
array $_hook_info

Hook Information "id" => "4914" "instance_id" => "api_microsoft" "name" => "oauth_client" "hook" => "webservice.oauth_client" "type" => "object" "registry_name" => "oauth_client" "function_name" => "" "internal" => "no" "dep_mode" => "local" "active" => "yes" "title" => "Basic functions" "description" => "" "options" => ""

from  BaseHook

Methods

mixed
__call(string $name, array $arguments)

Checks if a callback is defined on the Method and calls it.

string
getHookDescription()

No description

from  BaseHook
string
getHookIdentifier()

Get Hook identifier consists of instance_id puncto separator and hook name Ex. instance_id.name

from  BaseHook
array
getHookInfoValue(bool $key = false)

Return Hook information (ID, InstanceID, Name, Hook Type, Object Type, ...)

from  BaseHook
string
getHookInstance()

No description

from  BaseHook
string
getHookName()

No description

from  BaseHook
string
getHookTitle()

No description

from  BaseHook
callable|null
getMethodCallback(string $name)

Returns the callback of a method if it is defined.

getObject()

No description

from  BaseHook
get_service_description()

Returns the registered webservice methods.

init()

Initialization after construction with access to $this->mod

bool
isAvailable()

Returns true if this hook is available otherwise false.

array
setupMethods()

Define and return your webservice methods.

Details

at line 124
mixed __call(string $name, array $arguments)

Checks if a callback is defined on the Method and calls it.

Parameters

string $name
array $arguments

Return Value

mixed

Exceptions

RuntimeException

Thrown if the method is not defined.

See also

Method::setCallback

in BaseHook at line 76
string getHookDescription()

No description

Return Value

string

in BaseHook at line 55
string getHookIdentifier()

Get Hook identifier consists of instance_id puncto separator and hook name Ex. instance_id.name

Return Value

string

in BaseHook at line 45
array getHookInfoValue(bool $key = false)

Return Hook information (ID, InstanceID, Name, Hook Type, Object Type, ...)

Parameters

bool $key

Return Value

array

in BaseHook at line 69
string getHookInstance()

No description

Return Value

string

in BaseHook at line 62
string getHookName()

No description

Return Value

string

in BaseHook at line 83
string getHookTitle()

No description

Return Value

string

at line 140
callable|null getMethodCallback(string $name)

Returns the callback of a method if it is defined.

Parameters

string $name

Method name

Return Value

callable|null

in BaseHook at line 88
getObject()

No description

at line 168
get_service_description()

Returns the registered webservice methods.

See also

setupMethods Please use setupMethods to define and return your webservice methods.

Examples

[
'hello' => [
'input' => ['name' => 'string],
'output' => ['greeting' => 'string],
]

at line 153
init()

Initialization after construction with access to $this->mod

at line 185
bool isAvailable()

Returns true if this hook is available otherwise false.

Return Value

bool

at line 112
abstract array setupMethods()

Define and return your webservice methods.

Return Value

array