<?php
/**
* Events usage example.
*
* This script must be located at site root.
* It will output:
* <pre>
* ---
* firing 'event' for first...
* ExampleModule1::handler()
* ---
* firing 'event' for second...
* ExampleModule2::handler()
* ---
* firing 'event' for third...
* ExampleModule3::handler()
* ---
* firing 'event' using AMI_Event::MOD_ANY...
* ---
* ExampleModule1::commonHandler()
* ExampleModule2::commonHandler(), following handling is interrupted
* ---
* dropping $oModule2 handler and firing 'common_event' using AMI_Event::MOD_ANY...
* ExampleModule1::commonHandler()
* ExampleModule3::commonHandler()
* ---
* $aEvent['count'] = 7
* </pre>
*
* @copyright Amiro.CMS.
* @category Example
* @package AMI_Event
* @since 5.10.0
*/
require 'ami_env.php';
/**
* @var AMI_Response
*/
$oResponse->start();
abstract class ExampleModule{
protected $modId = '';
public function __construct(){
}
public function handler($name, array $aEvent, $handlerModId, $srcModId){
AMI::getSingleton('response')->write(get_class($this) . "::handler()<br />");
$aEvent['count']++;
return $aEvent;
}
public function commonHandler($name, array $aEvent, $handlerModId, $srcModId){
AMI::getSingleton('response')->write(get_class($this) . "::commonHandler()<br />");
$aEvent['count']++;
return $aEvent;
}
class ExampleModule1 extends ExampleModule{
protected $modId = 'first';
}
class ExampleModule2 extends ExampleModule{
protected $modId = 'second';
public function commonHandler($name, array $aEvent, $handlerModId, $srcModId){
AMI::getSingleton('response')->write(
get_class($this) . "::commonHandler(), following handling is interrupted<br />"
);
$aEvent['_break_event'] = true;
$aEvent['count']++;
return $aEvent;
}
}
class ExampleModule3 extends ExampleModule{
protected $modId = 'third';
}
$oModule1 = new ExampleModule1;
$oModule2 = new ExampleModule2;
$oModule3 = new ExampleModule3;
$aEvent = array('count' => 0);
$oResponse->write("<hr />firing 'event' for first...<br />");
$oResponse->write("<hr />firing 'event' for second...<br />");
$oResponse->write("<hr />firing 'event' for third...<br />");
$oResponse->write("<hr />firing 'event' using AMI_Event::MOD_ANY...<br />");
$oResponse->write("<hr />firing 'common_event' using AMI_Event::MOD_ANY...<br />");
$oResponse->write("<hr />dropping \$oModule2 handler and firing 'common_event' using AMI_Event::MOD_ANY...<br />");
$oResponse->write('<hr />$aEvent[\'count\'] = ' . $aEvent['count']);
$oResponse->send();