Changeset 2886

Show
Ignore:
Timestamp:
06/24/08 01:44:33 (2 months ago)
Author:
Shadowhand
Message:

Changes to Router:

  • Massive simplification of Router::setup()
  • Optimizations to Router::routed_uri()
  • Small cleanups all around
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/system/libraries/Router.php

    r2737 r2886  
    1212class Router_Core { 
    1313 
    14     protected static $routes = array(); 
     14    protected static $routes; 
    1515 
    1616    public static $current_uri  = ''; 
     
    1919    public static $url_suffix   = ''; 
    2020 
    21     public static $segments    = array(); 
    22     public static $rsegments   = array(); 
    23  
    24     public static $directory       = FALSE; 
    25     public static $controller      = FALSE; 
    26     public static $controller_path = FALSE; 
    27     public static $method          = FALSE; 
    28     public static $arguments       = FALSE; 
     21    public static $segments; 
     22    public static $rsegments; 
     23 
     24    public static $controller; 
     25    public static $controller_path; 
     26 
     27    public static $method    = 'index'; 
     28    public static $arguments = array(); 
    2929 
    3030    /** 
     
    4141        } 
    4242 
    43         // Load routing configuration 
    44         self::$routes = Config::item('routes'); 
     43        if (self::$routes === NULL) 
     44        { 
     45            // Load routes 
     46            self::$routes = Config::item('routes'); 
     47        } 
    4548 
    4649        // Default route status 
     
    8184        self::$rsegments = explode('/', self::$rsegments); 
    8285 
    83         // Prepare for Controller search 
    84         self::$directory  = ''; 
    85         self::$controller = ''; 
    86  
    87         // Optimize the check for the most common controller location 
    88         if (is_file(APPPATH.'controllers/'.self::$rsegments[0].EXT)) 
    89         { 
    90             self::$directory  = APPPATH.'controllers/'; 
    91             self::$controller = self::$rsegments[0]; 
    92             self::$method     = isset(self::$rsegments[1]) ? self::$rsegments[1] : 'index'; 
    93             self::$arguments  = isset(self::$rsegments[2]) ? array_slice(self::$rsegments, 2) : array(); 
    94         } 
    95         else 
    96         { 
    97             // Fetch the include paths 
    98             $include_paths = Config::include_paths(); 
    99  
    100             // Path to be added to as we search deeper 
    101             $search = 'controllers'; 
    102  
    103             // controller path to be added to as we search deeper 
    104             $controller_path = ''; 
    105  
    106             // Use the rsegments to find the controller 
    107             foreach (self::$rsegments as $key => $segment) 
    108             { 
    109                 foreach ($include_paths as $path) 
    110                 { 
    111                     // The controller has been found, all arguments can be set 
    112                     if (is_file($path.$search.'/'.$segment.EXT)) 
    113                     { 
    114                         self::$directory       = $path.$search.'/'; 
    115                         self::$controller_path = $controller_path; 
    116                         self::$controller      = $segment; 
    117                         self::$method          = isset(self::$rsegments[$key + 1]) ? self::$rsegments[$key + 1] : 'index'; 
    118                         self::$arguments       = isset(self::$rsegments[$key + 2]) ? array_slice(self::$rsegments, $key + 2) : array(); 
    119  
    120                         // Stop searching, two levels 
    121                         break 2; 
    122                     } 
    123                 } 
    124  
    125                 // Add the segment to the search 
    126                 $search .= '/'.$segment; 
    127                 $controller_path .= $segment.'/'; 
     86        // Prepare to find the controller 
     87        $controller_path = ''; 
     88 
     89        foreach (self::$rsegments as $key => $segment) 
     90        { 
     91            // Add the segment to the search path 
     92            $controller_path .= $segment; 
     93 
     94            if ($path = Kohana::find_file('controllers', $controller_path, FALSE)) 
     95            { 
     96                // Set controller name 
     97                self::$controller = $segment; 
     98 
     99                // Change controller path 
     100                self::$controller_path = $path; 
     101            } 
     102            else 
     103            { 
     104                // Stop searching 
     105                break; 
     106            } 
     107 
     108            // Add another slash 
     109            $controller_path .= '/'; 
     110        } 
     111 
     112        if (self::$controller !== NULL AND isset(self::$rsegments[$key])) 
     113        { 
     114            // Set method 
     115            self::$method = self::$rsegments[$key]; 
     116 
     117            if (isset(self::$rsegments[$key + 1])) 
     118            { 
     119                // Set arguments 
     120                self::$arguments = array_slice(self::$rsegments, $key + 1); 
    128121            } 
    129122        } 
     
    132125        Event::run('system.post_routing'); 
    133126 
    134         if (empty(self::$controller)) 
     127        if (self::$controller === NULL) 
    135128        { 
    136129            // No controller was found, so no page can be rendered 
     
    211204            { 
    212205                // Remove the URL suffix 
    213                 self::$current_uri = preg_replace('!'.preg_quote($suffix).'$!u', '', self::$current_uri); 
     206                self::$current_uri = preg_replace('#'.preg_quote($suffix).'$#u', '', self::$current_uri); 
    214207 
    215208                // Set the URL suffix 
     
    218211 
    219212            // Reduce multiple slashes into single slashes 
    220             self::$current_uri = preg_replace('!//+!', '/', self::$current_uri); 
     213            self::$current_uri = preg_replace('#//+#', '/', self::$current_uri); 
    221214        } 
    222215    } 
     
    230223    public static function routed_uri($uri) 
    231224    { 
    232         $routes = Config::item('routes'); 
    233         $uri    = $routed_uri = trim($uri, '/'); 
    234  
    235         if (isset($routes[$uri])) 
     225        if (self::$routes === NULL) 
     226        { 
     227            // Load routes 
     228            self::$routes = Config::item('routes'); 
     229        } 
     230 
     231        // Prepare variables 
     232        $routed_uri = $uri = trim($uri, '/'); 
     233 
     234        if (isset(self::$routes[$uri])) 
    236235        { 
    237236            // Literal match, no need for regex 
    238             $routed_uri = $routes[$uri]; 
     237            $routed_uri = self::$routes[$uri]; 
    239238        } 
    240239        else 
    241240        { 
    242241            // Loop through the routes and see if anything matches 
    243             foreach ($routes as $key => $val) 
     242            foreach (self::$routes as $key => $val) 
    244243            { 
    245244                if ($key === '_default') continue; 
     
    249248                $val = trim($val, '/'); 
    250249 
    251                 // Does this route match the current URI? 
    252250                if (preg_match('#^'.$key.'$#u', $uri)) 
    253251                { 
    254                     // If the regex contains a valid callback, we'll use it 
    255252                    if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE) 
    256253                    { 
     254                        // Use regex routing 
    257255                        $routed_uri = preg_replace('#^'.$key.'$#u', $val, $uri); 
    258256                    } 
    259257                    else 
    260258                    { 
     259                        // Standard routing 
    261260                        $routed_uri = $val; 
    262261                    } 
    263262 
    264                     // A valid route was found, stop parsing other routes 
     263                    // A valid route has been found 
    265264                    break; 
    266265                } 
     
    268267        } 
    269268 
    270         // Check router one more time to do some magic 
    271         if (isset($routes[$routed_uri])) 
    272         { 
    273             $routed_uri = $routes[$routed_uri]; 
    274         } 
    275  
    276         return $routed_uri; 
     269        if (isset(self::$routes[$routed_uri])) 
     270        { 
     271            // Check for double routing (without regex) 
     272            $routed_uri = self::$routes[$routed_uri]; 
     273        } 
     274 
     275        return trim($routed_uri, '/'); 
    277276    } 
    278277