[geeklog-cvs] Geeklog-2/system ActionManager.php,NONE,1.1 EventManager.php,1.2,NONE

tony at iowaoutdoors.org tony at iowaoutdoors.org
Tue Dec 21 19:00:54 EST 2004


Update of /var/cvs/Geeklog-2/system
In directory www:/tmp/cvs-serv31725

Added Files:
	ActionManager.php 
Removed Files:
	EventManager.php 
Log Message:
Renamed EventManager to ActionManager


--- EventManager.php DELETED ---

--- NEW FILE: ActionManager.php ---
<?php

/* Reminder: always indent with 4 spaces (no tabs). */
/**
 * Geeklog 2
 *
 * License Details To Be Determined
 *
 */
 
/**
 * 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
 * @author Vincent Furia <vfuria at gmail.com>
 * @author Tony Bibbs <tony at geeklog.net>
 */
class Geeklog_ActionManager {
    /**
    * 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 action
    *
    * @author Vincent Furia <vfuria at gmail.com>
    * @author Tony Bibbs <tony at geeklog.net>
    * @access public
    * @param  mixed   $action  action(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 actions
    *
    * @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 $actionName  Name of action to get listeners of
    * @return array  array of listeners to action
    *
    */
	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