Auth
5 months ago
Cookie
5 months ago
Exception
5 months ago
Proxy
5 months ago
Response
5 months ago
Transport
5 months ago
Utility
5 months ago
Auth.php
5 months ago
Autoload.php
5 months ago
Capability.php
5 months ago
Cookie.php
5 months ago
Exception.php
5 months ago
HookManager.php
5 months ago
Hooks.php
5 months ago
IdnaEncoder.php
5 months ago
Ipv6.php
5 months ago
Iri.php
5 months ago
Port.php
5 months ago
Proxy.php
5 months ago
Requests.php
5 months ago
Response.php
5 months ago
Session.php
5 months ago
Ssl.php
5 months ago
Transport.php
5 months ago
Hooks.php
100 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Handles adding and dispatching events |
| 4 | * |
| 5 | * @package Requests\EventDispatcher |
| 6 | */ |
| 7 | |
| 8 | namespace AmeliaVendor\WpOrg\Requests; |
| 9 | |
| 10 | use AmeliaVendor\WpOrg\Requests\Exception\InvalidArgument; |
| 11 | use AmeliaVendor\WpOrg\Requests\HookManager; |
| 12 | use AmeliaVendor\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 \AmeliaVendor\WpOrg\Requests\Exception\InvalidArgument When the passed $hook argument is not a string. |
| 34 | * @throws \AmeliaVendor\WpOrg\Requests\Exception\InvalidArgument When the passed $callback argument is not callable. |
| 35 | * @throws \AmeliaVendor\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 \AmeliaVendor\WpOrg\Requests\Exception\InvalidArgument When the passed $hook argument is not a string. |
| 68 | * @throws \AmeliaVendor\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 |