Changeset 3408

Show
Ignore:
Timestamp:
08/31/2008 06:56:54 AM (3 months ago)
Author:
Geert
Message:

The mandatory router (regex) optimizations everybody was waiting for:

  • compile() about 15% faster now
  • keys() about 10% faster now
  • other minor cleanups
Location:
trunk/system
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/system/classes/router.php

    r3405 r3408  
    2626        /** 
    2727         * Router setup routine. Called during the [system.routing][ref-esr] 
    28          * Event by default.  
     28         * Event by default. 
    2929         * 
    3030         * [ref-esr]: http://docs.kohanaphp.com/events/system.routing 
     
    3434        public static function setup() 
    3535        { 
    36                 // Remove all dot-paths from the URI, they are not valid 
    37                 self::$current_uri = trim(preg_replace('#\.[\s./]*/#', '', self::$current_uri), '/'); 
    38  
    3936                // Set the complete URI 
    4037                self::$complete_uri = self::$current_uri.self::$query_string; 
     
    4845                        ( 
    4946                                'Routing API Changed!', 
    50                                 'Routing has been significantly changed, and your configuration '. 
    51                                 'files are not up to date. Please check http://dev.kohanaphp.com/changeset/3366 '. 
    52                                 'for more details.' 
     47                                'Routing has been significantly changed, and your configuration files are not up to date. '. 
     48                                'Please check http://dev.kohanaphp.com/changeset/3366 for more details.' 
    5349                        ); 
    5450                } 
     
    204200                if (($strpos_fc = strpos(self::$current_uri, $fc)) !== FALSE) 
    205201                { 
    206                         // Remove the front controller from the current uri 
     202                        // Remove the front controller from the current URI 
    207203                        self::$current_uri = substr(self::$current_uri, $strpos_fc + strlen($fc)); 
    208204                } 
     
    215211                        // Reduce multiple slashes into single slashes 
    216212                        self::$current_uri = preg_replace('#//+#', '/', self::$current_uri); 
     213 
     214                        // Remove all dot-paths from the URI, they are not valid 
     215                        self::$current_uri = preg_replace('#\.[\s./]*/#', '', self::$current_uri); 
    217216 
    218217                        // Make sure the URL is not tainted with HTML characters 
     
    264263 
    265264                // Remove trailing parts from the URI 
    266                 $uri = preg_replace('#/?\:.+$#', '', $uri); 
     265                $uri = preg_replace('#/?:.+$#', '', $uri); 
    267266 
    268267                return $uri; 
     
    281280 
    282281                // Find all keys that start with a colon 
    283                 preg_match_all('#:([a-z]+)#', $uri, $keys); 
    284  
    285                 return $keys[1]; 
    286         } 
    287  
    288         /** 
    289          * Creates a [regular expression][ref-reg] that can be used to match a  
     282                preg_match_all('#(?<=:)[a-z]+#', $uri, $keys); 
     283 
     284                return $keys[0]; 
     285        } 
     286 
     287        /** 
     288         * Creates a [regular expression][ref-reg] that can be used to match a 
    290289         * route against a URI with [preg_match][ref-prm]. 
    291290         * 
     
    302301 
    303302                // Regular expression end 
    304                 $end = array(); 
     303                $end = ''; 
    305304 
    306305                // Nothing is optional yet 
     
    309308                foreach ($uri as $i => $segment) 
    310309                { 
    311                         // Regular expression 
    312                         $exp = array(); 
    313  
    314310                        if ($segment[0] === ':') 
    315311                        { 
    316312                                // Find the actual segment key and any trailing garbage 
    317                                 preg_match('#^:([a-z]+)(.*)$#', $segment, $matches); 
     313                                preg_match('#^:([a-z]++)(.*)$#', $segment, $matches); 
    318314 
    319315                                // Segment key 
    320316                                $key = $matches[1]; 
     317 
     318                                // Regular expression 
     319                                $exp = ''; 
    321320 
    322321                                if ($optional === FALSE AND isset($route[$key])) 
     
    329328                                if ($optional === TRUE) 
    330329                                { 
    331                                         // Start the expression as an optional match 
    332                                         $exp[] = '(?:'; 
     330                                        // Start the expression as non-capturing group 
     331                                        $exp .= '(?:'; 
     332 
     333                                        // End the expression as an optional match 
     334                                        $end .= ')?'; 
    333335                                } 
    334336 
     
    336338                                { 
    337339                                        // Add the slash from the previous segment 
    338                                         $exp[] = '/'; 
     340                                        $exp .= '/'; 
    339341                                } 
    340342 
     
    342344                                { 
    343345                                        // Matches specified regex for the segment 
    344                                         $exp[] = '('.$route['regex'][$key].')'; 
     346                                        $exp .= '('.$route['regex'][$key].')'; 
    345347                                } 
    346348                                else 
    347349                                { 
    348                                         // Default regex matches all characters 
    349                                         $exp[] = '([^/]+)'; 
     350                                        // Default regex matches all characters except slashes 
     351                                        $exp .= '([^/]++)'; 
    350352                                } 
    351353 
     
    353355                                { 
    354356                                        // Add trailing segment junk 
    355                                         $exp[] = preg_quote($matches[2], '#'); 
    356                                 } 
    357  
    358                                 if ($optional === TRUE) 
    359                                 { 
    360                                         // End the expression 
    361                                         $end[] = ')?'; 
     357                                        $exp .= preg_quote($matches[2], '#'); 
    362358                                } 
    363359 
    364360                                // Replace the segment with the segment expression 
    365                                 $uri[$i] = implode('', $exp); 
     361                                $uri[$i] = $exp; 
    366362                        } 
    367363                        else 
     
    378374                } 
    379375 
    380                 return implode('', $uri).implode('', $end); 
     376                return implode('', $uri).$end; 
    381377        } 
    382378 
  • trunk/system/config/routes.php

    r3407 r3408  
    1717 * The converted regex for this route is: 
    1818 * 
    19  *     (?:([^/]+)(?:/([^/]+)(?:/([^/]+))?)?)? 
     19 *     (?:([^/]++)(?:/([^/]++)(?:/([^/]++))?)?)? 
    2020 * 
    2121 * To define a specific pattern for a key, you can use the special "regex" key: