moved all routing classes into routing namespace.
This commit is contained in:
43
system/routing/filter.php
Normal file
43
system/routing/filter.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php namespace System\Routing;
|
||||
|
||||
class Filter {
|
||||
|
||||
/**
|
||||
* The loaded route filters.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $filters;
|
||||
|
||||
/**
|
||||
* Call a set of route filters.
|
||||
*
|
||||
* @param string $filter
|
||||
* @param array $parameters
|
||||
* @param bool $override
|
||||
* @return mixed
|
||||
*/
|
||||
public static function call($filters, $parameters = array(), $override = false)
|
||||
{
|
||||
if (is_null(static::$filters))
|
||||
{
|
||||
static::$filters = require APP_PATH.'filters'.EXT;
|
||||
}
|
||||
|
||||
foreach (explode(', ', $filters) as $filter)
|
||||
{
|
||||
if ( ! isset(static::$filters[$filter]))
|
||||
{
|
||||
throw new \Exception("Route filter [$filter] is not defined.");
|
||||
}
|
||||
|
||||
$response = call_user_func_array(static::$filters[$filter], $parameters);
|
||||
|
||||
if ( ! is_null($response) and $override)
|
||||
{
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
89
system/routing/finder.php
Normal file
89
system/routing/finder.php
Normal file
@@ -0,0 +1,89 @@
|
||||
<?php namespace System\Routing;
|
||||
|
||||
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)
|
||||
{
|
||||
// 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))
|
||||
{
|
||||
return static::$names[$name];
|
||||
}
|
||||
|
||||
$arrayIterator = new \RecursiveArrayIterator(static::$routes);
|
||||
|
||||
$recursiveIterator = new \RecursiveIteratorIterator($arrayIterator);
|
||||
|
||||
foreach ($recursiveIterator as $iterator)
|
||||
{
|
||||
$route = $recursiveIterator->getSubIterator();
|
||||
|
||||
if (isset($route['name']) and $route['name'] == $name)
|
||||
{
|
||||
return static::$names[$name] = array($arrayIterator->key() => iterator_to_array($route));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
}
|
||||
33
system/routing/loader.php
Normal file
33
system/routing/loader.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php namespace System\Routing;
|
||||
|
||||
class Loader {
|
||||
|
||||
/**
|
||||
* Load the appropriate routes for the request URI.
|
||||
*
|
||||
* @param string
|
||||
* @return array
|
||||
*/
|
||||
public static function load($uri)
|
||||
{
|
||||
$base = require APP_PATH.'routes'.EXT;
|
||||
|
||||
if ( ! is_dir(APP_PATH.'routes') or $uri == '')
|
||||
{
|
||||
return $base;
|
||||
}
|
||||
|
||||
list($routes, $segments) = array(array(), explode('/', $uri));
|
||||
|
||||
foreach (array_reverse($segments, true) as $key => $value)
|
||||
{
|
||||
if (file_exists($path = ROUTE_PATH.implode('/', array_slice($segments, 0, $key + 1)).EXT))
|
||||
{
|
||||
$routes = require $path;
|
||||
}
|
||||
}
|
||||
|
||||
return array_merge($routes, $base);
|
||||
}
|
||||
|
||||
}
|
||||
78
system/routing/route.php
Normal file
78
system/routing/route.php
Normal file
@@ -0,0 +1,78 @@
|
||||
<?php namespace System\Routing;
|
||||
|
||||
use System\Response;
|
||||
|
||||
class Route {
|
||||
|
||||
/**
|
||||
* The route key, including request method and URI.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $key;
|
||||
|
||||
/**
|
||||
* The route callback or array.
|
||||
*
|
||||
* @var mixed
|
||||
*/
|
||||
public $callback;
|
||||
|
||||
/**
|
||||
* The parameters that will passed to the route function.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $parameters;
|
||||
|
||||
/**
|
||||
* Create a new Route instance.
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $callback
|
||||
* @param array $parameters
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($key, $callback, $parameters = array())
|
||||
{
|
||||
$this->key = $key;
|
||||
$this->callback = $callback;
|
||||
$this->parameters = $parameters;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the route function.
|
||||
*
|
||||
* @param mixed $route
|
||||
* @param array $parameters
|
||||
* @return Response
|
||||
*/
|
||||
public function call()
|
||||
{
|
||||
$response = null;
|
||||
|
||||
if (is_callable($this->callback))
|
||||
{
|
||||
$response = call_user_func_array($this->callback, $this->parameters);
|
||||
}
|
||||
elseif (is_array($this->callback))
|
||||
{
|
||||
$response = isset($this->callback['before']) ? Filter::call($this->callback['before'], array(), true) : null;
|
||||
|
||||
if (is_null($response) and isset($this->callback['do']))
|
||||
{
|
||||
$response = call_user_func_array($this->callback['do'], $this->parameters);
|
||||
}
|
||||
}
|
||||
|
||||
$response = Response::prepare($response);
|
||||
|
||||
if (is_array($this->callback) and isset($this->callback['after']))
|
||||
{
|
||||
Filter::call($this->callback['after'], array($response));
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
}
|
||||
113
system/routing/router.php
Normal file
113
system/routing/router.php
Normal file
@@ -0,0 +1,113 @@
|
||||
<?php namespace System\Routing;
|
||||
|
||||
use System\Request;
|
||||
|
||||
class Router {
|
||||
|
||||
/**
|
||||
* The request method and URI.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $request;
|
||||
|
||||
/**
|
||||
* All of the loaded routes.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $routes;
|
||||
|
||||
/**
|
||||
* Create a new router for a request method and URI.
|
||||
*
|
||||
* @param string $method
|
||||
* @param string $uri
|
||||
* @param array $routes
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($method, $uri, $routes)
|
||||
{
|
||||
// Put the request method and URI in route form. Routes begin with
|
||||
// the request method and a forward slash.
|
||||
$this->request = $method.' /'.trim($uri, '/');
|
||||
$this->routes = $routes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new router for a request method and URI.
|
||||
*
|
||||
* @param string $method
|
||||
* @param string $uri
|
||||
* @param array $routes
|
||||
* @return Router
|
||||
*/
|
||||
public static function make($method, $uri, $routes = null)
|
||||
{
|
||||
return new static($method, $uri, $routes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Search a set of routes for the route matching a method and URI.
|
||||
*
|
||||
* @return Route
|
||||
*/
|
||||
public function route()
|
||||
{
|
||||
if (isset($this->routes[$this->request]))
|
||||
{
|
||||
return Request::$route = new Route($this->request, $this->routes[$this->request]);
|
||||
}
|
||||
|
||||
foreach ($this->routes as $keys => $callback)
|
||||
{
|
||||
// Only check routes that have multiple URIs or wildcards.
|
||||
// Other routes would have been caught by the check for literal matches.
|
||||
if (strpos($keys, '(') !== false or strpos($keys, ',') !== false )
|
||||
{
|
||||
foreach (explode(', ', $keys) as $key)
|
||||
{
|
||||
if (preg_match('#^'.$this->translate_wildcards($key).'$#', $this->request))
|
||||
{
|
||||
return Request::$route = new Route($keys, $callback, $this->parameters($this->request, $key));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Translate route URI wildcards into actual regular expressions.
|
||||
*
|
||||
* @param string $key
|
||||
* @return string
|
||||
*/
|
||||
private function translate_wildcards($key)
|
||||
{
|
||||
$replacements = 0;
|
||||
|
||||
// For optional parameters, first translate the wildcards to their
|
||||
// regex equivalent, sans the ")?" ending. We will add the endings
|
||||
// back on after we know how many replacements we made.
|
||||
$key = str_replace(array('/(:num?)', '/(:any?)'), array('(?:/([0-9]+)', '(?:/([a-zA-Z0-9\-_]+)'), $key, $replacements);
|
||||
|
||||
$key .= ($replacements > 0) ? str_repeat(')?', $replacements) : '';
|
||||
|
||||
return str_replace(array(':num', ':any'), array('[0-9]+', '[a-zA-Z0-9\-_]+'), $key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract the parameters from a URI based on a route URI.
|
||||
*
|
||||
* Any route segment wrapped in parentheses is considered a parameter.
|
||||
*
|
||||
* @param string $uri
|
||||
* @param string $route
|
||||
* @return array
|
||||
*/
|
||||
private function parameters($uri, $route)
|
||||
{
|
||||
return array_values(array_intersect_key(explode('/', $uri), preg_grep('/\(.+\)/', explode('/', $route))));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user