Commit f9f168ea by Taylor Otwell

added more dependency injection to route loader and finder.

parent 0af326b6
...@@ -3,13 +3,6 @@ ...@@ -3,13 +3,6 @@
class Finder { class Finder {
/** /**
* All of the loaded routes.
*
* @var array
*/
public static $routes;
/**
* The named routes that have been found so far. * The named routes that have been found so far.
* *
* @var array * @var array
...@@ -17,35 +10,25 @@ class Finder { ...@@ -17,35 +10,25 @@ class Finder {
public static $names = array(); public static $names = array();
/** /**
* Find a route by name. * Find a named route in a given array of routes.
* *
* @param string $name * @param string $name
* @param array $routes
* @return array * @return array
*/ */
public static function find($name) public static function find($name, $routes)
{
// This class maintains its own list of routes because the router only loads routes that
// are applicable to the current request URI. But, this class obviously needs access
// to all of the routes, not just the ones applicable to the request URI.
if (is_null(static::$routes))
{
static::$routes = require APP_PATH.'routes'.EXT;
if (is_dir(APP_PATH.'routes'))
{ {
static::$routes = array_merge(static::load(), static::$routes);
}
}
if (array_key_exists($name, static::$names)) if (array_key_exists($name, static::$names))
{ {
return static::$names[$name]; return static::$names[$name];
} }
$arrayIterator = new \RecursiveArrayIterator(static::$routes); $arrayIterator = new \RecursiveArrayIterator($routes);
$recursiveIterator = new \RecursiveIteratorIterator($arrayIterator); $recursiveIterator = new \RecursiveIteratorIterator($arrayIterator);
// Since routes can be nested deep within sub-directories, we need to recursively
// iterate through each directory and gather all of the routes.
foreach ($recursiveIterator as $iterator) foreach ($recursiveIterator as $iterator)
{ {
$route = $recursiveIterator->getSubIterator(); $route = $recursiveIterator->getSubIterator();
...@@ -57,33 +40,4 @@ class Finder { ...@@ -57,33 +40,4 @@ class Finder {
} }
} }
/**
* Load all of the routes from the routes directory.
*
* All of the various route files will be merged together
* into a single array that can be searched.
*
* @return array
*/
private static function load()
{
$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(APP_PATH.'routes');
$recursiveIterator = new \RecursiveIteratorIterator($directoryIterator, \RecursiveIteratorIterator::SELF_FIRST);
foreach ($recursiveIterator as $file)
{
if (filetype($file) === 'file' and strpos($file, EXT) !== false)
{
$routes = array_merge(require $file, $routes);
}
}
return $routes;
}
} }
\ No newline at end of file
...@@ -3,6 +3,13 @@ ...@@ -3,6 +3,13 @@
class Loader { class Loader {
/** /**
* All of the routes for the application.
*
* @var array
*/
private static $routes;
/**
* Load the appropriate routes for the request URI. * Load the appropriate routes for the request URI.
* *
* @param string * @param string
...@@ -39,4 +46,38 @@ class Loader { ...@@ -39,4 +46,38 @@ class Loader {
return array(); return array();
} }
/**
* Get all of the routes for the application.
*
* To improve performance, this operation will only be performed once. The routes
* will be cached and returned on every subsequent call.
*
* @return array
*/
public static function everything()
{
if ( ! is_null(static::$routes)) return static::$routes;
$routes = require APP_PATH.'routes'.EXT;
if (is_dir(APP_PATH.'routes'))
{
// 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(APP_PATH.'routes');
$recursiveIterator = new \RecursiveIteratorIterator($directoryIterator, \RecursiveIteratorIterator::SELF_FIRST);
foreach ($recursiveIterator as $file)
{
if (filetype($file) === 'file' and strpos($file, EXT) !== false)
{
$routes = array_merge(require $file, $routes);
}
}
}
return static::$routes = $routes;
}
} }
\ No newline at end of file
...@@ -71,7 +71,7 @@ class URL { ...@@ -71,7 +71,7 @@ class URL {
*/ */
public static function to_route($name, $parameters = array(), $https = false) public static function to_route($name, $parameters = array(), $https = false)
{ {
if ( ! is_null($route = Routing\Finder::find($name))) if ( ! is_null($route = Routing\Finder::find($name, Routing\Loader::everything())))
{ {
$uris = explode(', ', key($route)); $uris = explode(', ', key($route));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment