From 82f3784d2b5c8867f7ec0f071b3f9b8241351af8 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 19 Sep 2011 23:43:17 -0500 Subject: [PATCH] refactored and comment route loader. --- laravel/routing/loader.php | 39 ++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/laravel/routing/loader.php b/laravel/routing/loader.php index ac8196df..9d846a0b 100644 --- a/laravel/routing/loader.php +++ b/laravel/routing/loader.php @@ -1,6 +1,8 @@ $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)) { - return require $path; + $routes = array_merge($routes, require $path); } - elseif (file_exists($path = str_replace('.php', '/routes.php', $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. + if (file_exists($path = str_replace('.php', '/routes.php', $path))) { - return require $path; + $routes = array_merge($routes, require $path); } + + if (count($routes) > 0) return $routes; } - return array(); + return $routes; } /** * Get every route defined for the application. * + * For fast performance, if the routes have already been loaded once, they will not + * be loaded again, and the same routes will be returned on subsequent calls. + * * @return array */ public function everything() @@ -88,14 +106,15 @@ class Loader { $routes = array(); // Since route files can be nested deep within the route directory, we need to - // recursively spin through the directory to find every file. - $directoryIterator = new \RecursiveDirectoryIterator($this->nest); + // recursively spin through each directory to find every file. + $recursiveIterator = new Iterator(new DirectoryIterator($this->nest), Iterator::SELF_FIRST); - $recursiveIterator = new \RecursiveIteratorIterator($directoryIterator, \RecursiveIteratorIterator::SELF_FIRST); - - foreach ($recursiveIterator as $file) + foreach ($iterator as $file) { - if (filetype($file) === 'file') + // Since some Laravel developers may place HTML files in the route directories, we will + // check for the PHP extension before merging the file. Typically, the HTML files are + // present in installations that are not using mod_rewrite and the public directory. + if (filetype($file) === 'file' and strpos($file, EXT) !== false) { $routes = array_merge(require $file, $routes); }