ru-se.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

Hooks.php (1400B)


      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 	 * Registered callbacks for each hook
     18 	 *
     19 	 * @var array
     20 	 */
     21 	protected $hooks = array();
     22 
     23 	/**
     24 	 * Constructor
     25 	 */
     26 	public function __construct() {
     27 		// pass
     28 	}
     29 
     30 	/**
     31 	 * Register a callback for a hook
     32 	 *
     33 	 * @param string $hook Hook name
     34 	 * @param callback $callback Function/method to call on event
     35 	 * @param int $priority Priority number. <0 is executed earlier, >0 is executed later
     36 	 */
     37 	public function register($hook, $callback, $priority = 0) {
     38 		if (!isset($this->hooks[$hook])) {
     39 			$this->hooks[$hook] = array();
     40 		}
     41 		if (!isset($this->hooks[$hook][$priority])) {
     42 			$this->hooks[$hook][$priority] = array();
     43 		}
     44 
     45 		$this->hooks[$hook][$priority][] = $callback;
     46 	}
     47 
     48 	/**
     49 	 * Dispatch a message
     50 	 *
     51 	 * @param string $hook Hook name
     52 	 * @param array $parameters Parameters to pass to callbacks
     53 	 * @return boolean Successfulness
     54 	 */
     55 	public function dispatch($hook, $parameters = array()) {
     56 		if (empty($this->hooks[$hook])) {
     57 			return false;
     58 		}
     59 
     60 		foreach ($this->hooks[$hook] as $priority => $hooked) {
     61 			foreach ($hooked as $callback) {
     62 				call_user_func_array($callback, $parameters);
     63 			}
     64 		}
     65 
     66 		return true;
     67 	}
     68 }