event.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * utilities for defining and running event handlers
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Event
  23. * @package StatusNet
  24. * @author Evan Prodromou <evan@status.net>
  25. * @copyright 2008 StatusNet, Inc.
  26. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  27. * @link http://status.net/
  28. */
  29. if (!defined('STATUSNET') && !defined('LACONICA')) {
  30. exit(1);
  31. }
  32. /**
  33. * Class for events
  34. *
  35. * This "class" two static functions for managing events in the StatusNet code.
  36. *
  37. * @category Event
  38. * @package StatusNet
  39. * @author Evan Prodromou <evan@status.net>
  40. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  41. * @link http://status.net/
  42. *
  43. * @todo Define a system for using Event instances
  44. */
  45. class Event {
  46. /* Global array of hooks, mapping eventname => array of callables */
  47. protected static $_handlers = array();
  48. /**
  49. * Add an event handler
  50. *
  51. * To run some code at a particular point in StatusNet processing.
  52. * Named events include receiving an XMPP message, adding a new notice,
  53. * or showing part of an HTML page.
  54. *
  55. * The arguments to the handler vary by the event. Handlers can return
  56. * two possible values: false means that the event has been replaced by
  57. * the handler completely, and no default processing should be done.
  58. * Non-false means successful handling, and that the default processing
  59. * should succeed. (Note that this only makes sense for some events.)
  60. *
  61. * Handlers can also abort processing by throwing an exception; these will
  62. * be caught by the closest code and displayed as errors.
  63. *
  64. * @param string $name Name of the event
  65. * @param callable $handler Code to run
  66. *
  67. * @return void
  68. */
  69. public static function addHandler($name, $handler) {
  70. if (array_key_exists($name, Event::$_handlers)) {
  71. Event::$_handlers[$name][] = $handler;
  72. } else {
  73. Event::$_handlers[$name] = array($handler);
  74. }
  75. }
  76. /**
  77. * Handle an event
  78. *
  79. * Events are any point in the code that we want to expose for admins
  80. * or third-party developers to use.
  81. *
  82. * We pass in an array of arguments (including references, for stuff
  83. * that can be changed), and each assigned handler gets run with those
  84. * arguments. Exceptions can be thrown to indicate an error.
  85. *
  86. * @param string $name Name of the event that's happening
  87. * @param array $args Arguments for handlers
  88. *
  89. * @return boolean flag saying whether to continue processing, based
  90. * on results of handlers.
  91. */
  92. public static function handle($name, array $args=array()) {
  93. $result = null;
  94. if (array_key_exists($name, Event::$_handlers)) {
  95. foreach (Event::$_handlers[$name] as $handler) {
  96. $result = call_user_func_array($handler, $args);
  97. if ($result === false) {
  98. break;
  99. }
  100. }
  101. }
  102. return ($result !== false);
  103. }
  104. /**
  105. * Check to see if an event handler exists
  106. *
  107. * Look to see if there's any handler for a given event, or narrow
  108. * by providing the name of a specific plugin class.
  109. *
  110. * @param string $name Name of the event to look for
  111. * @param string $plugin Optional name of the plugin class to look for
  112. *
  113. * @return boolean flag saying whether such a handler exists
  114. *
  115. */
  116. public static function hasHandler($name, $plugin=null) {
  117. if (array_key_exists($name, Event::$_handlers)) {
  118. if (isset($plugin)) {
  119. foreach (Event::$_handlers[$name] as $handler) {
  120. if (get_class($handler[0]) == $plugin) {
  121. return true;
  122. }
  123. }
  124. } else {
  125. return true;
  126. }
  127. }
  128. return false;
  129. }
  130. public static function getHandlers($name)
  131. {
  132. return Event::$_handlers[$name];
  133. }
  134. /**
  135. * Disables any and all handlers that have been set up so far;
  136. * use only if you know it's safe to reinitialize all plugins.
  137. */
  138. public static function clearHandlers() {
  139. Event::$_handlers = array();
  140. }
  141. }