Hooks.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * Handles adding and dispatching events
  4. *
  5. * @package Requests\EventDispatcher
  6. */
  7. namespace WpOrg\Requests;
  8. use WpOrg\Requests\Exception\InvalidArgument;
  9. use WpOrg\Requests\HookManager;
  10. use WpOrg\Requests\Utility\InputValidator;
  11. /**
  12. * Handles adding and dispatching events
  13. *
  14. * @package Requests\EventDispatcher
  15. */
  16. class Hooks implements HookManager {
  17. /**
  18. * Registered callbacks for each hook
  19. *
  20. * @var array
  21. */
  22. protected $hooks = [];
  23. /**
  24. * Register a callback for a hook
  25. *
  26. * @param string $hook Hook name
  27. * @param callable $callback Function/method to call on event
  28. * @param int $priority Priority number. <0 is executed earlier, >0 is executed later
  29. * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $hook argument is not a string.
  30. * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $callback argument is not callable.
  31. * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $priority argument is not an integer.
  32. */
  33. public function register($hook, $callback, $priority = 0) {
  34. if (is_string($hook) === false) {
  35. throw InvalidArgument::create(1, '$hook', 'string', gettype($hook));
  36. }
  37. if (is_callable($callback) === false) {
  38. throw InvalidArgument::create(2, '$callback', 'callable', gettype($callback));
  39. }
  40. if (InputValidator::is_numeric_array_key($priority) === false) {
  41. throw InvalidArgument::create(3, '$priority', 'integer', gettype($priority));
  42. }
  43. if (!isset($this->hooks[$hook])) {
  44. $this->hooks[$hook] = [
  45. $priority => [],
  46. ];
  47. } elseif (!isset($this->hooks[$hook][$priority])) {
  48. $this->hooks[$hook][$priority] = [];
  49. }
  50. $this->hooks[$hook][$priority][] = $callback;
  51. }
  52. /**
  53. * Dispatch a message
  54. *
  55. * @param string $hook Hook name
  56. * @param array $parameters Parameters to pass to callbacks
  57. * @return boolean Successfulness
  58. * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $hook argument is not a string.
  59. * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $parameters argument is not an array.
  60. */
  61. public function dispatch($hook, $parameters = []) {
  62. if (is_string($hook) === false) {
  63. throw InvalidArgument::create(1, '$hook', 'string', gettype($hook));
  64. }
  65. // Check strictly against array, as Array* objects don't work in combination with `call_user_func_array()`.
  66. if (is_array($parameters) === false) {
  67. throw InvalidArgument::create(2, '$parameters', 'array', gettype($parameters));
  68. }
  69. if (empty($this->hooks[$hook])) {
  70. return false;
  71. }
  72. if (!empty($parameters)) {
  73. // Strip potential keys from the array to prevent them being interpreted as parameter names in PHP 8.0.
  74. $parameters = array_values($parameters);
  75. }
  76. ksort($this->hooks[$hook]);
  77. foreach ($this->hooks[$hook] as $priority => $hooked) {
  78. foreach ($hooked as $callback) {
  79. $callback(...$parameters);
  80. }
  81. }
  82. return true;
  83. }
  84. }