The term event and signal are used to mean the same thing in this class.
This class listens for signals that occur within an application and
notifies some other part of the system that a the signal has occurred.
Example
This maybe useful e.g. if you want to send email each time a new user is registered.
Loading The Library
To start using the signal dispatcher, load it like any other CODIGNTER library, preferably using autoload
$autoload['libraries'] = array(
'powerdispatch/signal',<------------------------------------ the dispatcher
'powerorm/orm',
'powerauth/auth'
);
The Sender
For this to happen the user registration mechanism has to issue some signals to notify
other parts of the system/application it has registered a user,
So in this case the user registration mechanism is called the sender. i.e it sends signals
To send a signal you use the \Signal::dispatch() method of the Signal class as shown in the example below
class User_model extends Base_model{
... // other methods
public function save($args){
... // saving logic
$saved_user = ...
// notify everyone that is listening that a user has been registered
$this->signal->dispatch('powerorm.model.post_save', get_class($this), $saved_user);
}
}
class User_controller extends CI_Controller{
... // logic to save user
$this->user_model->save($args);
}
Signal
A signal just a PHP string,
When coming up with a signal name try to make them a unique as possible to avoid collisions with other
parts of the application dispatching a signal having the same string for a signal.
e,g the one i have used 'powerorm.model.post_save' in the example. in which my models leave in a
package called powerorm
The Receiver
Once the user registration mechanism has issued a notification that it has registered a user, The 'Signal' class,
will notify all other parts of the application/system of this notification.
For any part of the system/application to be notified of this signals,
it has to register with the Signal class.
Once registered it becomes a receiver i.e. it receives signals.
A receiver can be any PHP function or method that is it returns true when tested by is_callable()
and callable via call_user_func()
Registering Receivers
To register recievers, create a file config/signals.php add create and array of receivers.
Just like you would CI HOOKS
Take Note of
$receivers['model.post_save'][] = ...
This allows for multiple receivers to listen for the same signal.
The order you define your array will be the execution order.
Continuing with our example the first two receivers will be notified when the registration mechanism registers a user.
Which will the perform some actions
// class method
$receivers['powerorm.model.post_save'][] = array(
'class' => 'Authauth', // the class to be invoked, leave blank if you just using a function
'function' => 'auth_check', // function / method to be called
'filename' => 'Authauth.php', // the file name containing the function or class with the method to be called
'filepath' => 'libraries' // the directory where the file is located relative to the application directory.
);
// function
$receivers['powerorm.model.post_save'][] = array(
'class' => '',
'function' => 'email',
'filename' => 'sender.php',
'filepath' => 'libraries'
'sender' => 'user_model' // in this case we only want to listen for when user_model issues a `model.post_save` signal
);
// closure function
$receivers['powerorm.model.pre_save'][] =function($sender, $params){
};
// closure function
// not here that only one receiver is listening for this signal
$receivers['powerauth.auth.login_success'] =function($sender, $params){
};
How it works
When a sender issues a signal, all the registered receivers are notified, the receivers the performs some action
Important Things To Note
Does not replace the CI Hooks but Works parallel to Hooks.
Load it early enough to be able to catch all signals.
I recommend use ci autoload and make the first in the libraries array.
Receivers are registered in a config file called config/signals.php
Registering a receiver requires same amount paramaters as hooks
Difference from CI HOOKS
A receiver can register to listen for specific senders only. see receiver example above
The receiver method get two mandatory arguments
sender -- this is the object that sent the signal
params -- This are extra arguments sent to the receiver by the sendender,
while most sender will not send extra arguments the receiver is expected accept this argument
this because the sender might start sending arguments in future
Summary
MethodsPropertiesConstantsdispatch() No public properties foundNo constants foundNo protected methods foundNo protected properties foundN/ANo private methods foundNo private properties foundN/A