diff options
author | Tim Almdal <tnalmdal@shaw.ca> | 2009-12-26 11:24:50 -0800 |
---|---|---|
committer | Tim Almdal <tnalmdal@shaw.ca> | 2009-12-26 11:24:50 -0800 |
commit | 3060a6f662da66008d57a461bf1c9b5b4aa2b002 (patch) | |
tree | 442fd290505817efc0324f2af6e01805cb7396aa /system/core/Event.php | |
parent | 1cd6a615bb47a33794e4a4f690c87a348ab752d7 (diff) | |
parent | 32d25dafd5b033338b6a9bb8c7c53edab462543a (diff) |
Merge branch 'master' into talmdal_dev
Conflicts:
modules/gallery/controllers/albums.php
modules/gallery/controllers/movies.php
modules/gallery/controllers/photos.php
Diffstat (limited to 'system/core/Event.php')
-rw-r--r-- | system/core/Event.php | 81 |
1 files changed, 41 insertions, 40 deletions
diff --git a/system/core/Event.php b/system/core/Event.php index 06468a8d..a9b88034 100644 --- a/system/core/Event.php +++ b/system/core/Event.php @@ -4,21 +4,21 @@ * to be added to 'events'. Events can be run multiple times, and can also * process event-specific data. By default, Kohana has several system events. * - * $Id: Event.php 4390 2009-06-04 03:05:36Z zombor $ + * $Id: Event.php 4679 2009-11-10 01:45:52Z isaiah $ * * @package Core * @author Kohana Team - * @copyright (c) 2007 Kohana Team - * @license http://kohanaphp.com/license.html + * @copyright (c) 2007-2009 Kohana Team + * @license http://kohanaphp.com/license * @link http://docs.kohanaphp.com/general/events */ -final class Event { +abstract class Event_Core { // Event callbacks - private static $events = array(); + protected static $events = array(); // Cache of events that have been run - private static $has_run = array(); + protected static $has_run = array(); // Data that can be processed during events public static $data; @@ -26,25 +26,26 @@ final class Event { /** * Add a callback to an event queue. * - * @param string event name - * @param array http://php.net/callback + * @param string event name + * @param array http://php.net/callback + * @param boolean prevent duplicates * @return boolean */ - public static function add($name, $callback) + public static function add($name, $callback, $unique = FALSE) { - if ( ! isset(self::$events[$name])) + if ( ! isset(Event::$events[$name])) { // Create an empty event if it is not yet defined - self::$events[$name] = array(); + Event::$events[$name] = array(); } - elseif (in_array($callback, self::$events[$name], TRUE)) + elseif ($unique AND in_array($callback, Event::$events[$name], TRUE)) { // The event already exists return FALSE; } // Add the event - self::$events[$name][] = $callback; + Event::$events[$name][] = $callback; return TRUE; } @@ -59,15 +60,15 @@ final class Event { */ public static function add_before($name, $existing, $callback) { - if (empty(self::$events[$name]) OR ($key = array_search($existing, self::$events[$name])) === FALSE) + if (empty(Event::$events[$name]) OR ($key = array_search($existing, Event::$events[$name])) === FALSE) { // Just add the event if there are no events - return self::add($name, $callback); + return Event::add($name, $callback); } else { // Insert the event immediately before the existing event - return self::insert_event($name, $key, $callback); + return Event::insert_event($name, $key, $callback); } } @@ -81,15 +82,15 @@ final class Event { */ public static function add_after($name, $existing, $callback) { - if (empty(self::$events[$name]) OR ($key = array_search($existing, self::$events[$name])) === FALSE) + if (empty(Event::$events[$name]) OR ($key = array_search($existing, Event::$events[$name])) === FALSE) { // Just add the event if there are no events - return self::add($name, $callback); + return Event::add($name, $callback); } else { // Insert the event immediately after the existing event - return self::insert_event($name, $key + 1, $callback); + return Event::insert_event($name, $key + 1, $callback); } } @@ -103,18 +104,18 @@ final class Event { */ private static function insert_event($name, $key, $callback) { - if (in_array($callback, self::$events[$name], TRUE)) + if (in_array($callback, Event::$events[$name], TRUE)) return FALSE; // Add the new event at the given key location - self::$events[$name] = array_merge + Event::$events[$name] = array_merge ( // Events before the key - array_slice(self::$events[$name], 0, $key), + array_slice(Event::$events[$name], 0, $key), // New event callback array($callback), // Events after the key - array_slice(self::$events[$name], $key) + array_slice(Event::$events[$name], $key) ); return TRUE; @@ -130,21 +131,21 @@ final class Event { */ public static function replace($name, $existing, $callback) { - if (empty(self::$events[$name]) OR ($key = array_search($existing, self::$events[$name], TRUE)) === FALSE) + if (empty(Event::$events[$name]) OR ($key = array_search($existing, Event::$events[$name], TRUE)) === FALSE) return FALSE; - if ( ! in_array($callback, self::$events[$name], TRUE)) + if ( ! in_array($callback, Event::$events[$name], TRUE)) { // Replace the exisiting event with the new event - self::$events[$name][$key] = $callback; + Event::$events[$name][$key] = $callback; } else { // Remove the existing event from the queue - unset(self::$events[$name][$key]); + unset(Event::$events[$name][$key]); // Reset the array so the keys are ordered properly - self::$events[$name] = array_values(self::$events[$name]); + Event::$events[$name] = array_values(Event::$events[$name]); } return TRUE; @@ -158,7 +159,7 @@ final class Event { */ public static function get($name) { - return empty(self::$events[$name]) ? array() : self::$events[$name]; + return empty(Event::$events[$name]) ? array() : Event::$events[$name]; } /** @@ -172,18 +173,18 @@ final class Event { { if ($callback === FALSE) { - self::$events[$name] = array(); + Event::$events[$name] = array(); } - elseif (isset(self::$events[$name])) + elseif (isset(Event::$events[$name])) { // Loop through each of the event callbacks and compare it to the // callback requested for removal. The callback is removed if it // matches. - foreach (self::$events[$name] as $i => $event_callback) + foreach (Event::$events[$name] as $i => $event_callback) { if ($callback === $event_callback) { - unset(self::$events[$name][$i]); + unset(Event::$events[$name][$i]); } } } @@ -198,24 +199,24 @@ final class Event { */ public static function run($name, & $data = NULL) { - if ( ! empty(self::$events[$name])) + if ( ! empty(Event::$events[$name])) { // So callbacks can access Event::$data - self::$data =& $data; - $callbacks = self::get($name); + Event::$data =& $data; + $callbacks = Event::get($name); foreach ($callbacks as $callback) { - call_user_func($callback); + call_user_func_array($callback, array(&$data)); } // Do this to prevent data from getting 'stuck' $clear_data = ''; - self::$data =& $clear_data; + Event::$data =& $clear_data; } // The event has been run! - self::$has_run[$name] = $name; + Event::$has_run[$name] = $name; } /** @@ -226,7 +227,7 @@ final class Event { */ public static function has_run($name) { - return isset(self::$has_run[$name]); + return isset(Event::$has_run[$name]); } } // End Event
\ No newline at end of file |