overall code refactoring.
This commit is contained in:
86
system/route/finder.php
Normal file
86
system/route/finder.php
Normal file
@@ -0,0 +1,86 @@
|
||||
<?php namespace System\Route;
|
||||
|
||||
class Finder {
|
||||
|
||||
/**
|
||||
* All of the loaded routes.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $routes;
|
||||
|
||||
/**
|
||||
* The named routes that have been found so far.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $names = array();
|
||||
|
||||
/**
|
||||
* Find a route by name.
|
||||
*
|
||||
* @param string $name
|
||||
* @return array
|
||||
*/
|
||||
public static function find($name)
|
||||
{
|
||||
// --------------------------------------------------------------
|
||||
// Load the routes if we haven't already.
|
||||
// --------------------------------------------------------------
|
||||
if (is_null(static::$routes))
|
||||
{
|
||||
static::$routes = (is_dir(APP_PATH.'routes')) ? static::load() : require APP_PATH.'routes'.EXT;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------
|
||||
// Have we already located this route by name?
|
||||
// --------------------------------------------------------------
|
||||
if (array_key_exists($name, static::$names))
|
||||
{
|
||||
return static::$names[$name];
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------
|
||||
// Instantiate the SPL array iterators.
|
||||
// --------------------------------------------------------------
|
||||
$arrayIterator = new \RecursiveArrayIterator(static::$routes);
|
||||
$recursiveIterator = new \RecursiveIteratorIterator($arrayIterator);
|
||||
|
||||
// --------------------------------------------------------------
|
||||
// Iterate over the routes and find the named route.
|
||||
// --------------------------------------------------------------
|
||||
foreach ($recursiveIterator as $iterator)
|
||||
{
|
||||
$route = $recursiveIterator->getSubIterator();
|
||||
|
||||
if ($route['name'] == $name)
|
||||
{
|
||||
return static::$names[$name] = array($arrayIterator->key() => iterator_to_array($route));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load all of the routes from the routes directory.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private static function load()
|
||||
{
|
||||
$routes = array();
|
||||
|
||||
// --------------------------------------------------------------
|
||||
// Merge all of the various route files together.
|
||||
// --------------------------------------------------------------
|
||||
foreach (glob(APP_PATH.'routes/*') as $file)
|
||||
{
|
||||
if (filetype($file) == 'file')
|
||||
{
|
||||
$routes = array_merge(require $file, $routes);
|
||||
}
|
||||
}
|
||||
|
||||
return $routes;
|
||||
}
|
||||
|
||||
}
|
||||
49
system/route/loader.php
Normal file
49
system/route/loader.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php namespace System\Route;
|
||||
|
||||
class Loader {
|
||||
|
||||
/**
|
||||
* Load the route file based on the first segment of the URI.
|
||||
*
|
||||
* @param string $uri
|
||||
* @return void
|
||||
*/
|
||||
public static function load($uri)
|
||||
{
|
||||
// --------------------------------------------------------------
|
||||
// If a single routes is being used, return it.
|
||||
// --------------------------------------------------------------
|
||||
if ( ! is_dir(APP_PATH.'routes'))
|
||||
{
|
||||
return require APP_PATH.'routes'.EXT;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------
|
||||
// If the request is to the root, load the "home" routes file.
|
||||
// --------------------------------------------------------------
|
||||
if ($uri == '/')
|
||||
{
|
||||
if ( ! file_exists(APP_PATH.'routes/home'.EXT))
|
||||
{
|
||||
throw new \Exception("A [home] route file is required when using a route directory.");
|
||||
}
|
||||
|
||||
return require APP_PATH.'routes/home'.EXT;
|
||||
}
|
||||
// --------------------------------------------------------------
|
||||
// Load the route file matching the first segment of the URI.
|
||||
// --------------------------------------------------------------
|
||||
else
|
||||
{
|
||||
$segments = explode('/', trim($uri, '/'));
|
||||
|
||||
if ( ! file_exists(APP_PATH.'routes/'.$segments[0].EXT))
|
||||
{
|
||||
throw new \Exception("No route file defined for routes beginning with [".$segments[0]."]");
|
||||
}
|
||||
|
||||
return array_merge(require APP_PATH.'routes/'.$segments[0].EXT, require APP_PATH.'routes/home'.EXT);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
45
system/route/parser.php
Normal file
45
system/route/parser.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php namespace System\Route;
|
||||
|
||||
class Parser {
|
||||
|
||||
/**
|
||||
* Get the parameters that should be passed to the route callback.
|
||||
*
|
||||
* @param string $uri
|
||||
* @param string $route
|
||||
* @return array
|
||||
*/
|
||||
public static function parameters($uri, $route)
|
||||
{
|
||||
// --------------------------------------------------------------
|
||||
// Split the request URI into segments.
|
||||
// --------------------------------------------------------------
|
||||
$uri_segments = explode('/', $uri);
|
||||
|
||||
// --------------------------------------------------------------
|
||||
// Split the route URI into segments.
|
||||
// --------------------------------------------------------------
|
||||
$route_segments = explode('/', $route);
|
||||
|
||||
// --------------------------------------------------------------
|
||||
// Initialize the array of parameters.
|
||||
// --------------------------------------------------------------
|
||||
$parameters = array();
|
||||
|
||||
// --------------------------------------------------------------
|
||||
// Extract all of the parameters out of the URI.
|
||||
//
|
||||
// Any segment wrapped in parentheses is considered a parameter.
|
||||
// --------------------------------------------------------------
|
||||
for ($i = 0; $i < count($route_segments); $i++)
|
||||
{
|
||||
if (strpos($route_segments[$i], '(') === 0)
|
||||
{
|
||||
$parameters[] = $uri_segments[$i];
|
||||
}
|
||||
}
|
||||
|
||||
return $parameters;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user