| | 86 | } |
| | 87 | |
| | 88 | /* |
| | 89 | * Method: add_before |
| | 90 | * Add a callback to an event queue, before a given event. |
| | 91 | * |
| | 92 | * Parameters: |
| | 93 | * name - event name |
| | 94 | * existing - existing event callback |
| | 95 | * callback - event callback |
| | 96 | */ |
| | 97 | public static function add_before($name, $existing, $callback) |
| | 98 | { |
| | 99 | if (empty($name) OR empty($existing) OR empty($callback)) |
| | 100 | return FALSE; |
| | 101 | |
| | 102 | if ( ! isset(self::$events[$name]) OR ($key = array_search($existing, self::$events[$name])) === FALSE) |
| | 103 | { |
| | 104 | // Add the event if there are no events or if the exsisting event is not set |
| | 105 | self::add($name, $callback); |
| | 106 | } |
| | 107 | else |
| | 108 | { |
| | 109 | // Insert the event immediately before the existing event |
| | 110 | self::insert_event($name, $key, $callback); |
| | 111 | } |
| | 112 | |
| | 113 | return TRUE; |
| | 114 | } |
| | 115 | |
| | 116 | /* |
| | 117 | * Method: add_after |
| | 118 | * Add a callback to an event queue, after a given event. |
| | 119 | * |
| | 120 | * Parameters: |
| | 121 | * name - event name |
| | 122 | * existing - existing event callback |
| | 123 | * callback - event callback |
| | 124 | */ |
| | 125 | public static function add_after($name, $existing, $callback) |
| | 126 | { |
| | 127 | if (empty($name) OR empty($existing) OR empty($callback)) |
| | 128 | return FALSE; |
| | 129 | |
| | 130 | if ( ! isset(self::$events[$name]) OR ($key = array_search($existing, self::$events[$name])) === FALSE) |
| | 131 | { |
| | 132 | // Add the event if there are no events or if the exsisting event is not set |
| | 133 | self::add($name, $callback); |
| | 134 | } |
| | 135 | else |
| | 136 | { |
| | 137 | // Insert the event immediately after the existing event |
| | 138 | self::insert_event($name, $key + 1, $callback); |
| | 139 | } |
| | 140 | |
| | 141 | return TRUE; |
| | 142 | } |
| | 143 | |
| | 144 | /* |
| | 145 | * Method: insert_event |
| | 146 | * Inserts a new event at a specfic key location. |
| | 147 | * |
| | 148 | * Parameters: |
| | 149 | * name - event name |
| | 150 | * key - key to insert new event at |
| | 151 | * callback - event callback |
| | 152 | */ |
| | 153 | private static function insert_event($name, $key, $callback) |
| | 154 | { |
| | 155 | // Add the new event at the given key location |
| | 156 | self::$events[$name] = array_merge |
| | 157 | ( |
| | 158 | // Events before the key |
| | 159 | array_slice(self::$events[$name], 0, $key), |
| | 160 | // New event callback |
| | 161 | array($callback), |
| | 162 | // Events after the key |
| | 163 | array_slice(self::$events[$name], $key) |
| | 164 | ); |