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/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        }