mirror of
https://github.com/Mibew/mibew.git
synced 2024-11-15 08:34:11 +03:00
Update EventDispatcher class
There is no need to register events any more.
This commit is contained in:
parent
62728e608d
commit
219aa4c9b1
@ -31,37 +31,7 @@ class EventDispatcherTest extends PHPUnit_Framework_TestCase {
|
||||
/**
|
||||
* @depends testGetInstance
|
||||
*/
|
||||
public function testRegisterEvent($dispatcher) {
|
||||
// Try to register new event
|
||||
$this->assertTrue($dispatcher->registerEvent('some_test_event'));
|
||||
$this->assertTrue($dispatcher->registerEvent('some_another_test_event'));
|
||||
|
||||
// Try to register already registered event
|
||||
// Following code wait for trigger user error, which converts by PHPUnit to an
|
||||
// Exception
|
||||
try{
|
||||
$dispatcher->registerEvent('some_test_event');
|
||||
$this->fail("Error expected!");
|
||||
} catch(Exception $e) {}
|
||||
return $dispatcher;
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testRegisterEvent
|
||||
*/
|
||||
public function testAttachListener($dispatcher) {
|
||||
// Try to attach listener to unregistered event
|
||||
// Following code wait for trigger user error, which converts by PHPUnit to an
|
||||
// Exception
|
||||
try{
|
||||
$dispatcher->attachListener(
|
||||
'unreginstered_event',
|
||||
self::$plugin,
|
||||
'testEventListener'
|
||||
);
|
||||
$this->fail("Error expected!");
|
||||
} catch(Exception $e) {}
|
||||
|
||||
// Try to Attach wrong method as listener to event
|
||||
// Following code wait for trigger user error, which converts by PHPUnit to an
|
||||
// Exception
|
||||
@ -74,7 +44,7 @@ class EventDispatcherTest extends PHPUnit_Framework_TestCase {
|
||||
$this->fail("Error expected!");
|
||||
} catch(Exception $e) {}
|
||||
|
||||
// Try to attach listener to registered event
|
||||
// Try to attach listener to event
|
||||
$this->assertTrue(
|
||||
$dispatcher->attachListener(
|
||||
'some_test_event',
|
||||
@ -83,7 +53,7 @@ class EventDispatcherTest extends PHPUnit_Framework_TestCase {
|
||||
)
|
||||
);
|
||||
|
||||
// Try to attach listener to registered event
|
||||
// Try to attach listener to event
|
||||
$this->assertTrue(
|
||||
$dispatcher->attachListener(
|
||||
'some_another_test_event',
|
||||
@ -99,18 +69,6 @@ class EventDispatcherTest extends PHPUnit_Framework_TestCase {
|
||||
* @depends testAttachListener
|
||||
*/
|
||||
public function testDetachListener($dispatcher) {
|
||||
// Try to detach listener for unregistered event.
|
||||
// Following code wait for trigger user error, which converts by PHPUnit to an
|
||||
// Exception
|
||||
try{
|
||||
$dispatcher->detachListener(
|
||||
'unreginstered_event',
|
||||
self::$plugin,
|
||||
'testEventListener'
|
||||
);
|
||||
$this->fail("Error expected!");
|
||||
} catch(Exception $e) {}
|
||||
|
||||
// Try to detach listner that was not attached to registerd event
|
||||
$this->assertFalse(
|
||||
$dispatcher->detachListener(
|
||||
@ -135,14 +93,6 @@ class EventDispatcherTest extends PHPUnit_Framework_TestCase {
|
||||
* @depends testDetachListener
|
||||
*/
|
||||
public function testTriggerEvent($dispatcher) {
|
||||
// Try to trigger unregistered event
|
||||
// Following code wait for trigger user error, which converts by PHPUnit to an
|
||||
// Exception
|
||||
try{
|
||||
$dispatcher->triggerEvent('unregistered_event');
|
||||
$this->fail("Error expected!");
|
||||
} catch(Exception $e) {}
|
||||
|
||||
// Try to trigger registered event
|
||||
$test_array = array();
|
||||
$dispatcher->triggerEvent('some_another_test_event', $test_array);
|
||||
|
@ -5,27 +5,15 @@
|
||||
*/
|
||||
Class PhpunitAutotestPluginManagerPlugin extends Plugin{
|
||||
|
||||
public $eventsRegistered = false;
|
||||
public $listenersRegistered = false;
|
||||
|
||||
public function getWeight() {
|
||||
return 10;
|
||||
}
|
||||
|
||||
public function registerEvents() {
|
||||
$this->eventsRegistered = true;
|
||||
$this->checkRegistration();
|
||||
}
|
||||
|
||||
public function registerListeners() {
|
||||
$this->listenersRegistered = true;
|
||||
$this->checkRegistration();
|
||||
}
|
||||
|
||||
public function checkRegistration() {
|
||||
if ($this->eventsRegistered && $this->listenersRegistered) {
|
||||
$GLOBALS['phpunit_autotest_plugin_manager'] = true;
|
||||
}
|
||||
$GLOBALS['phpunit_autotest_plugin_manager'] = true;
|
||||
}
|
||||
|
||||
public function testEventListener(&$vars) {
|
||||
|
@ -19,13 +19,8 @@ Class RequestProcessorTestPlugin extends Plugin{
|
||||
return 10;
|
||||
}
|
||||
|
||||
public function registerEvents() {
|
||||
$dispatcher = EventDispatcher::getInstance();
|
||||
$dispatcher->registerEvent('testRequestProcessorCallback');
|
||||
}
|
||||
|
||||
public function registerListeners() {
|
||||
$registered_events = array(
|
||||
$processor_events = array(
|
||||
'testRequestReceived',
|
||||
'testRequestError',
|
||||
'testResponseReceived',
|
||||
@ -34,7 +29,7 @@ Class RequestProcessorTestPlugin extends Plugin{
|
||||
'testRequestProcessorCallback'
|
||||
);
|
||||
$dispatcher = EventDispatcher::getInstance();
|
||||
foreach ($registered_events as $event) {
|
||||
foreach ($processor_events as $event) {
|
||||
$dispatcher->attachListener($event, $this, $event);
|
||||
}
|
||||
}
|
||||
|
@ -72,16 +72,15 @@ Class EventDispatcher {
|
||||
* @see Plugin::getWeight()
|
||||
*/
|
||||
public function attachListener($event_name, Plugin $plugin, $listener, $priority = null){
|
||||
// Check event exists
|
||||
if (! array_key_exists($event_name, $this->events)) {
|
||||
trigger_error("Event '{$event_name}' does not exists!", E_USER_WARNING);
|
||||
return false;
|
||||
}
|
||||
// Check method is callable
|
||||
if (! is_callable(array($plugin, $listener))) {
|
||||
trigger_error("Method '{$listener}' is not callable!", E_USER_WARNING);
|
||||
return false;
|
||||
}
|
||||
// Create empty array for event listener if it not exists
|
||||
if (! array_key_exists($event_name, $this->events)) {
|
||||
$this->events[$event_name] = array();
|
||||
}
|
||||
// Check priority
|
||||
if (is_null($priority)) {
|
||||
$priority = $plugin->getWeight();
|
||||
@ -106,7 +105,6 @@ Class EventDispatcher {
|
||||
public function detachListener($event_name, Plugin $plugin, $listener){
|
||||
// Check event exists
|
||||
if (! array_key_exists($event_name, $this->events)) {
|
||||
trigger_error("Event '{$event_name}' does not exists!", E_USER_WARNING);
|
||||
return false;
|
||||
}
|
||||
// Search event and $plugin->$listener
|
||||
@ -120,23 +118,6 @@ Class EventDispatcher {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers new event
|
||||
*
|
||||
* @param string $event_name Event's name
|
||||
* @return boolean true on success or false on failure
|
||||
*/
|
||||
public function registerEvent($event_name){
|
||||
// Check event exists
|
||||
if (array_key_exists($event_name, $this->events)) {
|
||||
trigger_error("Event '{$event_name}' already exists!", E_USER_WARNING);
|
||||
return false;
|
||||
}
|
||||
// register event
|
||||
$this->events[$event_name] = array();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Triggers the event
|
||||
*
|
||||
@ -145,10 +126,9 @@ Class EventDispatcher {
|
||||
* @return boolean true on success or false on failure
|
||||
*/
|
||||
public function triggerEvent($event_name, &$arguments = array()){
|
||||
// Check event exists
|
||||
// Check event listeners exists
|
||||
if (! array_key_exists($event_name, $this->events)) {
|
||||
trigger_error("Event '{$event_name}' does not exists!", E_USER_WARNING);
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
// Sorting listeners by priority
|
||||
uksort($this->events[$event_name], 'strnatcmp');
|
||||
@ -158,6 +138,7 @@ Class EventDispatcher {
|
||||
$listener = $event['listener'];
|
||||
$plugin->$listener($arguments);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -41,11 +41,6 @@ abstract Class Plugin {
|
||||
*/
|
||||
abstract public function getWeight();
|
||||
|
||||
/**
|
||||
* Register events
|
||||
*/
|
||||
abstract public function registerEvents();
|
||||
|
||||
/**
|
||||
* Register listeners
|
||||
*
|
||||
|
@ -146,7 +146,6 @@ Class PluginManager {
|
||||
uksort($loading_queue, 'strnatcmp');
|
||||
// Add events and listeners
|
||||
foreach ($loading_queue as $plugin) {
|
||||
$plugin->registerEvents();
|
||||
$plugin->registerListeners();
|
||||
}
|
||||
}
|
||||
|
@ -18,7 +18,7 @@
|
||||
/**
|
||||
* Implements abstract class for request processing
|
||||
*
|
||||
* Register events (see RequestProcessor::registerEvents() for details):
|
||||
* Following events can be triggered by the class:
|
||||
* - <eventPrefix>RequestReceived
|
||||
* - <eventPrefix>RequestError
|
||||
* - <eventPrefix>ResponseReceived
|
||||
@ -27,8 +27,62 @@
|
||||
*
|
||||
* <eventPrefix> variable specifies in RequestProcessor::__construct()
|
||||
*
|
||||
*
|
||||
* Full description of triggered events:
|
||||
*
|
||||
* 1. "<eventPrefix>RequestReceived" - triggers when request decoded and
|
||||
* validate successfully, before execution functions from request.
|
||||
*
|
||||
* An associative array passed to event handler have following keys:
|
||||
* - 'package' : decoded and validated package array. See Mibew API for details
|
||||
* of the package structure
|
||||
*
|
||||
*
|
||||
* 2. "<eventPrefix>RequestError" - triggers when error occurs during received
|
||||
* request processing.
|
||||
*
|
||||
* An associative array passed to event handler have following keys:
|
||||
* - 'exception' : an object of Exception (or inherited) class related to
|
||||
* occurred error.
|
||||
*
|
||||
*
|
||||
* 3. "<eventPrefix>ResponseReceived" - triggers when request sent successfully,
|
||||
* and response received.
|
||||
*
|
||||
* An associative array passed to event handler have following keys:
|
||||
* - 'package' : decoded and validated response package array. See Mibew API
|
||||
* for details of the package structure.
|
||||
*
|
||||
*
|
||||
* 4. "<eventPrefix>CallError" - triggers when error occurs in
|
||||
* call() method.
|
||||
*
|
||||
* An associative array passed to event handler have following keys:
|
||||
* - 'exception' : an object of Exception (or inherited) class related to
|
||||
* occurred error.
|
||||
*
|
||||
*
|
||||
* 5. "<eventPrefix>FunctionCall" - triggers when function from request calls.
|
||||
*
|
||||
* An associative array passed to event handler is 'function' array. See Mibew
|
||||
* API for detail of the 'function' array structure.
|
||||
*
|
||||
* If function wants to return some results, it should add results to the
|
||||
* 'results' element of the function array.
|
||||
*
|
||||
* Example of the event handler:
|
||||
* <code>
|
||||
* public function callHandler(&$function) {
|
||||
* if ($function['function'] == 'microtime') {
|
||||
* $as_float = empty($function['arguments']['as_float'])
|
||||
* ? false
|
||||
* : $function['arguments']['as_float'];
|
||||
* $function['results']['time'] = microtime($as_float);
|
||||
* }
|
||||
* }
|
||||
* </code>
|
||||
*
|
||||
* @see RequestProcessor::__construct()
|
||||
* @see RequestProcessor::registerEvents()
|
||||
*/
|
||||
abstract class RequestProcessor {
|
||||
|
||||
@ -39,7 +93,7 @@ abstract class RequestProcessor {
|
||||
protected $mibewAPI = null;
|
||||
|
||||
/**
|
||||
* Prefix that uses for all registered by the class events.
|
||||
* Prefix that uses for all events triggered by the class.
|
||||
* @var string
|
||||
*/
|
||||
protected $eventPrefix = '';
|
||||
@ -65,8 +119,9 @@ abstract class RequestProcessor {
|
||||
* - 'trusted_signatures': array of trusted signatures. Uses for identify another
|
||||
* side of interaction.
|
||||
* And may contains following (if not default values will be used)
|
||||
* - 'event_prefix': prefix that uses for all registered by the class events. The default value is the class
|
||||
* name with first character in lower case
|
||||
* - 'event_prefix': prefix that uses for all events triggered by the
|
||||
* class. The default value is the class name with first character in
|
||||
* lower case
|
||||
*/
|
||||
public function __construct($config) {
|
||||
// Check signature
|
||||
@ -90,9 +145,6 @@ abstract class RequestProcessor {
|
||||
|
||||
// Store config
|
||||
$this->config = $config;
|
||||
|
||||
// Register Events
|
||||
$this->registerEvents();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -260,70 +312,6 @@ abstract class RequestProcessor {
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register events
|
||||
*
|
||||
* Registered Events:
|
||||
*
|
||||
* 1. "<eventPrefix>RequestReceived" - triggers when request decoded and validate
|
||||
* successfully, before execution functions from request.
|
||||
*
|
||||
* An associative array passed to event handler have following keys:
|
||||
* - 'package' : decoded and validated package array. See Mibew API for details of the
|
||||
* package structure
|
||||
*
|
||||
*
|
||||
* 2. "<eventPrefix>RequestError" - triggers when error occurs during received
|
||||
* request processing.
|
||||
*
|
||||
* An associative array passed to event handler have following keys:
|
||||
* - 'exception' : an object of Exception (or inherited) class related to occurred error.
|
||||
*
|
||||
*
|
||||
* 3. "<eventPrefix>ResponseReceived" - triggers when request sent successfully, and
|
||||
* response received.
|
||||
*
|
||||
* An associative array passed to event handler have following keys:
|
||||
* - 'package' : decoded and validated response package array. See Mibew API for details of
|
||||
* the package structure.
|
||||
*
|
||||
*
|
||||
* 4. "<eventPrefix>CallError" - triggers when error occurs in
|
||||
* call() method.
|
||||
*
|
||||
* An associative array passed to event handler have following keys:
|
||||
* - 'exception' : an object of Exception (or inherited) class related to occurred error.
|
||||
*
|
||||
*
|
||||
* 5. "<eventPrefix>FunctionCall" - triggers when function from request calls.
|
||||
*
|
||||
* An associative array passed to event handler is 'function' array. See Mibew API for
|
||||
* detail of the 'function' array structure.
|
||||
*
|
||||
* If function wants to return some results, it should add results to the 'results' element
|
||||
* of the function array.
|
||||
*
|
||||
* Example of the event handler:
|
||||
* <code>
|
||||
* public function callHandler(&$function) {
|
||||
* if ($function['function'] == 'microtime') {
|
||||
* $as_float = empty($function['arguments']['as_float'])
|
||||
* ? false
|
||||
* : $function['arguments']['as_float'];
|
||||
* $function['results']['time'] = microtime($as_float);
|
||||
* }
|
||||
* }
|
||||
* </code>
|
||||
*/
|
||||
protected function registerEvents() {
|
||||
$dispatcher = EventDispatcher::getInstance();
|
||||
$dispatcher->registerEvent($this->eventPrefix . 'RequestReceived');
|
||||
$dispatcher->registerEvent($this->eventPrefix . 'RequestError');
|
||||
$dispatcher->registerEvent($this->eventPrefix . 'ResponseReceived');
|
||||
$dispatcher->registerEvent($this->eventPrefix . 'CallError');
|
||||
$dispatcher->registerEvent($this->eventPrefix . 'FunctionCall');
|
||||
}
|
||||
|
||||
/**
|
||||
* Process request
|
||||
*
|
||||
@ -460,7 +448,6 @@ abstract class RequestProcessor {
|
||||
* It calls before '<eventPrefix>FunctionCall' event triggers.
|
||||
*
|
||||
* @param array &$func Function array equals to array, passed to the '<eventPrefix>FunctionCall' event.
|
||||
* @see RequestProcessor::registerEvents()
|
||||
* @todo Create some unit tests
|
||||
*/
|
||||
protected function processorCall(&$func) {
|
||||
|
@ -18,14 +18,16 @@
|
||||
/**
|
||||
* Incapsulates thread api and thread processing functions.
|
||||
*
|
||||
* Register events (see RequestProcessor::registerEvents() for details):
|
||||
* Event triggered by the class (see description of the RequestProcessor class
|
||||
* for details):
|
||||
* - threadRequestReceived
|
||||
* - threadReceiveRequestError
|
||||
* - threadCallError
|
||||
* - threadFunctionCall
|
||||
*
|
||||
* WARNING:
|
||||
* threadResponseReceived registered but never called because of asynchronous nature of Core-to-Window interaction
|
||||
* threadResponseReceived registered but never called because of asynchronous
|
||||
* nature of Core-to-Window interaction
|
||||
*
|
||||
* Implements Singleton pattern
|
||||
*/
|
||||
|
@ -18,7 +18,8 @@
|
||||
/**
|
||||
* Incapsulates awaiting users list api related functions.
|
||||
*
|
||||
* Register events (see RequestProcessor::registerEvents() for details):
|
||||
* Events triggered by the class (see description of the RequestProcessor class
|
||||
* for details):
|
||||
* - usersRequestReceived
|
||||
* - usersReceiveRequestError
|
||||
* - usersCallError
|
||||
|
Loading…
Reference in New Issue
Block a user