Changeset 1084

Show
Ignore:
Timestamp:
11/11/2007 12:59:10 AM (11 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!

Location:
trunk/system
Files:
4 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 
  • trunk/system/core/Kohana.php

    r1047 r1084  
    118118                setlocale(LC_ALL, Config::item('locale.language').'.UTF-8'); 
    119119 
     120                // Enable Kohana routing 
     121                Event::add('system.routing', array('Router', 'find_uri')); 
     122                Event::add('system.routing', array('Router', 'setup')); 
     123 
     124                // Enable Kohana controller initialization 
     125                Event::add('system.execute', array('Kohana', 'instance')); 
     126 
     127                // Enable Kohana output handling 
     128                Event::add('system.shutdown', array('Kohana', 'display')); 
     129 
    120130                if ($hooks = Config::item('hooks.enable')) 
    121131                { 
    122                         // All hooks are enabled, we must build an array of filenames 
    123132                        if ( ! is_array($hooks)) 
    124133                        { 
    125                                 $hooks = array(); 
    126                                 foreach(Config::include_paths() as $path) 
    127                                 { 
    128                                         // Find all the hooks in each path 
    129                                         if ($files = glob($path.'hooks/*'.EXT)) 
    130                                         { 
    131                                                 $hooks = array_merge($hooks, $files); 
    132                                         } 
    133                                 } 
     134                                // All hooks are enabled, find them 
     135                                $hooks = Kohana::list_files('hooks'); 
    134136                        } 
    135137 
     
    143145                        } 
    144146                } 
    145  
    146                 // Enable Kohana routing 
    147                 Event::add('system.routing', array('Router', 'setup')); 
    148  
    149                 // Enable Kohana controller initialization 
    150                 Event::add('system.execute', array('Kohana', 'instance')); 
    151  
    152                 // Enable Kohana output handling 
    153                 Event::add('system.shutdown', array('Kohana', 'display')); 
    154147 
    155148                if (Config::item('log.threshold') > 0) 
     
    491484                } 
    492485 
    493                 require self::find_file($type, $file, TRUE); 
     486                require_once self::find_file($type, $file, TRUE); 
    494487 
    495488                if ($type == 'libraries') 
     
    499492                                require $extension; 
    500493                        } 
    501                         else 
     494                        elseif (substr($class, -5) !== '_Core') 
    502495                        { 
    503496                                eval('class '.$class.' extends '.$class.'_Core { }'); 
     
    686679        { 
    687680                // No array to search 
    688                 if (empty($keys) OR empty($array)) 
     681                if ((empty($keys) AND is_array($keys)) OR (empty($array) AND is_array($array))) 
    689682                        return; 
    690683 
  • trunk/system/libraries/Router.php

    r932 r1084  
    3535                        throw new Kohana_Exception('core.no_default_route'); 
    3636 
    37                 // The follow block of if/else attempts to retrieve the URI segments automagically 
    38                 // Supported methods: CLI, GET, PATH_INFO, ORIG_PATH_INFO, PHP_SELF 
    39                 if (PHP_SAPI === 'cli') 
    40                 { 
    41                         // Command line requires a bit of hacking 
    42                         if (isset($_SERVER['argv'][1])) 
    43                         { 
    44                                 self::$segments = $_SERVER['argv'][1]; 
    45  
    46                                 // Remove GET string from segments 
    47                                 if (($query = strrpos(self::$segments, '?')) !== FALSE) 
    48                                 { 
    49                                         list (self::$segments, $query) = explode('?', self::$segments); 
    50  
    51                                         // Insert query into GET array 
    52                                         foreach(explode('&', $query) as $pair) 
    53                                         { 
    54                                                 list ($key, $val) = array_pad(explode('=', $pair), 1, ''); 
    55  
    56                                                 $_GET[utf8::clean($key)] = utf8::clean($val); 
    57                                         } 
    58                                 } 
    59                         } 
    60                 } 
    61                 elseif (count($_GET) === 1 AND current($_GET) == '') 
    62                 { 
    63                         self::$segments = current(array_keys($_GET)); 
    64  
    65                         // Fixes really stange handling of a suffix in a GET string 
    66                         if ($suffix = Config::item('core.url_suffix') AND substr(self::$segments, -(strlen($suffix))) === '_'.substr($suffix, 1)) 
    67                         { 
    68                                 self::$segments = substr(self::$segments, 0, -(strlen($suffix))); 
    69                         } 
    70  
    71                         // Destroy GET 
    72                         $_GET = array(); 
    73                 } 
    74                 elseif (isset($_SERVER['PATH_INFO']) AND $_SERVER['PATH_INFO']) 
    75                 { 
    76                         self::$segments = $_SERVER['PATH_INFO']; 
    77                 } 
    78                 elseif (isset($_SERVER['ORIG_PATH_INFO']) AND $_SERVER['ORIG_PATH_INFO']) 
    79                 { 
    80                         self::$segments = $_SERVER['ORIG_PATH_INFO']; 
    81                 } 
    82                 elseif (isset($_SERVER['PHP_SELF']) AND $_SERVER['PHP_SELF']) 
    83                 { 
    84                         self::$segments = $_SERVER['PHP_SELF']; 
    85                 } 
    86  
    87                 // Find the URI string based on the location of the front controller 
    88                 if (($offset = strpos(self::$segments, KOHANA)) !== FALSE) 
    89                 { 
    90                         // Add the length of the index file to the offset 
    91                         $offset += strlen(KOHANA); 
    92  
    93                         // Get the segment part of the URL 
    94                         self::$segments = substr(self::$segments, $offset); 
    95                         self::$segments = trim(self::$segments, '/'); 
    96                 } 
    97  
    98  
    9937                // Use the default route when no segments exist 
    100                 if (self::$segments == '' OR self::$segments == '/') 
    101                 { 
    102                         self::$segments = self::$routes['_default']; 
     38                if (self::$current_uri == '' OR self::$current_uri == '/') 
     39                { 
     40                        self::$current_uri = self::$routes['_default']; 
    10341                        $default_route = TRUE; 
    10442                } 
     
    11149                if ($suffix = Config::item('core.url_suffix')) 
    11250                { 
    113                         self::$segments = preg_replace('!'.preg_quote($suffix).'$!u', '', self::$segments); 
     51                        self::$current_uri = preg_replace('!'.preg_quote($suffix).'$!u', '', self::$current_uri); 
    11452                } 
    11553 
    11654                // Remove extra slashes from the segments that could cause fucked up routing 
    117                 self::$segments = preg_replace('!//+!', '/', self::$segments); 
     55                self::$current_uri = preg_replace('!//+!', '/', self::$current_uri); 
    11856 
    11957                // At this point, set the segments, rsegments, and current URI 
    12058                // In many cases, all of these variables will match 
    121                 self::$segments = self::$rsegments = self::$current_uri = trim(self::$segments, '/'); 
     59                self::$segments = self::$rsegments = self::$current_uri = trim(self::$current_uri, '/'); 
    12260 
    12361                (self::$segments === 'L0LEAST3R') and include SYSPATH.'views/kohana_holiday.php'; 
     
    243181                { 
    244182                        Kohana::show_404(); 
     183                } 
     184        } 
     185 
     186        /* 
     187         * Method: find_uri 
     188         *  Attempts to determine the current URI using CLI, GET, PATH_INFO, ORIG_PATH_INFO, or PHP_SELF. 
     189         */ 
     190        public static function find_uri() 
     191        { 
     192                if (PHP_SAPI === 'cli') 
     193                { 
     194                        // Command line requires a bit of hacking 
     195                        if (isset($_SERVER['argv'][1])) 
     196                        { 
     197                                self::$current_uri = $_SERVER['argv'][1]; 
     198 
     199                                // Remove GET string from segments 
     200                                if (($query = strpos(self::$current_uri, '?')) !== FALSE) 
     201                                { 
     202                                        list (self::$current_uri, $query) = explode('?', self::$segments, 2); 
     203 
     204                                        // Insert query into GET array 
     205                                        foreach(explode('&', $query) as $pair) 
     206                                        { 
     207                                                list ($key, $val) = array_pad(explode('=', $pair), 1, ''); 
     208 
     209                                                $_GET[utf8::clean($key)] = utf8::clean($val); 
     210                                        } 
     211                                } 
     212                        } 
     213                } 
     214                elseif (count($_GET) === 1 AND current($_GET) == '') 
     215                { 
     216                        self::$current_uri = current(array_keys($_GET)); 
     217 
     218                        // Fixes really stange handling of a suffix in a GET string 
     219                        if ($suffix = Config::item('core.url_suffix') AND substr(self::$current_uri, -(strlen($suffix))) === '_'.substr($suffix, 1)) 
     220                        { 
     221                                self::$current_uri = substr(self::$current_uri, 0, -(strlen($suffix))); 
     222                        } 
     223 
     224                        // Destroy GET 
     225                        $_GET = array(); 
     226                } 
     227                elseif (isset($_SERVER['PATH_INFO']) AND $_SERVER['PATH_INFO']) 
     228                { 
     229                        self::$current_uri = $_SERVER['PATH_INFO']; 
     230                } 
     231                elseif (isset($_SERVER['ORIG_PATH_INFO']) AND $_SERVER['ORIG_PATH_INFO']) 
     232                { 
     233                        self::$current_uri = $_SERVER['ORIG_PATH_INFO']; 
     234                } 
     235                elseif (isset($_SERVER['PHP_SELF']) AND $_SERVER['PHP_SELF']) 
     236                { 
     237                        self::$current_uri = $_SERVER['PHP_SELF']; 
     238                } 
     239 
     240                // Find the URI string based on the location of the front controller 
     241                if (($offset = strpos(self::$current_uri, KOHANA)) !== FALSE) 
     242                { 
     243                        // Add the length of the index file to the offset 
     244                        $offset += strlen(KOHANA); 
     245 
     246                        // Get the segment part of the URL 
     247                        self::$current_uri = substr(self::$current_uri, $offset); 
     248                        self::$current_uri = trim(self::$current_uri, '/'); 
    245249                } 
    246250        } 
  • trunk/system/libraries/View.php

    r1016 r1084  
    4545                if (is_array($data) AND ! empty($data)) 
    4646                { 
    47                         foreach($data as $name => $value) 
    48                         { 
    49                                 $this->data[$name] = $value; 
    50                         } 
     47                        $this->data = $data; 
    5148                } 
    5249 
     
    6562         *  View object 
    6663         */ 
    67         public function set($name, $value) 
     64        public function set($name, $value = NULL) 
    6865        { 
    69                 $this->__set($name, $value); 
     66                if (func_num_args() === 1 AND is_array($name)) 
     67                { 
     68                        foreach($name as $key => $value) 
     69                        { 
     70                                $this->__set($key, $value); 
     71                        } 
     72                } 
     73                else 
     74                { 
     75                        $this->__set($name, $value); 
     76                } 
    7077                return $this; 
    7178        }