API Methods

SkynetEventListenerAbstract base class offers to your listener some of useful API method to use.

Most important of them are request and response objects, but there is more:

Request and Response


$this->request

This is the main Request object

$this->response

This is the main Response object

$this->requestsData[]

Read only array with requests fields indexed by fields keys

$this->responseData[]

Read only array with response fields indexed by fields keys


Registry


$this->reg_set('foo', 'bar');

Registry option set

$foo = $this->reg_get('foo');

Registry option get


Options


$this->opt_set('foo', 'bar');

System option set

$foo = $this->opt_get('foo');

System option get


Monits and Logs


$this->addMonit('monit message');

Creates monit message

$this->addLog('log message');

Creates log message


Debugging


$this->debug->dump($foo);

Debugs (dumps) variable/array/object

$this->debug->txt('debug message');

Adds debug message


Clusters informations


$this->senderClusterUrl

Address of request/response sender

$this->receiverClusterUrl

Address of request/response receiver

$this->myAddress

Address of actual cluster


Events and connections informations


$this->eventName

Name of actual event

$this->context

Name of actual context

$this->connId

Number of actual connection

$this->sender

True if listener was called from request sender, False if listener was called from responder


Core objects


$this->auth

Authorization object

$this->db

Database object

$this->verifier

Verifier object

Of course, you can use more of Skynet objects, like Logger, Emailer or Console by create its instances.
Full API reference you will find in API Documentation.

Example of API methods usage:

/src/SkynetUser/MyListener.php:
public function onResponse($context)
{
  if($context == "beforeSend")
  {
    /* If request field 'foo' is set */
    if($this->request->get('foo') !== null)
    {
      /* get value of request field 'foo' */
      $foo = $this->request->get('foo');
      
      /* if request field 'foo' has value 'bar' */ 
      if($foo == 'bar')
      {
        /* set response field 'bar' with value 'Foo is Bar' */
        $this->response->set('bar', 'Foo is Bar');
        
        /* set monit to show about receive 'bar' */
        $this->addMonit('Received Bar');
        
        /* set registry field 'foo' to 'bar' if actual value is not 'bar' */
        if($this->reg_get('foo') != 'bar')
        {
          $this->reg_set('foo', 'bar');
        }
      
      /* if request field 'foo' is not 'bar' */
      } else {
        
        /* set response and debug field 'foo' */
        $this->response->set('bar', 'Foo is not Bar');
        $this->debug->txt('Foo is not Bar');
        $this->debug->dump($foo);
      }
      
    }
    
    /* set monits about sender and receiver */
    $this->addMonit('Request received from:'. $this->receiverClusterUrl);
    $this->addMonit('Sending response from:'. $this->senderClusterUrl);
    
    /* save log */
    $this->addLog('Log from cluster: '.$this->myAddress.' from event: '.$this->eventName.' with context: '.$this->context);
    
    /* check option 'sleep' from options table */
    if($this->opt_get('sleep') == 0)
    {
      $this->addLog('My cluster is not sleeped');
      
      /* if code here was called from responder then save log */
      if(!$this->sender)
      {
        $this->addLog('Log saved from responder');
      }
    }
    
    /* generate monit with actual response fields */
    $responseString = '';
    foreach($this->responseData as $key => $value)
    {
      $responseString.= $key.':'.$value.',';      
    }
    $this->addMonit('Actual response fields:'. $responseString);
  } 
}
Full API Documentation is included in Skynet packages available on GitHub.