PageRenderTime 39ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/event/dispatcher.php

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