PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/event/dispatcher.php

https://github.com/joebushi/joomla
PHP | 128 lines | 55 code | 13 blank | 60 comment | 7 complexity | a5d30739ffbef69a6b0285e0269b31d5 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. <?php
  2. /**
  3. * @version $Id$
  4. * @package Joomla.Framework
  5. * @subpackage Event
  6. * @copyright Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. defined('JPATH_BASE') or die;
  10. jimport('joomla.base.observable');
  11. /**
  12. * Class to handle dispatching of events.
  13. *
  14. * This is the Observable part of the Observer design pattern
  15. * for the event architecture.
  16. *
  17. * @package Joomla.Framework
  18. * @subpackage Event
  19. * @since 1.5
  20. * @see JPlugin
  21. * @link http://docs.joomla.org/Tutorial:Plugins Plugin tutorials
  22. */
  23. class JDispatcher extends JObservable
  24. {
  25. /**
  26. * Returns the global Event Dispatcher object, only creating it
  27. * if it doesn't already exist.
  28. *
  29. * @access public
  30. * @return JDispatcher The EventDispatcher object.
  31. * @since 1.5
  32. */
  33. public static function getInstance()
  34. {
  35. static $instance;
  36. if (!is_object($instance)) {
  37. $instance = new JDispatcher();
  38. }
  39. return $instance;
  40. }
  41. /**
  42. * Registers an event handler to the event dispatcher
  43. *
  44. * @access public
  45. * @param string $event Name of the event to register handler for
  46. * @param string $handler Name of the event handler
  47. * @return void
  48. * @since 1.5
  49. */
  50. public function register($event, $handler)
  51. {
  52. // Are we dealing with a class or function type handler?
  53. if (function_exists($handler))
  54. {
  55. // Ok, function type event handler... lets attach it.
  56. $method = array('event' => $event, 'handler' => $handler);
  57. $this->attach($method);
  58. }
  59. elseif (class_exists($handler))
  60. {
  61. // Ok, class type event handler... lets instantiate and attach it.
  62. $this->attach(new $handler($this));
  63. }
  64. else
  65. {
  66. JError::raiseWarning('SOME_ERROR_CODE', 'JDispatcher::register: Event handler not recognized.', 'Handler: '.$handler);
  67. }
  68. }
  69. /**
  70. * Triggers an event by dispatching arguments to all observers that handle
  71. * the event and returning their return values.
  72. *
  73. * @access public
  74. * @param string $event The event to trigger.
  75. * @param array $args An array of arguments.
  76. * @return array An array of results from each function call.
  77. * @since 1.5
  78. */
  79. public function trigger($event, $args = array())
  80. {
  81. // Initialise variables.
  82. $result = array();
  83. /*
  84. * If no arguments were passed, we still need to pass an empty array to
  85. * the call_user_func_array function.
  86. */
  87. if (!is_array($args)) {
  88. $args = (array)$args;
  89. }
  90. $event = strtolower($event);
  91. // Check if any plugins are attached to the event.
  92. if (!isset($this->_methods[$event]) || empty($this->_methods[$event])) {
  93. // No Plugins Associated To Event!
  94. return $result;
  95. }
  96. // Loop through all plugins having a method matching our event
  97. foreach ($this->_methods[$event] AS $key)
  98. {
  99. // Check if the plugin is present.
  100. if (!isset($this->_observers[$key])) {
  101. continue;
  102. }
  103. // Fire the event for an object based observer.
  104. if (is_object($this->_observers[$key])) {
  105. $args['event'] = $event;
  106. $result[] = $this->_observers[$key]->update($args);
  107. }
  108. // Fire the event for a function based observer.
  109. elseif (is_array($this->_observers[$key])) {
  110. $result[] = call_user_func_array($this->_observers[$key]['handler'], $args);
  111. }
  112. }
  113. return $result;
  114. }
  115. }