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