Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Remove global routing #656

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions classes/Kohana/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -454,13 +454,11 @@ public static function post_max_size_exceeded()
* @param array $routes Route
* @return array
*/
public static function process(Request $request, $routes = NULL)
public static function process(Request $request, array $routes = [])
{
// Load routes
$routes = (empty($routes)) ? Route::all() : $routes;
$params = NULL;

foreach ($routes as $name => $route)
foreach ($routes as $route)
{
// Use external routes for reverse routing only
if ($route->is_external())
Expand Down Expand Up @@ -571,7 +569,7 @@ protected static function _parse_accept( & $header, array $accepts = NULL)
protected $_route;

/**
* @var Route array of routes to manually look at instead of the global namespace
* @var array array of routes to manually look at instead of the global namespace
*/
protected $_routes;

Expand Down Expand Up @@ -803,6 +801,29 @@ public function referrer($referrer = NULL)
return $this;
}

/**
* Gets injected/set routes from the request.
*
* @return array injected routes
*/
public function get_routes()
{
return $this->_routes;
}

/**
* Set routes to the request. Overrides injected routes.
*
* @param array $routes routes to override injected routes
* @return self
*/
public function set_routes(array $routes)
{
$this->_routes = $routes;

return $this;
}

/**
* Sets and gets the route from the request.
*
Expand Down
2 changes: 2 additions & 0 deletions classes/Kohana/Request/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,11 +406,13 @@ public static function on_header_location(Request $request, Response $response,
}

// Prepare the additional request, copying any follow_headers that were present on the original request
$orig_routes = $request->get_routes();
$orig_headers = $request->headers()->getArrayCopy();
$follow_header_keys = array_intersect(array_keys($orig_headers), $client->follow_headers());
$follow_headers = \Arr::extract($orig_headers, $follow_header_keys);

$follow_request = Request::factory($response->headers('Location'))
->set_routes($orig_routes)
->method($follow_method)
->headers($follow_headers);

Expand Down
138 changes: 7 additions & 131 deletions classes/Kohana/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,136 +70,14 @@ class Kohana_Route {
public static $cache = FALSE;

/**
* @var array
*/
protected static $_routes = array();

/**
* Stores a named route and returns it. The "action" will always be set to
* "index" if it is not defined.
*
* Route::set('default', '(<controller>(/<action>(/<id>)))')
* ->defaults(array(
* 'controller' => 'welcome',
* ));
*
* @param string $name route name
* @param string $uri URI pattern
* @param array $regex regex patterns for route keys
* @return Route
* A shim to make the tests run
*/
public static function set($name, $uri = NULL, $regex = NULL)
{
return Route::$_routes[$name] = new Route($uri, $regex);
}

/**
* Retrieves a named route.
*
* $route = Route::get('default');
*
* @param string $name route name
* @return Route
* @throws Kohana_Exception
*/
public static function get($name)
{
if ( ! isset(Route::$_routes[$name]))
{
throw new Kohana_Exception('The requested route does not exist: :route',
array(':route' => $name));
}

return Route::$_routes[$name];
}

/**
* Retrieves all named routes.
*
* $routes = Route::all();
*
* @return array routes by name
*/
public static function all()
{
return Route::$_routes;
return new Route($uri, $regex);
}

/**
* Get the name of a route.
*
* $name = Route::name($route)
*
* @param Route $route instance
* @return string
*/
public static function name(Route $route)
{
return array_search($route, Route::$_routes);
}

/**
* Saves or loads the route cache. If your routes will remain the same for
* a long period of time, use this to reload the routes from the cache
* rather than redefining them on every page load.
*
* if ( ! Route::cache())
* {
* // Set routes here
* Route::cache(TRUE);
* }
*
* @param boolean $save cache the current routes
* @param boolean $append append, rather than replace, cached routes when loading
* @return void when saving routes
* @return boolean when loading routes
* @uses Kohana::cache
*/
public static function cache($save = FALSE, $append = FALSE)
{
if ($save === TRUE)
{
try
{
// Cache all defined routes
Kohana::cache('Route::cache()', Route::$_routes);
}
catch (Exception $e)
{
// We most likely have a lambda in a route, which cannot be cached
throw new Kohana_Exception('One or more routes could not be cached (:message)', array(
':message' => $e->getMessage(),
), 0, $e);
}
}
else
{
if ($routes = Kohana::cache('Route::cache()'))
{
if ($append)
{
// Append cached routes
Route::$_routes += $routes;
}
else
{
// Replace existing routes
Route::$_routes = $routes;
}

// Routes were cached
return Route::$cache = TRUE;
}
else
{
// Routes were not cached
return Route::$cache = FALSE;
}
}
}

/**
* Create a URL from a route name. This is a shortcut for:
/** * Create a URL from a route name. This is a shortcut for:
*
* echo URL::site(Route::get($name)->uri($params), $protocol);
*
Expand All @@ -210,15 +88,13 @@ public static function cache($save = FALSE, $append = FALSE)
* @since 3.0.7
* @uses URL::site
*/
public static function url($name, array $params = NULL, $protocol = NULL)
public function url(array $params = NULL, $protocol = NULL)
{
$route = Route::get($name);

// Create a URI with the route and convert it to a URL
if ($route->is_external())
return $route->uri($params);
if ($this->is_external())
return $this->uri($params);
else
return URL::site($route->uri($params), $protocol);
return URL::site($this->uri($params), $protocol);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/kohana/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public function test_method()
*/
public function test_route()
{
$request = Request::factory(''); // This should always match something, no matter what changes people make
$request = Request::factory('', [], TRUE, [new Route('')]); // This should always match something, no matter what changes people make

// We need to execute the request before it has matched a route
try
Expand Down
Loading