From 5140baf83d65e0ea137d43ced4380c63794da424 Mon Sep 17 00:00:00 2001 From: AD7six Date: Tue, 3 Jul 2012 19:32:41 +0200 Subject: [PATCH] Load routes as late as possible. As a concequence - routes will also work by default in the cli --- lib/Cake/Routing/Dispatcher.php | 15 ----------- lib/Cake/Routing/Router.php | 31 +++++++++++++++++++++++ lib/Cake/Test/Case/Routing/RouterTest.php | 4 +++ 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/lib/Cake/Routing/Dispatcher.php b/lib/Cake/Routing/Dispatcher.php index b10fe4ebb..3f891f32b 100644 --- a/lib/Cake/Routing/Dispatcher.php +++ b/lib/Cake/Routing/Dispatcher.php @@ -209,12 +209,6 @@ class Dispatcher implements CakeEventListener { public function parseParams($event) { $request = $event->data['request']; Router::setRequestInfo($request); - if (count(Router::$routes) == 0) { - $namedExpressions = Router::getNamedExpressions(); - extract($namedExpressions); - $this->_loadRoutes(); - } - $params = Router::parse($request->url); $request->addParams($params); @@ -269,13 +263,4 @@ class Dispatcher implements CakeEventListener { return false; } -/** - * Loads route configuration - * - * @return void - */ - protected function _loadRoutes() { - include APP . 'Config' . DS . 'routes.php'; - } - } diff --git a/lib/Cake/Routing/Router.php b/lib/Cake/Routing/Router.php index c74235fb3..2528db7ea 100644 --- a/lib/Cake/Routing/Router.php +++ b/lib/Cake/Routing/Router.php @@ -48,6 +48,13 @@ class Router { */ public static $routes = array(); +/** + * Have routes been loaded + * + * @var boolean + */ + public static $initialized = false; + /** * List of action prefixes used in connected routes. * Includes admin prefix @@ -284,6 +291,8 @@ class Router { * @throws RouterException */ public static function connect($route, $defaults = array(), $options = array()) { + self::$initialized = true; + foreach (self::$_prefixes as $prefix) { if (isset($defaults[$prefix])) { if ($defaults[$prefix]) { @@ -520,6 +529,10 @@ class Router { * @return array Parsed elements from URL */ public static function parse($url) { + if (!self::$initialized) { + self::_loadRoutes(); + } + $ext = null; $out = array(); @@ -748,6 +761,10 @@ class Router { * @return string Full translated URL with base path. */ public static function url($url = null, $full = false) { + if (!self::$initialized) { + self::_loadRoutes(); + } + $params = array('plugin' => null, 'controller' => null, 'action' => 'index'); if (is_bool($full)) { @@ -1117,6 +1134,10 @@ class Router { * @return array Array of extensions Router is configured to parse. */ public static function extensions() { + if (!self::$initialized) { + self::_loadRoutes(); + } + return self::$_validExtensions; } @@ -1138,6 +1159,16 @@ class Router { 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 diff --git a/lib/Cake/Test/Case/Routing/RouterTest.php b/lib/Cake/Test/Case/Routing/RouterTest.php index c13fb2576..c42285f45 100644 --- a/lib/Cake/Test/Case/Routing/RouterTest.php +++ b/lib/Cake/Test/Case/Routing/RouterTest.php @@ -2339,9 +2339,13 @@ class RouterTest extends CakeTestCase { /** * test reversing parameter arrays back into strings. * + * Mark the router as initialized so it doesn't auto-load routes + * * @return void */ public function testRouterReverse() { + Router::$initialized = true; + $params = array( 'controller' => 'posts', 'action' => 'view',