Skip to content

Commit

Permalink
Load routes as late as possible.
Browse files Browse the repository at this point in the history
As a concequence - routes will also work by default in the cli
  • Loading branch information
AD7six committed Oct 4, 2012
1 parent 57f81da commit 5140baf
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
15 changes: 0 additions & 15 deletions lib/Cake/Routing/Dispatcher.php
Expand Up @@ -209,12 +209,6 @@ protected function _invoke(Controller $controller, CakeRequest $request, CakeRes
public function parseParams($event) { public function parseParams($event) {
$request = $event->data['request']; $request = $event->data['request'];
Router::setRequestInfo($request); Router::setRequestInfo($request);
if (count(Router::$routes) == 0) {
$namedExpressions = Router::getNamedExpressions();
extract($namedExpressions);
$this->_loadRoutes();
}

$params = Router::parse($request->url); $params = Router::parse($request->url);
$request->addParams($params); $request->addParams($params);


Expand Down Expand Up @@ -269,13 +263,4 @@ protected function _loadController($request) {
return false; return false;
} }


/**
* Loads route configuration
*
* @return void
*/
protected function _loadRoutes() {
include APP . 'Config' . DS . 'routes.php';
}

} }
31 changes: 31 additions & 0 deletions lib/Cake/Routing/Router.php
Expand Up @@ -48,6 +48,13 @@ class Router {
*/ */
public static $routes = array(); public static $routes = array();


/**
* Have routes been loaded
*
* @var boolean
*/
public static $initialized = false;

/** /**
* List of action prefixes used in connected routes. * List of action prefixes used in connected routes.
* Includes admin prefix * Includes admin prefix
Expand Down Expand Up @@ -284,6 +291,8 @@ public static function resourceMap($resourceMap = null) {
* @throws RouterException * @throws RouterException
*/ */
public static function connect($route, $defaults = array(), $options = array()) { public static function connect($route, $defaults = array(), $options = array()) {
self::$initialized = true;

foreach (self::$_prefixes as $prefix) { foreach (self::$_prefixes as $prefix) {
if (isset($defaults[$prefix])) { if (isset($defaults[$prefix])) {
if ($defaults[$prefix]) { if ($defaults[$prefix]) {
Expand Down Expand Up @@ -520,6 +529,10 @@ public static function prefixes() {
* @return array Parsed elements from URL * @return array Parsed elements from URL
*/ */
public static function parse($url) { public static function parse($url) {
if (!self::$initialized) {
self::_loadRoutes();
}

$ext = null; $ext = null;
$out = array(); $out = array();


Expand Down Expand Up @@ -748,6 +761,10 @@ public static function promote($which = null) {
* @return string Full translated URL with base path. * @return string Full translated URL with base path.
*/ */
public static function url($url = null, $full = false) { public static function url($url = null, $full = false) {
if (!self::$initialized) {
self::_loadRoutes();
}

$params = array('plugin' => null, 'controller' => null, 'action' => 'index'); $params = array('plugin' => null, 'controller' => null, 'action' => 'index');


if (is_bool($full)) { if (is_bool($full)) {
Expand Down Expand Up @@ -1117,6 +1134,10 @@ public static function parseExtensions() {
* @return array Array of extensions Router is configured to parse. * @return array Array of extensions Router is configured to parse.
*/ */
public static function extensions() { public static function extensions() {
if (!self::$initialized) {
self::_loadRoutes();
}

return self::$_validExtensions; return self::$_validExtensions;
} }


Expand All @@ -1138,6 +1159,16 @@ public static function setExtensions($extensions, $merge = true) {
return self::$_validExtensions = array_merge(self::$_validExtensions, $extensions); return self::$_validExtensions = array_merge(self::$_validExtensions, $extensions);
} }


/**
* Loads route configuration
*
* @return void
*/
protected static function _loadRoutes() {
self::$initialized = true;
include APP . 'Config' . DS . 'routes.php';
}

} }


//Save the initial state //Save the initial state
Expand Down
4 changes: 4 additions & 0 deletions lib/Cake/Test/Case/Routing/RouterTest.php
Expand Up @@ -2339,9 +2339,13 @@ public function testCustomRouteException() {
/** /**
* test reversing parameter arrays back into strings. * test reversing parameter arrays back into strings.
* *
* Mark the router as initialized so it doesn't auto-load routes
*
* @return void * @return void
*/ */
public function testRouterReverse() { public function testRouterReverse() {
Router::$initialized = true;

$params = array( $params = array(
'controller' => 'posts', 'controller' => 'posts',
'action' => 'view', 'action' => 'view',
Expand Down

0 comments on commit 5140baf

Please sign in to comment.