From 8a1aa807ee7ad135713d60424616dd2b683a36df Mon Sep 17 00:00:00 2001 From: Jared Mashburn Date: Fri, 30 Aug 2013 14:56:05 -0400 Subject: [PATCH] Adding simple priority flag to ToroHook. Set the priority to dictate the order in which the Hooks fire. --- src/Toro.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Toro.php b/src/Toro.php index 2d50501..1775c93 100644 --- a/src/Toro.php +++ b/src/Toro.php @@ -88,18 +88,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)); } } }