Show
Ignore:
Timestamp:
11/11/2007 12:59:10 AM (13 months ago)
Author:
Shadowhand
Message:

Small changes:

  • Hooks are now loaded *after* system events have been added
  • Hook loading changed to use Kohana::list_files(), rather than manually finding them (DRY!)
  • Kohana::auto_load() now uses require_once() instead of require(), to prevent duplicate file loads that will cause E_FATAL errors
  • Kohana::auto_load() will no longer try to auto-extend classes that end with _Core (eg: "class Special_Router extends Router_Core {}" will no longer try to auto-load "Router_Core_Core")
  • Kohana::key_string() will handle numerically indexed key strings now (thanks Armen!)
  • Added Event::add_before() and Event::add_after(), to add events before and after a given event, and supporting private function Event::insert_event()
  • Cleaned up Event::add() to use empty() instead of == FALSE
  • View::construct() uses array_merge($this->data, $data) instead of doing a foreach($data) (Duh!)
  • View::set() will now handle an associative array (Duh!)
  • Router::setup() split into Router::find_uri() and Router::setup(), for better Event extensibility
  • Cleaned up Router::setup() to use self::$current_uri instead of self::$segments as the default URI location

Phew!

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/system/core/Event.php

    r933 r1084  
    7070        public static function add($name, $callback) 
    7171        { 
    72                 if ($name == FALSE OR $callback == FALSE) 
    73                         return FALSE; 
    74  
    75                 // Make sure that the event name is defined 
     72                if (empty($name) OR empty($callback)) 
     73                        return FALSE; 
     74 
    7675                if ( ! isset(self::$events[$name])) 
    7776                { 
     77                        // Create an empty event if it is not yet defined 
    7878                        self::$events[$name] = array(); 
    7979                } 
    8080 
    81                 // Make sure the event is not already in the queue 
    8281                if ( ! in_array($callback, self::$events[$name])) 
    8382                { 
     83                        // Add the event if it does not already exist in the queue 
    8484                        self::$events[$name][] = $callback; 
    8585                } 
     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                ); 
    86165        } 
    87166