first commit of 2.0

This commit is contained in:
Taylor Otwell
2011-08-18 19:56:29 -05:00
parent 119b356bde
commit 1e90e42404
79 changed files with 796 additions and 688 deletions

View File

@@ -0,0 +1,56 @@
<?php namespace Laravel\Routing;
class Filter {
/**
* The loaded route filters.
*
* @var array
*/
private static $filters = array();
/**
* Register a set of route filters.
*
* @param array $filters
* @return void
*/
public static function register($filters)
{
static::$filters = array_merge(static::$filters, $filters);
}
/**
* Clear all of the registered route filters.
*
* @return void
*/
public static function clear()
{
static::$filters = array();
}
/**
* 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)
{
foreach (explode(', ', $filters) as $filter)
{
if ( ! isset(static::$filters[$filter])) continue;
$response = call_user_func_array(static::$filters[$filter], $parameters);
// "Before" filters may override the request cycle. For example, an authentication
// filter may redirect a user to a login view if they are not logged in. Because of
// this, we will return the first filter response if overriding is enabled.
if ( ! is_null($response) and $override) return $response;
}
}
}

View File

@@ -0,0 +1,40 @@
<?php namespace Laravel\Routing;
class Finder {
/**
* The named routes that have been found so far.
*
* @var array
*/
public static $names = array();
/**
* Find a named route in a given array of routes.
*
* @param string $name
* @param array $routes
* @return array
*/
public static function find($name, $routes)
{
if (array_key_exists($name, static::$names)) return static::$names[$name];
$arrayIterator = new \RecursiveArrayIterator($routes);
$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)
{
$route = $recursiveIterator->getSubIterator();
if (isset($route['name']) and $route['name'] == $name)
{
return static::$names[$name] = array($arrayIterator->key() => iterator_to_array($route));
}
}
}
}

126
laravel/routing/loader.php Normal file
View File

@@ -0,0 +1,126 @@
<?php namespace Laravel\Routing;
use Laravel\Config;
class Loader {
/**
* All of the routes for the application.
*
* @var array
*/
private static $routes;
/**
* The path where the routes are located.
*
* @var string
*/
public $path;
/**
* Create a new route loader instance.
*
* @param string $path
* @return void
*/
public function __construct($path)
{
$this->path = $path;
}
/**
* Load the appropriate routes for the request URI.
*
* @param string
* @return array
*/
public function load($uri)
{
$base = (file_exists($path = $this->path.'routes'.EXT)) ? require $path : array();
return array_merge($this->load_nested_routes(explode('/', $uri)), $base);
}
/**
* Load the appropriate routes from the routes directory.
*
* @param array $segments
* @return array
*/
private function load_nested_routes($segments)
{
// If the request URI only more than one segment, and the last segment contains a dot, we will
// assume the request is for a specific format (users.json or users.xml) and strip off
// everything after the dot so we can load the appropriate file.
if (count($segments) > 0 and strpos(end($segments), '.') !== false)
{
$segment = array_pop($segments);
array_push($segments, substr($segment, 0, strpos($segment, '.')));
}
// Since it is no part of the route directory structure, shift the module name off of the
// beginning of the array so we can locate the appropriate route file.
if (count($segments) > 0 and ACTIVE_MODULE !== DEFAULT_MODULE)
{
array_shift($segments);
}
// Work backwards through the URI segments until we find the deepest possible
// matching route directory. Once we find it, we will return those routes.
foreach (array_reverse($segments, true) as $key => $value)
{
if (file_exists($path = $this->path.'routes/'.implode('/', array_slice($segments, 0, $key + 1)).EXT))
{
return require $path;
}
}
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.
*
* @param bool $reload
* @return array
*/
public static function all($reload = false)
{
if ( ! is_null(static::$routes) and ! $reload) return static::$routes;
$routes = array();
foreach (Module::paths() as $path)
{
if (file_exists($path.'routes'.EXT))
{
$routes = array_merge($routes, require $path.'routes'.EXT);
}
if (is_dir($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($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($routes, require $file);
}
}
}
}
return static::$routes = $routes;
}
}

105
laravel/routing/route.php Normal file
View File

@@ -0,0 +1,105 @@
<?php namespace Laravel\Routing;
use Laravel\Package;
use Laravel\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;
// The callback may be in array form, meaning it has attached filters or is named and we
// will need to evaluate it further to determine what to do. If the callback is just a
// closure, we can execute it now and return the result.
if (is_callable($this->callback))
{
$response = call_user_func_array($this->callback, $this->parameters);
}
elseif (is_array($this->callback))
{
if (isset($this->callback['needs']))
{
Package::load(explode(', ', $this->callback['needs']));
}
$response = isset($this->callback['before']) ? Filter::call($this->callback['before'], array(), true) : null;
if (is_null($response) and ! is_null($handler = $this->find_route_function()))
{
$response = call_user_func_array($handler, $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;
}
/**
* Extract the route function from the route.
*
* If a "do" index is specified on the callback, that is the handler.
* Otherwise, we will return the first callable array value.
*
* @return Closure
*/
private function find_route_function()
{
if (isset($this->callback['do'])) return $this->callback['do'];
foreach ($this->callback as $value)
{
if (is_callable($value)) return $value;
}
}
}

116
laravel/routing/router.php Normal file
View File

@@ -0,0 +1,116 @@
<?php namespace Laravel\Routing;
use Laravel\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 Loader $loader
* @return void
*/
public function __construct($method, $uri, $loader)
{
// 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 = $loader->load($uri);
}
/**
* Create a new router for a request method and URI.
*
* @param string $method
* @param string $uri
* @param Loader $loader
* @return Router
*/
public static function make($method, $uri, $loader)
{
return new static($method, $uri, $loader);
}
/**
* Search a set of routes for the route matching a method and URI.
*
* @return Route
*/
public function route()
{
// Check for a literal route match first. If we find one, there is
// no need to spin through all of the routes.
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))));
}
}