Files
@ f43d1a4680a9
Branch filter:
Location: hot67beta/libraries/joomla/event/dispatcher.php
f43d1a4680a9
5.1 KiB
text/x-php
menubar 0 to 10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | <?php
/**
* @version $Id: dispatcher.php 11387 2009-01-04 02:47:53Z ian $
* @package Joomla.Framework
* @subpackage Event
* @copyright Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
* @license GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/
// Check to ensure this file is within the rest of the framework
defined('JPATH_BASE') or die();
jimport('joomla.base.observable');
/**
* Class to handle dispatching of events.
*
* This is the Observable part of the Observer design pattern
* for the event architecture.
*
* @package Joomla.Framework
* @subpackage Event
* @since 1.5
* @see JPlugin
* @link http://docs.joomla.org/Tutorial:Plugins Plugin tutorials
*/
class JDispatcher extends JObservable
{
/**
* Constructor
*
* @access protected
*/
function __construct()
{
parent::__construct();
}
/**
* Returns a reference to the global Event Dispatcher object, only creating it
* if it doesn't already exist.
*
* This method must be invoked as:
* <pre> $dispatcher = &JDispatcher::getInstance();</pre>
*
* @access public
* @return JDispatcher The EventDispatcher object.
* @since 1.5
*/
function & getInstance()
{
static $instance;
if (!is_object($instance)) {
$instance = new JDispatcher();
}
return $instance;
}
/**
* Registers an event handler to the event dispatcher
*
* @access public
* @param string $event Name of the event to register handler for
* @param string $handler Name of the event handler
* @return void
* @since 1.5
*/
function register($event, $handler)
{
// Are we dealing with a class or function type handler?
if (function_exists($handler))
{
// Ok, function type event handler... lets attach it.
$method = array ('event' => $event, 'handler' => $handler);
$this->attach($method);
}
elseif (class_exists($handler))
{
//Ok, class type event handler... lets instantiate and attach it.
$this->attach(new $handler($this));
}
else
{
JError::raiseWarning('SOME_ERROR_CODE', 'JDispatcher::register: Event handler not recognized.', 'Handler: '.$handler );
}
}
/**
* Triggers an event by dispatching arguments to all observers that handle
* the event and returning their return values.
*
* @access public
* @param string $event The event name
* @param array $args An array of arguments
* @param boolean $doUnpublished [DEPRECATED]
* @return array An array of results from each function call
* @since 1.5
*/
function trigger($event, $args = null, $doUnpublished = false)
{
// Initialize variables
$result = array ();
/*
* If no arguments were passed, we still need to pass an empty array to
* the call_user_func_array function.
*/
if ($args === null) {
$args = array ();
}
/*
* We need to iterate through all of the registered observers and
* trigger the event for each observer that handles the event.
*/
foreach ($this->_observers as $observer)
{
if (is_array($observer))
{
/*
* Since we have gotten here, we know a little something about
* the observer. It is a function type observer... lets see if
* it handles our event.
*/
if ($observer['event'] == $event)
{
if (function_exists($observer['handler']))
{
$result[] = call_user_func_array($observer['handler'], $args);
}
else
{
/*
* Couldn't find the function that the observer specified..
* wierd, lets throw an error.
*/
JError::raiseWarning('SOME_ERROR_CODE', 'JDispatcher::trigger: Event Handler Method does not exist.', 'Method called: '.$observer['handler']);
}
}
else
{
// Handler doesn't handle this event, move on to next observer.
continue;
}
}
elseif (is_object($observer))
{
/*
* Since we have gotten here, we know a little something about
* the observer. It is a class type observer... lets see if it
* is an object which has an update method.
*/
if (method_exists($observer, 'update'))
{
/*
* Ok, now we know that the observer is both not an array
* and IS an object. Lets trigger its update method if it
* handles the event and return any results.
*/
if (method_exists($observer, $event))
{
$args['event'] = $event;
$result[] = $observer->update($args);
}
else
{
/*
* Handler doesn't handle this event, move on to next
* observer.
*/
continue;
}
}
else
{
/*
* At this point, we know that the registered observer is
* neither a function type observer nor an object type
* observer. PROBLEM, lets throw an error.
*/
JError::raiseWarning('SOME_ERROR_CODE', 'JDispatcher::trigger: Unknown Event Handler.', $observer );
}
}
}
return $result;
}
}
|