Responder
- Object
- Responder
A Responder instance automatically sends notification messages to one or several instances. Responder instances can be linked so notifications are automatically transmitted from one responder to another.
INSTANCE METHODS
- namespace Responder;
- require_once 'So-o.php';
- defclass('Responder', null, 1, null, array('nextResponders'), null, array('respondTo', 'nextResponders', 'setNextResponders', 'addNextResponder', 'removeNextResponder'));
The Responder class inherits from the Object class. The instance property nextResponders holds the list of instances which have asked to be notified when an event occurs. The Responder class adds the instance messages respondTo, nextResponders, setNextResponders, addNextResponder and removeNextResponder.
nextResponders
SYNOPSIS
sendmsg($self, 'nextResponders')
DESCRIPTION
nextResponders
returns the list of responders of $self
in an array.
CODE
- function i_nextResponders($self) {
- return sendmsg($self, 'get', 'nextResponders');
- }
setNextResponders
SYNOPSIS
sendmsg($self, 'setNextResponders', $responders)
DESCRIPTION
setNextResponders
initializes the list of responders of $self
to $responders
.
$responders
is an array of instances.
CODE
- function i_setNextResponders($self, $responders) {
- return sendmsg($self, 'set', 'nextResponders', $responders);
- }
addNextResponder
SYNOPSIS
sendmsg($self, 'addNextResponder', $r)
DESCRIPTION
addNextResponder
adds $r
to the list of responders of $self
.
CODE
- function i_addNextResponder($self, $r) {
- $responders=sendmsg($self, 'get', 'nextResponders');
- if ($responders) {
- if (!in_array($r, $responders)) {
- $responders[]=$r;
- sendmsg($self, 'set', 'nextResponders', $responders);
- }
- }
- else {
- $responders=array($r);
- sendmsg($self, 'set', 'nextResponders', $responders);
- }
- return $self;
- }
removeNextResponder
SYNOPSIS
sendmsg($self, 'removeNextResponder', $r)
DESCRIPTION
removeNextResponder
removes $r
from the list of responders of $self
.
CODE
- function i_removeNextResponder($self, $r) {
- $responders=sendmsg($self, 'get', 'nextResponders');
- if ($responders) {
- $i=array_search($r, $responders);
- if ($i !== false) {
- unset($responders[$i]);
- sendmsg($self, 'set', 'nextResponders', $responders);
- }
- }
- return $self;
- }
respondTo
SYNOPSIS
sendmsg($self, 'respondTo', $msg[, $arg ...])
DESCRIPTION
CODE
If self
responds to $msg
, respondTo
sends $msg
and its parameters $arg
to $self
.
If $self
doesn't respond to $msg
or if the execution of the message returns false
, respondTo
transmits the message and its parameters to all the instances of the list of responders of $self
.
- function i_respondTo($self, $msg) {
- if (sendmsg($self, 'respondsTo', $msg) and sendmsg($self, 'perform', $msg, array_slice(func_get_args(), 2))) {
- return $self;
- }
- $responders=sendmsg($self, 'get', 'nextResponders');
- if ($responders) {
- foreach ($responders as $r) {
- sendmsg($r, 'perform', 'respondTo', array_slice(func_get_args(), 1));
- }
- }
- return $self;
- }
EXAMPLE
An instance of Button notifies the message clicked when it receives the message click:
- namespace Button;
- require_once 'So-o.php';
- require_once 'Responder.php';
- defclass('Button', $Responder, 1, null, null, null, array('click'));
- function i_click($self) {
- echo $self, ' click', PHP_EOL;
- sendmsg($self, 'respondTo', 'clicked', $self);
- }
click
displays a trace message then notifies the message clicked.
An instance of Action responds to clicked:
- namespace Action;
- require_once 'So-o.php';
- require_once 'Responder.php';
- defclass('Action', $Responder, 1, null, null, null, array('clicked'));
- function i_clicked($self, $sender) {
- echo $self . ' clicked from ', $sender, PHP_EOL;
- return false;
- }
$ php -a
php > require_once 'Button.php';
php > $btn1=sendmsg($Button, 'new');
php > sendmsg($btn1, 'click');
object(Button) click
Create an instance of Action and add it as a responder to the instance of Button:
php > require_once 'Action.php';
php > $act1=sendmsg($Action, 'new');
php > sendmsg($btn1, 'addNextResponder', $act1);
php > sendmsg($btn1, 'click');
object(Button) click
object(Action) clicked from object(Button)
clicked is automatically sent by $btn1
to $act1
.
An instance of AnotherAction responds to clicked
too:
- namespace AnotherAction;
- require_once 'So-o.php';
- require_once 'Responder.php';
- defclass('AnotherAction', $Responder, 1, null, null, null, array('clicked'));
- function i_clicked($self, $sender) {
- echo $self . ' clicked from ', $sender, PHP_EOL;
- return true;
- }
Add an instance of AnotherAction as a responder to the instance of Button:
php > require_once 'AnotherAction.php';
php > $act2=sendmsg($AnotherAction, 'new');
php > sendmsg($btn1, 'addNextResponder', $act2);
php > sendmsg($btn1, 'click');
object(Button) click
object(Action) clicked from object(Button)
object(AnotherAction) clicked from object(Button)
clicked is sent by $btn1
to $act1
then to $act2
.
Responders can build a chain:
php > sendmsg($btn1, 'removeNextResponder', $act2);
php > sendmsg($btn1, 'click');
object(Button) click
object(Action) clicked from object(Button)
$btn1
has only $act1
as responder. Add $act2
as a responder to $act1
:
php > sendmsg($act1, 'addNextResponder', $act2);
php > sendmsg($btn1, 'click');
object(Button) click
object(Action) clicked from object(Button)
object(AnotherAction) clicked from object(Button)
In this configuration, clicked is sent to the responder of $btn1
then to the responder of $act1
.
If a responder returns true
, the chain is broken:
php > sendmsg($act1, 'removeNextResponder', $act2);
php > sendmsg($btn1, 'removeNextResponder', $act1);
php > sendmsg($btn1, 'click');
object(Button) click
php > sendmsg($btn1, 'addNextResponder', $act2);
php > sendmsg($act2, 'addNextResponder', $act1);
php > sendmsg($btn1, 'click');
object(Button) click
object(AnotherAction) clicked from object(Button)
clicked is sent by $btn1
to $act2
which returns true
and blocks sending the message to $act1
.
Comments