added more dependency injection to route loader and finder.
This commit is contained in:
parent
0af326b636
commit
f9f168eacb
|
@ -2,13 +2,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.
|
||||||
*
|
*
|
||||||
|
@ -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 @@ public static function find($name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
|
@ -2,6 +2,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.
|
||||||
*
|
*
|
||||||
|
@ -39,4 +46,38 @@ private function load_nested_routes($uri)
|
||||||
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -71,7 +71,7 @@ public static function to_asset($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));
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue