Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This class will play an important role in speeding up reverse routing.
- Loading branch information
Showing
3 changed files
with
120 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
namespace Cake\Routing; | ||
|
||
use Cake\Routing\Route\Route; | ||
|
||
class RouteCollection { | ||
|
||
protected $_routes = array(); | ||
|
||
protected $_routeTable = array(); | ||
|
||
public function add(Route $route) { | ||
$this->_routes[] = $route; | ||
} | ||
|
||
public function match($url, $params = null) { | ||
$output = false; | ||
for ($i = 0, $len = count($this->_routes); $i < $len; $i++) { | ||
$originalUrl = $url; | ||
$route =& $this->_routes[$i]; | ||
|
||
if (isset($route->options['persist'], $params)) { | ||
$url = $route->persistParams($url, $params); | ||
} | ||
|
||
if ($match = $route->match($url)) { | ||
$output = trim($match, '/'); | ||
break; | ||
} | ||
$url = $originalUrl; | ||
} | ||
return $output; | ||
} | ||
|
||
public function parse($url) { | ||
$out = array(); | ||
for ($i = 0, $len = count($this->_routes); $i < $len; $i++) { | ||
$route = $this->_routes[$i]; | ||
|
||
if (($r = $route->parse($url)) !== false) { | ||
$out = $r; | ||
break; | ||
} | ||
} | ||
return $out; | ||
} | ||
|
||
/** | ||
* Promote a route (by default, the last one added) to the beginning of the list. | ||
* Does not modify route ordering stored in the hashtable lookups. | ||
* | ||
* @param integer $which A zero-based array index representing | ||
* the route to move. For example, | ||
* if 3 routes have been added, the last route would be 2. | ||
* @return boolean Returns false if no route exists at the position | ||
* specified by $which. | ||
*/ | ||
public function promote($which) { | ||
if ($which === null) { | ||
$which = count($this->_routes) - 1; | ||
} | ||
if (!isset($this->_routes[$which])) { | ||
return false; | ||
} | ||
$route =& $this->_routes[$which]; | ||
unset($this->_routes[$which]); | ||
array_unshift($this->_routes, $route); | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ | |
use Cake\Network\Request; | ||
use Cake\Network\Response; | ||
use Cake\Routing\Route\Route; | ||
use Cake\Routing\RouteCollection; | ||
use Cake\Utility\Hash; | ||
use Cake\Utility\Inflector; | ||
|
||
|
@@ -43,11 +44,11 @@ | |
class Router { | ||
|
||
/** | ||
* Array of routes connected with Router::connect() | ||
* RouteCollection object containing all the connected routes. | ||
* | ||
* @var array | ||
* @var Cake\Routing\RouteCollection | ||
*/ | ||
public static $routes = array(); | ||
public static $_routes; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
markstory
Author
Member
|
||
|
||
/** | ||
* List of action prefixes used in connected routes. | ||
|
@@ -263,7 +264,7 @@ public static function resourceMap($resourceMap = null) { | |
* shifted into the passed arguments, supplying patterns for routing parameters and supplying the name of a | ||
* custom routing class. | ||
* @see routes | ||
* @return array Array of routes | ||
* @return void | ||
* @throws RouterException | ||
*/ | ||
public static function connect($route, $defaults = array(), $options = array()) { | ||
|
@@ -293,8 +294,7 @@ public static function connect($route, $defaults = array(), $options = array()) | |
if ($routeClass === 'Cake\Routing\Route\RedirectRoute' && isset($defaults['redirect'])) { | ||
$defaults = $defaults['redirect']; | ||
} | ||
static::$routes[] = new $routeClass($route, $defaults, $options); | ||
return static::$routes; | ||
static::$_routes->add(new $routeClass($route, $defaults, $options)); | ||
} | ||
|
||
/** | ||
|
@@ -415,15 +415,8 @@ public static function parse($url) { | |
|
||
extract(static::_parseExtension($url)); | ||
|
||
for ($i = 0, $len = count(static::$routes); $i < $len; $i++) { | ||
$route =& static::$routes[$i]; | ||
$out = static::$_routes->parse($url); | ||
|
||
if (($r = $route->parse($url)) !== false) { | ||
static::$_currentRoute[] =& $route; | ||
$out = $r; | ||
break; | ||
} | ||
} | ||
if (isset($out['prefix'])) { | ||
$out['action'] = $out['prefix'] . '_' . $out['action']; | ||
} | ||
|
@@ -489,6 +482,16 @@ public static function setRequestInfo($request) { | |
} | ||
} | ||
|
||
/** | ||
* Set the route collection object Router should use. | ||
* | ||
* @param Cake\Routing\RouteCollection $routes | ||
* @return void | ||
*/ | ||
public static function setRouteCollection(RouteCollection $routes) { | ||
self::$_routes = $routes; | ||
} | ||
|
||
/** | ||
* Pops a request off of the request stack. Used when doing requestAction | ||
* | ||
|
@@ -579,6 +582,7 @@ public static function reload() { | |
} | ||
} | ||
static::_setPrefixes(); | ||
static::$_routes = new RouteCollection(); | ||
} | ||
|
||
/** | ||
|
@@ -589,16 +593,7 @@ public static function reload() { | |
* @return boolean Returns false if no route exists at the position specified by $which. | ||
*/ | ||
public static function promote($which = null) { | ||
if ($which === null) { | ||
$which = count(static::$routes) - 1; | ||
} | ||
if (!isset(static::$routes[$which])) { | ||
return false; | ||
} | ||
$route =& static::$routes[$which]; | ||
unset(static::$routes[$which]); | ||
array_unshift(static::$routes, $route); | ||
return true; | ||
return static::$_routes->promote($which); | ||
} | ||
|
||
/** | ||
|
@@ -698,22 +693,9 @@ public static function url($url = null, $full = false) { | |
|
||
$url += array('controller' => $params['controller'], 'plugin' => $params['plugin']); | ||
|
||
$match = false; | ||
|
||
for ($i = 0, $len = count(static::$routes); $i < $len; $i++) { | ||
$originalUrl = $url; | ||
$output = self::$_routes->match($url, $params); | ||
|
||
if (isset(static::$routes[$i]->options['persist'], $params)) { | ||
$url = static::$routes[$i]->persistParams($url, $params); | ||
} | ||
|
||
if ($match = static::$routes[$i]->match($url)) { | ||
$output = trim($match, '/'); | ||
break; | ||
} | ||
$url = $originalUrl; | ||
} | ||
if ($match === false) { | ||
if ($output === false) { | ||
$output = static::_handleNoRoute($url); | ||
} | ||
} else { | ||
|
@@ -923,6 +905,7 @@ public static function normalize($url = '/') { | |
* Returns the route matching the current request URL. | ||
* | ||
* @return Cake\Routing\Route\Route Matching route object. | ||
* @todo Remove? Not really that useful. | ||
*/ | ||
public static function &requestRoute() { | ||
return static::$_currentRoute[0]; | ||
|
@@ -932,6 +915,7 @@ public static function &requestRoute() { | |
* Returns the route matching the current request (useful for requestAction traces) | ||
* | ||
* @return Cake\Routing\Route\Route Matching route object. | ||
* @todo Remove? Not really that useful. | ||
*/ | ||
public static function ¤tRoute() { | ||
return static::$_currentRoute[count(static::$_currentRoute) - 1]; | ||
|
@@ -943,6 +927,7 @@ public static function ¤tRoute() { | |
* @param string $base Base URL | ||
* @param string $plugin Plugin name | ||
* @return string base url with plugin name removed if present | ||
* @todo Remove? Not really that useful. | ||
*/ | ||
public static function stripPlugin($base, $plugin = null) { | ||
if ($plugin != null) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
underscored public attribute
Should this be changed to a protected attribute?