Skip to content

Commit

Permalink
Merge pull request #57 from jmashburn/ToroHookPriority
Browse files Browse the repository at this point in the history
Added priority flags to hooks
  • Loading branch information
martinbean committed Sep 25, 2013
2 parents b1e76b0 + 8a1aa80 commit dc3a846
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/Toro.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,24 @@ class ToroHook
private function __construct() {}
private function __clone() {}

public static function add($hook_name, $fn)
public static function add($hook_name, $fn, $priority = 1)
{
$instance = self::get_instance();
$instance->hooks[$hook_name][] = $fn;
$instance->hooks[$hook_name][] = array('data' => $fn, 'priority' => (int) $priority);
}

public static function fire($hook_name, $params = null)
{
$instance = self::get_instance();
if (isset($instance->hooks[$hook_name])) {
foreach ($instance->hooks[$hook_name] as $fn) {
call_user_func_array($fn, array(&$params));
uksort($instance->hooks[$hook_name], function ($a, $b) use($instance, $hook_name) {
if ($instance->hooks[$hook_name][$a]['priority'] == $instance->hooks[$hook_name][$b]['priority']) {
return ($a>$b)?1:-1;
}
return $instance->hooks[$hook_name][$a]['priority'] < $instance->hooks[$hook_name][$b]['priority']?1:-1;
});
foreach ($instance->hooks[$hook_name] as $hook) {
call_user_func_array($hook['data'], array(&$params));
}
}
}
Expand Down

0 comments on commit dc3a846

Please sign in to comment.