From 096280c95878bd2940c04ce1d6c26ff0621465bc Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 20 Sep 2011 00:07:16 -0500 Subject: [PATCH] refactoring route loader and router. --- laravel/routing/loader.php | 26 +++++++++++++------------- laravel/routing/router.php | 17 ++++++++--------- public/index.php | 9 +-------- 3 files changed, 22 insertions(+), 30 deletions(-) diff --git a/laravel/routing/loader.php b/laravel/routing/loader.php index 9d846a0b..c10a351b 100644 --- a/laravel/routing/loader.php +++ b/laravel/routing/loader.php @@ -64,31 +64,25 @@ public function load($uri) */ protected function nested($segments) { - $routes = array(); - // Work backwards through the URI segments until we find the deepest possible // matching route directory. Once we find it, we will return those routes. foreach (array_reverse($segments, true) as $key => $value) { - // First we check to determine if there is a route file matching the segment - // of the URI. If there is, its routes will be merged into the route array. if (file_exists($path = $this->nest.implode('/', array_slice($segments, 0, $key + 1)).EXT)) { - $routes = array_merge($routes, require $path); + return require $path; } - // Even if we have already loaded routes for the URI, we still want to check - // for a "routes.php" file which could handle the root route and any routes - // that are impossible to handle in an explicitly named file. + // Even if we didn't find a matching file for the segment, we still want to + // check for a "routes.php" file which could handle the root route and any + // routes that are impossible to handle in an explicitly named file. if (file_exists($path = str_replace('.php', '/routes.php', $path))) { - $routes = array_merge($routes, require $path); + return require $path; } - - if (count($routes) > 0) return $routes; } - return $routes; + return array(); } /** @@ -105,9 +99,15 @@ public function everything() $routes = array(); + // First we will check for the base routes file in the application directory. + if (file_exists($path = $this->base.'routes'.EXT)) + { + $routes = array_merge($routes, require $path); + } + // Since route files can be nested deep within the route directory, we need to // recursively spin through each directory to find every file. - $recursiveIterator = new Iterator(new DirectoryIterator($this->nest), Iterator::SELF_FIRST); + $iterator = new Iterator(new DirectoryIterator($this->nest), Iterator::SELF_FIRST); foreach ($iterator as $file) { diff --git a/laravel/routing/router.php b/laravel/routing/router.php index 3e7e8a74..616d5fed 100644 --- a/laravel/routing/router.php +++ b/laravel/routing/router.php @@ -48,19 +48,18 @@ public function __construct(Loader $loader, $controllers) */ public function find($name) { + // First we will check the cache of route names. If we have already found the given route, + // we will simply return that route from the cache to improve performance. if (array_key_exists($name, $this->names)) return $this->names[$name]; - $arrayIterator = new \RecursiveArrayIterator($this->loader->everything()); - - $recursiveIterator = new \RecursiveIteratorIterator($arrayIterator); - - foreach ($recursiveIterator as $iterator) + // Spin through every route defined for the application searching for a route that has + // a name matching the name passed to the method. If the route is found, it will be + // cached in the array of named routes and returned. + foreach ($this->loader->everything() as $key => $value) { - $route = $recursiveIterator->getSubIterator(); - - if (isset($route['name']) and $route['name'] === $name) + if (is_array($value) and isset($value['name']) and $value['name'] === $name) { - return $this->names[$name] = array($arrayIterator->key() => iterator_to_array($route)); + return $this->names[$name] = array($key => $value); } } } diff --git a/public/index.php b/public/index.php index 17627ad5..44e94ffe 100644 --- a/public/index.php +++ b/public/index.php @@ -15,11 +15,6 @@ */ define('START_TIME', microtime(true)); -function elapsed() -{ - return number_format((microtime(true) - START_TIME) * 1000, 2); -} - /* |-------------------------------------------------------------------------- | Laravel Installation Paths @@ -48,6 +43,4 @@ function elapsed() | 3... 2... 1... Lift-off! |-------------------------------------------------------------------------- */ -require $laravel.'/laravel.php'; - -echo elapsed(); \ No newline at end of file +require $laravel.'/laravel.php'; \ No newline at end of file