Hooks.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /**
  3. * Handles adding and dispatching events
  4. *
  5. * @package Requests
  6. * @subpackage Utilities
  7. */
  8. /**
  9. * Handles adding and dispatching events
  10. *
  11. * @package Requests
  12. * @subpackage Utilities
  13. */
  14. class Requests_Hooks implements Requests_Hooker {
  15. /**
  16. * Registered callbacks for each hook
  17. *
  18. * @var array
  19. */
  20. protected $hooks = array();
  21. /**
  22. * Constructor
  23. */
  24. public function __construct() {
  25. // pass
  26. }
  27. /**
  28. * Register a callback for a hook
  29. *
  30. * @param string $hook Hook name
  31. * @param callback $callback Function/method to call on event
  32. * @param int $priority Priority number. <0 is executed earlier, >0 is executed later
  33. */
  34. public function register($hook, $callback, $priority = 0) {
  35. if (!isset($this->hooks[$hook])) {
  36. $this->hooks[$hook] = array();
  37. }
  38. if (!isset($this->hooks[$hook][$priority])) {
  39. $this->hooks[$hook][$priority] = array();
  40. }
  41. $this->hooks[$hook][$priority][] = $callback;
  42. }
  43. /**
  44. * Dispatch a message
  45. *
  46. * @param string $hook Hook name
  47. * @param array $parameters Parameters to pass to callbacks
  48. * @return boolean Successfulness
  49. */
  50. public function dispatch($hook, $parameters = array()) {
  51. if (empty($this->hooks[$hook])) {
  52. return false;
  53. }
  54. foreach ($this->hooks[$hook] as $priority => $hooked) {
  55. foreach ($hooked as $callback) {
  56. call_user_func_array($callback, $parameters);
  57. }
  58. }
  59. return true;
  60. }
  61. }