Auth
4 years ago
Cookie
2 years ago
Exception
4 years ago
Proxy
4 years ago
Response
4 years ago
Transport
2 years ago
Utility
2 years ago
Auth.php
4 years ago
Cookie.php
4 years ago
Exception.php
4 years ago
Hooker.php
4 years ago
Hooks.php
2 years ago
IDNAEncoder.php
4 years ago
IPv6.php
4 years ago
IRI.php
2 years ago
Proxy.php
4 years ago
Response.php
4 years ago
SSL.php
4 years ago
Session.php
2 years ago
Transport.php
4 years ago
Hooks.php
72 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Handles adding and dispatching events |
| 4 | * |
| 5 | * @package Requests |
| 6 | * @subpackage Utilities |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * Handles adding and dispatching events |
| 11 | * |
| 12 | * @package Requests |
| 13 | * @subpackage Utilities |
| 14 | */ |
| 15 | class Requests_Hooks implements Requests_Hooker { |
| 16 | |
| 17 | public function __wakeup(){throw new \LogicException( __CLASS__ . " should never be unserialized" );} |
| 18 | |
| 19 | /** |
| 20 | * Registered callbacks for each hook |
| 21 | * |
| 22 | * @var array |
| 23 | */ |
| 24 | protected $hooks = array(); |
| 25 | |
| 26 | /** |
| 27 | * Constructor |
| 28 | */ |
| 29 | public function __construct() { |
| 30 | // pass |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Register a callback for a hook |
| 35 | * |
| 36 | * @param string $hook Hook name |
| 37 | * @param callback $callback Function/method to call on event |
| 38 | * @param int $priority Priority number. <0 is executed earlier, >0 is executed later |
| 39 | */ |
| 40 | public function register($hook, $callback, $priority = 0) { |
| 41 | if (!isset($this->hooks[$hook])) { |
| 42 | $this->hooks[$hook] = array(); |
| 43 | } |
| 44 | if (!isset($this->hooks[$hook][$priority])) { |
| 45 | $this->hooks[$hook][$priority] = array(); |
| 46 | } |
| 47 | |
| 48 | $this->hooks[$hook][$priority][] = $callback; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Dispatch a message |
| 53 | * |
| 54 | * @param string $hook Hook name |
| 55 | * @param array $parameters Parameters to pass to callbacks |
| 56 | * @return boolean Successfulness |
| 57 | */ |
| 58 | public function dispatch($hook, $parameters = array()) { |
| 59 | if (empty($this->hooks[$hook])) { |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | foreach ($this->hooks[$hook] as $priority => $hooked) { |
| 64 | foreach ($hooked as $callback) { |
| 65 | call_user_func_array($callback, $parameters); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | return true; |
| 70 | } |
| 71 | } |
| 72 |