From 7aecd7857fc01ecc76fdf15e135929e166a60a18 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Fri, 27 Jan 2012 08:47:12 -0600 Subject: [PATCH] refactoring the router. --- laravel/helpers.php | 11 +++++++++++ laravel/routing/router.php | 15 +++++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/laravel/helpers.php b/laravel/helpers.php index 0bad860b..f533999f 100644 --- a/laravel/helpers.php +++ b/laravel/helpers.php @@ -219,6 +219,17 @@ function array_strip_slashes($array) return $result; } +/** + * Divide an array into two arrays. One with keys and the other with values. + * + * @param array $array + * @return array + */ +function array_divide($array) +{ + return array(array_keys($array), array_values($array)); +} + /** * Determine if "Magic Quotes" are enabled on the server. * diff --git a/laravel/routing/router.php b/laravel/routing/router.php index a28a529d..6e19baf2 100644 --- a/laravel/routing/router.php +++ b/laravel/routing/router.php @@ -167,7 +167,9 @@ public static function route($method, $uri) $uri = ltrim($uri, '/'); } - return static::controller($bundle, $method, $destination, Str::segments($uri)); + $segments = Str::segments($uri); + + return static::controller($bundle, $method, $destination, $segments); } /** @@ -285,21 +287,26 @@ protected static function controller_key($segments, $directory) } /** - * Translate route URI wildcards into actual regular expressions. + * Translate route URI wildcards into regular expressions. * * @param string $key * @return string */ protected static function wildcards($key) { + list($search, $replace) = array_divide(static::$optional); + // For optional parameters, first translate the wildcards to their // regex equivalent, sans the ")?" ending. We'll add the endings // back on after we know how many replacements we made. - $key = str_replace(array_keys(static::$optional), array_values(static::$optional), $key, $count); + $key = str_replace($search, $replace, $key, $count); $key .= ($count > 0) ? str_repeat(')?', $count) : ''; - return str_replace(array_keys(static::$patterns), array_values(static::$patterns), $key); + // For "regular" parameters, we can just do a simple translate + // using the patterns array. There is not need to cap the + // pattern like we did with optional parameters. + return strtr($key, static::$patterns); } } \ No newline at end of file