Skip to content
This repository
Browse code

Load routes as late as possible.

As a concequence - routes will also work by default in the cli
  • Loading branch information...
commit 5140baf83d65e0ea137d43ced4380c63794da424 1 parent 57f81da
Andy Dawson authored
15  lib/Cake/Routing/Dispatcher.php
@@ -209,12 +209,6 @@ protected function _invoke(Controller $controller, CakeRequest $request, CakeRes
209 209
 	public function parseParams($event) {
210 210
 		$request = $event->data['request'];
211 211
 		Router::setRequestInfo($request);
212  
-		if (count(Router::$routes) == 0) {
213  
-			$namedExpressions = Router::getNamedExpressions();
214  
-			extract($namedExpressions);
215  
-			$this->_loadRoutes();
216  
-		}
217  
-
218 212
 		$params = Router::parse($request->url);
219 213
 		$request->addParams($params);
220 214
 
@@ -269,13 +263,4 @@ protected function _loadController($request) {
269 263
 		return false;
270 264
 	}
271 265
 
272  
-/**
273  
- * Loads route configuration
274  
- *
275  
- * @return void
276  
- */
277  
-	protected function _loadRoutes() {
278  
-		include APP . 'Config' . DS . 'routes.php';
279  
-	}
280  
-
281 266
 }
31  lib/Cake/Routing/Router.php
@@ -49,6 +49,13 @@ class Router {
49 49
 	public static $routes = array();
50 50
 
51 51
 /**
  52
+ * Have routes been loaded
  53
+ *
  54
+ * @var boolean
  55
+ */
  56
+	public static $initialized = false;
  57
+
  58
+/**
52 59
  * List of action prefixes used in connected routes.
53 60
  * Includes admin prefix
54 61
  *
@@ -284,6 +291,8 @@ public static function resourceMap($resourceMap = null) {
284 291
  * @throws RouterException
285 292
  */
286 293
 	public static function connect($route, $defaults = array(), $options = array()) {
  294
+		self::$initialized = true;
  295
+
287 296
 		foreach (self::$_prefixes as $prefix) {
288 297
 			if (isset($defaults[$prefix])) {
289 298
 				if ($defaults[$prefix]) {
@@ -520,6 +529,10 @@ public static function prefixes() {
520 529
  * @return array Parsed elements from URL
521 530
  */
522 531
 	public static function parse($url) {
  532
+		if (!self::$initialized) {
  533
+			self::_loadRoutes();
  534
+		}
  535
+
523 536
 		$ext = null;
524 537
 		$out = array();
525 538
 
@@ -748,6 +761,10 @@ public static function promote($which = null) {
748 761
  * @return string Full translated URL with base path.
749 762
  */
750 763
 	public static function url($url = null, $full = false) {
  764
+		if (!self::$initialized) {
  765
+			self::_loadRoutes();
  766
+		}
  767
+
751 768
 		$params = array('plugin' => null, 'controller' => null, 'action' => 'index');
752 769
 
753 770
 		if (is_bool($full)) {
@@ -1117,6 +1134,10 @@ public static function parseExtensions() {
1117 1134
  * @return array Array of extensions Router is configured to parse.
1118 1135
  */
1119 1136
 	public static function extensions() {
  1137
+		if (!self::$initialized) {
  1138
+			self::_loadRoutes();
  1139
+		}
  1140
+
1120 1141
 		return self::$_validExtensions;
1121 1142
 	}
1122 1143
 
@@ -1138,6 +1159,16 @@ public static function setExtensions($extensions, $merge = true) {
1138 1159
 		return self::$_validExtensions = array_merge(self::$_validExtensions, $extensions);
1139 1160
 	}
1140 1161
 
  1162
+/**
  1163
+ * Loads route configuration
  1164
+ *
  1165
+ * @return void
  1166
+ */
  1167
+	protected static function _loadRoutes() {
  1168
+		self::$initialized = true;
  1169
+		include APP . 'Config' . DS . 'routes.php';
  1170
+	}
  1171
+
1141 1172
 }
1142 1173
 
1143 1174
 //Save the initial state
4  lib/Cake/Test/Case/Routing/RouterTest.php
@@ -2339,9 +2339,13 @@ public function testCustomRouteException() {
2339 2339
 /**
2340 2340
  * test reversing parameter arrays back into strings.
2341 2341
  *
  2342
+ * Mark the router as initialized so it doesn't auto-load routes
  2343
+ *
2342 2344
  * @return void
2343 2345
  */
2344 2346
 	public function testRouterReverse() {
  2347
+		Router::$initialized = true;
  2348
+
2345 2349
 		$params = array(
2346 2350
 			'controller' => 'posts',
2347 2351
 			'action' => 'view',

0 notes on commit 5140baf

Please sign in to comment.
Something went wrong with that request. Please try again.