Changeset 2488

Show
Ignore:
Timestamp:
04/14/08 13:23:54 (5 months ago)
Author:
armen
Message:

Added Router::routed_uri and moved its stuff outside of Router::setup.

Files:
1 modified

Legend:

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

    r2487 r2488  
    4040            throw new Kohana_Exception('core.no_default_route'); 
    4141 
     42        $default_route = FALSE; 
     43         
    4244        // Use the default route when no segments exist 
    4345        if (self::$current_uri === '') 
     
    4547            self::$current_uri = trim(self::$routes['_default'], '/'); 
    4648            $default_route = TRUE; 
    47         } 
    48         else 
    49         { 
    50             $default_route = FALSE; 
    5149        } 
    5250 
     
    6462        if ($default_route === FALSE AND count(self::$routes) > 1) 
    6563        { 
    66             if (isset(self::$routes[self::$current_uri])) 
    67             { 
    68                 // Literal match, no need for regex 
    69                 self::$rsegments = self::$routes[self::$current_uri]; 
    70             } 
    71             else 
    72             { 
    73                 // Loop through the routes and see if anything matches 
    74                 foreach(self::$routes as $key => $val) 
    75                 { 
    76                     if ($key === '_default' OR $key === '_allowed') continue; 
    77  
    78                     // Trim slashes 
    79                     $key = trim($key, '/'); 
    80                     $val = trim($val, '/'); 
    81  
    82                     // Does this route match the current URI? 
    83                     if (preg_match('#^'.$key.'$#u', self::$segments)) 
    84                     { 
    85                         // If the regex contains a valid callback, we'll use it 
    86                         if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE) 
    87                         { 
    88                             self::$rsegments = preg_replace('#^'.$key.'$#u', $val, self::$segments); 
    89                         } 
    90                         else 
    91                         { 
    92                             self::$rsegments = $val; 
    93                         } 
    94  
    95                         // A valid route was found, stop parsing other routes 
    96                         break; 
    97                     } 
    98                 } 
    99             } 
    100  
    101             // Check router one more time to do some magic 
    102             if (isset(self::$routes[self::$rsegments])) 
    103             { 
    104                 self::$rsegments = self::$routes[self::$rsegments]; 
    105             } 
     64            self::$rsegments = self::routed_uri(self::$current_uri); 
    10665        } 
    10766 
     
    283242    } 
    284243 
     244    /** 
     245     * Generates routed URI from given URI. 
     246     * 
     247     * @param  string  URI to convert 
     248     * @return string  Routed uri 
     249     */ 
     250    public static function routed_uri($uri) 
     251    { 
     252        $routes = Config::item('routes'); 
     253        $uri    = $routed_uri = trim($uri, '/'); 
     254     
     255        if (isset($routes[$uri])) 
     256        { 
     257            // Literal match, no need for regex 
     258            $routed_uri = $routes[$uri]; 
     259        } 
     260        else 
     261        { 
     262            // Loop through the routes and see if anything matches 
     263            foreach ($routes as $key => $val) 
     264            { 
     265                if ($key === '_default' OR $key === '_allowed') continue; 
     266 
     267                // Trim slashes 
     268                $key = trim($key, '/'); 
     269                $val = trim($val, '/'); 
     270 
     271                // Does this route match the current URI? 
     272                if (preg_match('#^'.$key.'$#u', $uri)) 
     273                { 
     274                    // If the regex contains a valid callback, we'll use it 
     275                    if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE) 
     276                    { 
     277                        $routed_uri = preg_replace('#^'.$key.'$#u', $val, $uri); 
     278                    } 
     279                    else 
     280                    { 
     281                        $routed_uri = $val; 
     282                    } 
     283 
     284                    // A valid route was found, stop parsing other routes 
     285                    break; 
     286                } 
     287            } 
     288        } 
     289 
     290        // Check router one more time to do some magic 
     291        if (isset($routes[$routed_uri])) 
     292        { 
     293            $routed_uri = $routes[$routed_uri]; 
     294        } 
     295 
     296        return $routed_uri; 
     297    }     
     298 
    285299} // End Router class