The WebAbility® Network Developers - Documentation
WebAbility Site
\patterns\Dispatcher reference

I. Structure:


- namespace:
\patterns\

- Direct superclass:
\core\WAObject

- Direct known subclasses:
none

II. Description:


This is a pattern used to dispatch messages and code upon orders and status of parameters.
The class is a sort a super switch to call every respective method upon parameters.

Having a static set of dispatched messages is not really a big deal, but when your set of messages grow, is dynamic or auto-configuring,
with a mutating catalog of posibilities, the dispatcher becomes very usefull.

A typical use is to dispatch diverse type of emails based on the type of the message we want to send to the user, for instance
a welcome email, a thank you email, a social notification email, a purchase confirmation email, etc.


Example:



// creates some messages dispatcher class
class myDispatcher extends \patterns\Dispatcher
{
function __construct()
{
parent::__construct(array('cnx' => 'Connect', 'dx' => 'Disconnect', 'prt' => 'Print'));
}

private function Connect()
{ connect to the thing
}

private function Disconnect()
{ disconnect from the thing
}

private function Print($message)
{ print to the thing
}

}

// cnx, dx, prt are the 3 known messages of the dispatcher calling respective methods.

// gets the dispatcher instance
$D = new myDispatcher();

// Dispatch some messages
$F->dispatch('cnx');
$F->dispatch('prt', 'Hello World');
$F->dispatch('dx');




III. Reference:


3.1. Contructor:


\dispatcher\Dispatcher($catalog)
Creates the instance of your dispatcher with the catalog of known messages with each of the possible methods to call.

3.2. Constants:

There is no constant into this class.


3.3. Attributes:

There is no attributes into this class.


3.4. Methods:


public dispatch($messageid, [$param1, ...])
This method is used to dispatcha message to the corresponding method based on its messageid. The parameters will be passed to the method.

public registerEntry($messageid, $method)
This method is used to add a new message entry in the catalog.

public unregisterEntry($messageid)
This method is used to remove a message entry from the catalog.