From 219aa4c9b19e0d069a92fa227b5bc8cc61d2f375 Mon Sep 17 00:00:00 2001 From: Dmitriy Simushev Date: Mon, 4 Feb 2013 12:32:58 +0000 Subject: [PATCH] Update EventDispatcher class There is no need to register events any more. --- .../libs/classes/EventDispatcherTest.php | 54 +------ ...phpunit_autotest_plugin_manager_plugin.php | 14 +- .../request_processor_test_plugin.php | 9 +- .../webim/libs/classes/event_dispatcher.php | 33 +---- src/messenger/webim/libs/classes/plugin.php | 5 - .../webim/libs/classes/plugin_manager.php | 1 - .../webim/libs/classes/request_processor.php | 133 ++++++++---------- .../webim/libs/classes/thread_processor.php | 6 +- .../webim/libs/classes/users_processor.php | 3 +- 9 files changed, 78 insertions(+), 180 deletions(-) diff --git a/src/messenger/tests/server_side/webim/libs/classes/EventDispatcherTest.php b/src/messenger/tests/server_side/webim/libs/classes/EventDispatcherTest.php index 3a0b4ef7..2bd72ff8 100644 --- a/src/messenger/tests/server_side/webim/libs/classes/EventDispatcherTest.php +++ b/src/messenger/tests/server_side/webim/libs/classes/EventDispatcherTest.php @@ -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); diff --git a/src/messenger/tests/server_side/webim/plugins/phpunit_autotest_plugin_manager/phpunit_autotest_plugin_manager_plugin.php b/src/messenger/tests/server_side/webim/plugins/phpunit_autotest_plugin_manager/phpunit_autotest_plugin_manager_plugin.php index 28be6d32..363e499b 100644 --- a/src/messenger/tests/server_side/webim/plugins/phpunit_autotest_plugin_manager/phpunit_autotest_plugin_manager_plugin.php +++ b/src/messenger/tests/server_side/webim/plugins/phpunit_autotest_plugin_manager/phpunit_autotest_plugin_manager_plugin.php @@ -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) { diff --git a/src/messenger/tests/server_side/webim/plugins/request_processor_test/request_processor_test_plugin.php b/src/messenger/tests/server_side/webim/plugins/request_processor_test/request_processor_test_plugin.php index f64aa098..42a1aaeb 100644 --- a/src/messenger/tests/server_side/webim/plugins/request_processor_test/request_processor_test_plugin.php +++ b/src/messenger/tests/server_side/webim/plugins/request_processor_test/request_processor_test_plugin.php @@ -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); } } diff --git a/src/messenger/webim/libs/classes/event_dispatcher.php b/src/messenger/webim/libs/classes/event_dispatcher.php index 913a3a25..3fdfb14e 100644 --- a/src/messenger/webim/libs/classes/event_dispatcher.php +++ b/src/messenger/webim/libs/classes/event_dispatcher.php @@ -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; } } diff --git a/src/messenger/webim/libs/classes/plugin.php b/src/messenger/webim/libs/classes/plugin.php index a9252a00..0016264b 100644 --- a/src/messenger/webim/libs/classes/plugin.php +++ b/src/messenger/webim/libs/classes/plugin.php @@ -41,11 +41,6 @@ abstract Class Plugin { */ abstract public function getWeight(); - /** - * Register events - */ - abstract public function registerEvents(); - /** * Register listeners * diff --git a/src/messenger/webim/libs/classes/plugin_manager.php b/src/messenger/webim/libs/classes/plugin_manager.php index cbdefaf6..a47dac64 100644 --- a/src/messenger/webim/libs/classes/plugin_manager.php +++ b/src/messenger/webim/libs/classes/plugin_manager.php @@ -146,7 +146,6 @@ Class PluginManager { uksort($loading_queue, 'strnatcmp'); // Add events and listeners foreach ($loading_queue as $plugin) { - $plugin->registerEvents(); $plugin->registerListeners(); } } diff --git a/src/messenger/webim/libs/classes/request_processor.php b/src/messenger/webim/libs/classes/request_processor.php index 20c924e6..f3aebb0d 100644 --- a/src/messenger/webim/libs/classes/request_processor.php +++ b/src/messenger/webim/libs/classes/request_processor.php @@ -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: * - RequestReceived * - RequestError * - ResponseReceived @@ -27,8 +27,62 @@ * * variable specifies in RequestProcessor::__construct() * + * + * Full description of triggered events: + * + * 1. "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. "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. "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. "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. "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: + * + * 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); + * } + * } + * + * * @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. "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. "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. "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. "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. "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: - * - * 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); - * } - * } - * - */ - 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 'FunctionCall' event triggers. * * @param array &$func Function array equals to array, passed to the 'FunctionCall' event. - * @see RequestProcessor::registerEvents() * @todo Create some unit tests */ protected function processorCall(&$func) { diff --git a/src/messenger/webim/libs/classes/thread_processor.php b/src/messenger/webim/libs/classes/thread_processor.php index 3cff696b..66d9c00b 100644 --- a/src/messenger/webim/libs/classes/thread_processor.php +++ b/src/messenger/webim/libs/classes/thread_processor.php @@ -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 */ diff --git a/src/messenger/webim/libs/classes/users_processor.php b/src/messenger/webim/libs/classes/users_processor.php index 19f38f86..1892ed0e 100644 --- a/src/messenger/webim/libs/classes/users_processor.php +++ b/src/messenger/webim/libs/classes/users_processor.php @@ -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