[geeklog-cvs] Geeklog-2/system EventManager.php,1.1.1.1,1.2
tony at iowaoutdoors.org
tony at iowaoutdoors.org
Tue Dec 21 18:55:21 EST 2004
Update of /var/cvs/Geeklog-2/system
In directory www:/tmp/cvs-serv31267
Modified Files:
EventManager.php
Log Message:
Initial implementation, all coded but not tested so even compile errors are possible (should have no syntax
errors).
Index: EventManager.php
===================================================================
RCS file: /var/cvs/Geeklog-2/system/EventManager.php,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -C2 -d -r1.1.1.1 -r1.2
*** EventManager.php 17 Dec 2004 23:35:47 -0000 1.1.1.1
--- EventManager.php 21 Dec 2004 23:55:18 -0000 1.2
***************
*** 10,16 ****
/**
! * This is the official Geeklog 2 Event Manger. All plugins who want to listen to events fired off
* by the GL2 kernel or by other plugins must understand how this class works. The same goes for
! * any plugins that wish to expose events of their own.
*
* @copyright 2004 Tony Bibbs, Vincent Furia
--- 10,16 ----
/**
! * This is the official Geeklog 2 Action Manger. All plugins who want to listen to actions fired off
* by the GL2 kernel or by other plugins must understand how this class works. The same goes for
! * any plugins that wish to expose actions of their own.
*
* @copyright 2004 Tony Bibbs, Vincent Furia
***************
*** 20,74 ****
class Geeklog_EventManager {
/**
! * Array containing event keys and listener (arrays as) values
! * This takes the form of $listeners[<eventName>] =
* array(<pluginName1>, <pluginName2>, etc);
*/
! private $listeners = array();
!
! /**
! * PluginEventHandler Constructor
! *
! * @author Vincent Furia <vfuria at gmail.com>
! * @access public
! *
! */
! public function __construct() {
! // fetch registered listeners from database, populate $listeners
! }
! /**
* Register a plugin to listen for an event
*
* @author Vincent Furia <vfuria at gmail.com>
* @access public
* @param mixed $event event(s) to listen for (string or array)
* @param string $plugin plugin to be registered as listener
* @return boolean true on success
*
*/
! public function registerListener($events, $plugin) {
! // add the listener to the $listeners variable and the database
}
/**
! * Unregister a plugin from listening for an event
*
* @author Vincent Furia <vfuria at gmail.com>
* @access public
! * @param mixed $event event(s) to unregister (string or array)
* @param string $plugin plugin to be unregistered as listener
* @return boolean true on success
*
*/
! public function unregisterListener($plugin, $events = '') {
! // remove the listener for the specified events from $listeners
! // and the database. If events is empty then unregister the plugin
! // for all events
}
/**
! * Get all the listeners for a specific event
*
* @author Vincent Furia <vfuria at gmail.com>
* @access public
* @param string $event event to get listeners of
--- 20,121 ----
class Geeklog_EventManager {
/**
! * Array containing action keys and listener (arrays as) values
! * This takes the form of $listeners[<actionName>] =
* array(<pluginName1>, <pluginName2>, etc);
*/
! private static $listeners = array();
! /**
* Register a plugin to listen for an event
*
* @author Vincent Furia <vfuria at gmail.com>
+ * @author Tony Bibbs <tony at geeklog.net>
* @access public
* @param mixed $event event(s) to listen for (string or array)
* @param string $plugin plugin to be registered as listener
* @return boolean true on success
+ * @todo Should we consider some mechanism for caching?
*
*/
! static public function registerListener($pluginName, $actions)
! {
! require_once 'Geeklog-2/Gl2Plugin.php';
! require_once 'DataAccess/DAO.php';
!
! // Get handle to plugin domain object
! $dao = &Geeklog_DAO::singleton();
! $retval = $dao->find('getPluginByName', array($pluginName));
! $plugin = $retval[0];
!
! if (!is_array($actions)) {
! $actionArray = $dao->find('getActionsByName', array($actions));
! } else {
! $actionArray = $dao->find('getActionsByName', $actions);
! }
!
! // Bring in ActionListener and instantiate
! require_once 'Geeklog-2/Gl2ActionListener.php';
! $curListener = new Gl2ActionListener();
!
! // Save action listeners
! foreach ($actionArray as $curAction) {
! // Save to database
! $curListener->setNew(true);
! $curListener->setActionId($curAction->getActionId());
! $curListener->setPluginId($plugin->getPluginId());
! $dao->save($curListener);
!
! // Now save to memory
! $pluginArray = self::$listeners[$curAction->getActionName()];
! if (!in_array($pluginName, $actionArray)) {
! self::$listeners[$curAction->getActionName()][] = $pluginName;
! }
! }
}
/**
! * Unregister a plugin from listening for an action
! *
! * remove the listener for the specified action from $listeners and the database. If action is
! * empty then unregister the plugin for all events
*
* @author Vincent Furia <vfuria at gmail.com>
+ * @author Tony Bibbs <tony at geeklog.net>
* @access public
! * @param mixed $action action(s) to unregister (string or array)
* @param string $plugin plugin to be unregistered as listener
* @return boolean true on success
*
*/
! static public function unregisterListener($pluginName, $actions = '')
! {
! // Get handle to plugin domain object
! $dao = &Geeklog_DAO::singleton();
! $retval = $dao->find('getPluginByName', array($pluginName));
! $plugin = $retval[0];
!
! // Physically remove them from the database.
! $actionListeners = $plugin->getGl2ActionListeners();
! $dao->beginTransaction();
! foreach ($actionListeners as $curActionListener) {
! $dao->delete($curActionListener);
! }
! $dao->commit();
!
! // Now remove them from the listener array in memory
! foreach (self::$listeners as $curAction=>$pluginArray) {
! if (in_array($pluginName, $pluginArray)) {
! $keyToDelete = array_search($pluginName, $pluginArray);
! self::$listeners[$curAction] = array_splice($curAction, $keyToDelete, 1);
! }
! }
!
}
/**
! * Get all the listeners for a specific action
*
* @author Vincent Furia <vfuria at gmail.com>
+ * @author Tony Bibbs <tony at geeklog.net>
* @access public
* @param string $event event to get listeners of
***************
*** 76,113 ****
*
*/
! public function getListeners($event) {
! // remove the listener for the specified events from $listeners
! // and the database.
}
/**
! * Notify listener(s) that an event has occurred
*
! * @author Vincent Furia <vfuria at gmail.com>
! * @access public
! * @param mixed $event event requiring notification
! * @param mixed $vars event specific variables
! * @param mixed $plugin NOTIFY_ALL, specific plugin, or array of plugins
! * @return mixed event specific return values (or array of)
*
! */
! public function notify($event, $vars, $plugin = NOTIFY_ALL) {
! // call the handle event function for each plugin listening to the
! // relevant event (or, if $plugin specified, only that plugin)
! }
!
!
! /**
! * Creates a plugin and returns it
! *
! * @author Tony Bibbs <tony at geeklog.net>
* @access public
! * @param string $pluginName Name of the plugin to create
! * @return object An instance of the given plugin name
! *
*/
! private function &getPlugin($pluginName) {
! // creates a instance of a plugin so the plugin can be referenced
! // by other functions in this Class (i.e. notify).
}
}
--- 123,171 ----
*
*/
! static public function getListeners($actionName) {
! if (!isset(self::$listeners[$actionName])) {
! // Action isn't in listener array so load it
! $dao = &Geeklog_DAO::singleton();
! $retval = $dao->find('getActionListenersByActionName', array($actionName));
! foreach ($retval as $curActionListener) {
! $plugin = $curActionListener->getGl2Plugin();
! self::$listeners[$actionName][] = $plugin->getPluginName();
! }
! }
! return self::$listeners[$actionName];
}
/**
! * Notify listener(s) that an action has occurred
*
! * Call the handle action function for each plugin listening to the relevant action (or, if
! * $plugin specified, only that plugin)
*
! * @author Vincent Furia <vfuria at gmail.com>
* @access public
! * @param mixed $actionName action requiring notification
! * @param array $vars Action specific parameters
! * @param mixed $pluginName Array of plugin names to be notify
! * @return mixed Action specific return values (or array of)
! * @todo How will we handle return values?
*/
! static public function notify($actionName, $vars, $htmlPath, $pluginName = '')
! {
! if (empty($pluginName)) {
! // Load listeners, if needed.
! if (!isset(self::$listeners[$actionName])) {
! self::getListeners($actionName);
! }
! $pluginName = self::$listeners[$actionName];
! }
! if (!is_array($pluginName)) {
! $pluginName = array();
! }
!
! foreach($pluginName as $curPluginName) {
! require_once $htmlPath . "$pluginName.php";
! call_user_func_array(array($curPluginName, 'handleAction'), $vars);
! }
!
}
}
More information about the geeklog-cvs
mailing list