Skip to content

Commit

Permalink
Re-factor request context.
Browse files Browse the repository at this point in the history
Move it into RouteCollection.  It makes for one
less parameter to shuffle around.
  • Loading branch information
markstory committed Jul 4, 2012
1 parent 6406e52 commit 9519ac9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 49 deletions.
43 changes: 37 additions & 6 deletions lib/Cake/Routing/RouteCollection.php
Expand Up @@ -2,6 +2,7 @@
namespace Cake\Routing;

use Cake\Routing\Route\Route;
use Cake\Network\Request;

class RouteCollection implements \Countable {

Expand All @@ -21,6 +22,19 @@ class RouteCollection implements \Countable {
*/
protected $_routes = array();

/**
* The top most request's context. Updated whenever
* requests are pushed/popped off the stack in Router.
*
* @var array
*/
protected $_requestContext = array(
'_base' => '',
'_port' => 80,
'_scheme' => 'http',
'_host' => 'localhost',
);

/**
* Add a route to the collection.
*
Expand All @@ -42,21 +56,21 @@ public function add(Route $route) {
* Returns either the string URL generate by the route, or false on failure.
*
* @param array $url The url to match.
* @param array $requestContext The current request parameters, used for persistent parameters.
* @param array $currentParams The current request parameters, used for persistent parameters.
* @return void
* @TODO Remove persistent params? Are they even useful?
*/
public function match($url, $currentParams = array(), $requestContext = array()) {
public function match($url, $currentParams = array()) {
$names = $this->_getNames($url);
foreach ($names as $name) {
if (isset($this->_routeTable[$name])) {
$output = $this->_matchRoutes($this->_routeTable[$name], $url, $currentParams, $requestContext);
$output = $this->_matchRoutes($this->_routeTable[$name], $url, $currentParams);
if ($output) {
return $output;
}
}
}
return $this->_matchRoutes($this->_routes, $url, $currentParams, $requestContext);
return $this->_matchRoutes($this->_routes, $url, $currentParams);
}

/**
Expand All @@ -67,7 +81,7 @@ public function match($url, $currentParams = array(), $requestContext = array())
* @param array $requestContext The current request parameters, used for persistent parameters.
* @return mixed Either false on failure, or a string on success.
*/
protected function _matchRoutes($routes, $url, $currentParams, $requestContext) {
protected function _matchRoutes($routes, $url, $currentParams) {
$output = false;
for ($i = 0, $len = count($routes); $i < $len; $i++) {
$originalUrl = $url;
Expand All @@ -77,7 +91,7 @@ protected function _matchRoutes($routes, $url, $currentParams, $requestContext)
$url = $route->persistParams($url, $currentParams);
}

if ($match = $route->match($url, $requestContext)) {
if ($match = $route->match($url, $this->_requestContext)) {
$output = trim($match, '/');
break;
}
Expand Down Expand Up @@ -183,4 +197,21 @@ public function get($index) {
public function count() {
return count($this->_routes);
}

/**
* Populate the request context used to generate URL's
* Generally set to the last/most recent request.
*
* @param Cake\Network\Request $request
* @return void
*/
public function setContext(Request $request) {
$this->_requestContext = array(
'_base' => $request->base,
'_port' => $request->port(),
'_scheme' => $request->scheme(),
'_host' => $request->host()
);
}

}
46 changes: 3 additions & 43 deletions lib/Cake/Routing/Router.php
Expand Up @@ -133,19 +133,6 @@ class Router {
*/
protected static $_requests = array();

/**
* The top most request's context. Updated whenever
* requests are pushed/popped off the stack.
*
* @var array
*/
protected static $_requestContext = array(
'_base' => '',
'_port' => 80,
'_scheme' => 'http',
'_host' => 'localhost',
);

/**
* Initial state is populated the first time reload() is called which is at the bottom
* of this file. This is a cheat as get_class_vars() returns the value of static vars even if they
Expand Down Expand Up @@ -520,23 +507,7 @@ public static function setRequestInfo($request) {
*/
public static function pushRequest(Request $request) {
self::$_requests[] = $request;
self::_setRequestContext($request);
}

/**
* Populate the request context used to generate URL's
* Generally set to the last/most recent request.
*
* @param Cake\Network\Request $request
* @return void
*/
protected static function _setRequestcontext(Request $request) {
self::$_requestContext = array(
'_base' => $request->base,
'_port' => $request->port(),
'_scheme' => $request->scheme(),
'_host' => $request->host()
);
self::$_routes->setContext($request);
}

/**
Expand All @@ -549,21 +520,11 @@ protected static function _setRequestcontext(Request $request) {
public static function popRequest() {
$removed = array_pop(static::$_requests);
$last = end(static::$_requests);
static::_setRequestContext($last);
static::$_routes->setContext($last);
reset(static::$_requests);
return $removed;
}

/**
* Fetch the current request context.
*
* @return array An array with the current request context.
*/
public function getRequestContext() {
return self::$_requestContext;
>>>>>>> Start refactoring requestContext.
}

/**
* Get the either the current request object, or the first one.
*
Expand Down Expand Up @@ -715,8 +676,7 @@ public static function url($url = null, $full = false) {
'controller' => $params['controller'],
'plugin' => $params['plugin']
);
$requestContext = self::$_requestContext;
$output = self::$_routes->match($url, $params, $requestContext);
$output = self::$_routes->match($url, $params);
} else {
// String urls.
if (
Expand Down

0 comments on commit 9519ac9

Please sign in to comment.