From 5b3800b088a648eac43a62f988f05324f39daf97 Mon Sep 17 00:00:00 2001 From: Chuck Hagenbuch Date: Tue, 27 Oct 2009 23:26:41 -0400 Subject: [PATCH] When urlFor() is passed a named route, pass it to generate() Includes some other changes to make this work with formatted routes, and some updates to the tests to reflect updated assumptions. --- framework/Routes/lib/Horde/Routes/Mapper.php | 31 +++++++---- framework/Routes/lib/Horde/Routes/Route.php | 10 ++-- framework/Routes/lib/Horde/Routes/Utils.php | 8 ++- framework/Routes/test/Horde/Routes/UtilTest.php | 20 +++---- .../test/Horde/Routes/UtilWithExplicitTest.php | 64 +++++++++++++--------- 5 files changed, 78 insertions(+), 55 deletions(-) diff --git a/framework/Routes/lib/Horde/Routes/Mapper.php b/framework/Routes/lib/Horde/Routes/Mapper.php index 8a0ca66c8..be6742e9b 100644 --- a/framework/Routes/lib/Horde/Routes/Mapper.php +++ b/framework/Routes/lib/Horde/Routes/Mapper.php @@ -552,11 +552,20 @@ class Horde_Routes_Mapper * Usage: * $m->generate(array('controller' => 'content', 'action' => 'view', 'id' => 10)); * - * @param array $kargs Keyword arguments (key/value pairs) - * @return null|string URL text or null + * @param array $routeArgs Optional explicit route list + * @param array $kargs Keyword arguments (key/value pairs) + * @return null|string URL text or null */ - public function generate($kargs = array()) + public function generate($first = null, $second = null) { + if ($second) { + $routeArgs = $first; + $kargs = is_null($second) ? array() : $second; + } else { + $routeArgs = array(); + $kargs = is_null($first) ? array() : $first; + } + // Generate ourself if we haven't already if (!$this->_createdGens) { $this->_createGens(); @@ -596,13 +605,15 @@ class Horde_Routes_Mapper } } - $actionList = isset($this->_gendict[$controller]) ? $this->_gendict[$controller] : $this->_gendict['*']; - - list($keyList, $sortCache) = - (isset($actionList[$action])) ? $actionList[$action] : ((isset($actionList['*'])) ? $actionList['*'] : array(null, null)); - - if ($keyList === null) { - return null; + if ($routeArgs) { + $keyList = $routeArgs; + } else { + $actionList = isset($this->_gendict[$controller]) ? $this->_gendict[$controller] : $this->_gendict['*']; + list($keyList, $sortCache) = + (isset($actionList[$action])) ? $actionList[$action] : ((isset($actionList['*'])) ? $actionList['*'] : array(null, null)); + if ($keyList === null) { + return null; + } } $keys = array_keys($kargs); diff --git a/framework/Routes/lib/Horde/Routes/Route.php b/framework/Routes/lib/Horde/Routes/Route.php index 23d3eeb03..676d6d254 100644 --- a/framework/Routes/lib/Horde/Routes/Route.php +++ b/framework/Routes/lib/Horde/Routes/Route.php @@ -214,7 +214,7 @@ class Horde_Routes_Route // This is a list of characters natural splitters in a URL $this->_splitChars = array('/', ',', ';', '.', '#'); - // trip preceding '/' if present + // trim preceding '/' if present if (substr($this->routePath, 0, 1) == '/') { $routePath = substr($this->routePath, 1); } @@ -689,7 +689,7 @@ class Horde_Routes_Route * Generate a URL from ourself given a set of keyword arguments * * @param array $kargs Keyword arguments - * @param boolean|string False if generation failed, URL otherwise + * @param null|string Null if generation failed, URL otherwise */ public function generate($kargs) { @@ -709,7 +709,7 @@ class Horde_Routes_Route $value = (isset($kargs[$key])) ? $kargs[$key] : null; if (!empty($value) && !preg_match($this->_reqRegs[$key], $value)) { - return false; + return null; } } } @@ -722,7 +722,7 @@ class Horde_Routes_Route if ($this->conditions && isset($this->conditions['method']) && (!in_array(strtoupper($meth), $this->conditions['method']))) { - return false; + return null; } unset($kargs['method']); } @@ -764,7 +764,7 @@ class Horde_Routes_Route // No arg at all? This won't work } else { - return false; + return null; } $urlList[] = Horde_Routes_Utils::urlQuote($val, $this->encoding); diff --git a/framework/Routes/lib/Horde/Routes/Utils.php b/framework/Routes/lib/Horde/Routes/Utils.php index dbc2e4e1f..af9630833 100644 --- a/framework/Routes/lib/Horde/Routes/Utils.php +++ b/framework/Routes/lib/Horde/Routes/Utils.php @@ -124,6 +124,7 @@ class Horde_Routes_Utils } $route = null; + $routeArgs = array(); $static = false; $encoding = $this->mapper->encoding; $environ = $this->mapper->environ; @@ -131,7 +132,9 @@ class Horde_Routes_Utils if (isset($routeName)) { - if (isset($this->mapper->routeNames[$routeName])) { + if (isset($kargs['format']) && isset($this->mapper->routeNames['formatted_' . $routeName])) { + $route = $this->mapper->routeNames['formatted_' . $routeName]; + } elseif (isset($this->mapper->routeNames[$routeName])) { $route = $this->mapper->routeNames[$routeName]; } @@ -166,6 +169,7 @@ class Horde_Routes_Utils if (! $static) { if ($route) { + $routeArgs = array($route); $newargs = $route->defaults; foreach ($kargs as $key => $value) { $newargs[$key] = $value; @@ -190,7 +194,7 @@ class Horde_Routes_Utils $protocol = (isset($newargs['_protocol'])) ? $newargs['_protocol'] : $protocol; unset($newargs['_protocol']); - $url = $this->mapper->generate($newargs); + $url = $this->mapper->generate($routeArgs, $newargs); } if (!empty($anchor)) { diff --git a/framework/Routes/test/Horde/Routes/UtilTest.php b/framework/Routes/test/Horde/Routes/UtilTest.php index 7dad84600..5e5f85d19 100644 --- a/framework/Routes/test/Horde/Routes/UtilTest.php +++ b/framework/Routes/test/Horde/Routes/UtilTest.php @@ -131,10 +131,8 @@ class UtilTest extends PHPUnit_Framework_TestCase $utils->urlFor('category_home')); $this->assertEquals('/category/food', $utils->urlFor('category_home', array('section' => 'food'))); - $this->assertEquals('/category', - $utils->urlFor('home', array('action' => 'view', 'section' => 'home'))); - $this->assertEquals('/content/splash', - $utils->urlFor('home', array('controller' => 'content'))); + $this->assertNull($utils->urlFor('home', array('action' => 'view', 'section' => 'home'))); + $this->assertNull($utils->urlFor('home', array('controller' => 'content'))); $this->assertEquals('/', $utils->urlFor('/')); } @@ -142,7 +140,7 @@ class UtilTest extends PHPUnit_Framework_TestCase public function testWithRouteNamesAndDefaults() { $this->markTestSkipped(); - + $m = $this->mapper; $utils = $m->utils; @@ -305,7 +303,7 @@ class UtilTest extends PHPUnit_Framework_TestCase { $m = $this->mapper; $m->environ = array('SCRIPT_NAME' => '/webapp', 'HTTP_HOST' => 'example.com'); - + $utils = $m->utils; $utils->mapperDict = array(); @@ -384,8 +382,8 @@ class UtilTest extends PHPUnit_Framework_TestCase public function testWithSslEnviron() { $m = new Horde_Routes_Mapper(); - $m->environ = array('SCRIPT_NAME' => '', 'HTTPS' => 'on', 'SERVER_PORT' => '443', - 'PATH_INFO' => '/', 'HTTP_HOST' => 'example.com', + $m->environ = array('SCRIPT_NAME' => '', 'HTTPS' => 'on', 'SERVER_PORT' => '443', + 'PATH_INFO' => '/', 'HTTP_HOST' => 'example.com', 'SERVER_NAME' => 'example.com'); $utils = $m->utils; @@ -446,7 +444,7 @@ class UtilTest extends PHPUnit_Framework_TestCase $utils = $m->utils; $utils->mapperDict = array(); - + $m->subDomains = true; $m->connect(':controller/:action/:id'); $m->createRegs(array('content', 'archives', 'admin/comments')); @@ -571,7 +569,7 @@ class UtilTest extends PHPUnit_Framework_TestCase $controllerDir = "$hereDir/fixtures/controllers"; $controllers = Horde_Routes_Utils::controllerScan($controllerDir); - + $this->assertEquals(3, count($controllers)); $this->assertEquals('admin/users', $controllers[0]); $this->assertEquals('content', $controllers[1]); @@ -585,7 +583,7 @@ class UtilTest extends PHPUnit_Framework_TestCase $m = new Horde_Routes_Mapper(array('directory' => $controllerDir)); $m->alwaysScan = true; - + $m->connect(':controller/:action/:id'); $expected = array('action' => 'index', 'controller' => 'content', 'id' => null); diff --git a/framework/Routes/test/Horde/Routes/UtilWithExplicitTest.php b/framework/Routes/test/Horde/Routes/UtilWithExplicitTest.php index 161dfb3ab..4dfc4afd6 100644 --- a/framework/Routes/test/Horde/Routes/UtilWithExplicitTest.php +++ b/framework/Routes/test/Horde/Routes/UtilWithExplicitTest.php @@ -39,7 +39,7 @@ class UtilWithExplicitTest extends PHPUnit_Framework_TestCase { { $utils = $this->utils; $utils->mapperDict = array(); - + $this->assertNull($utils->urlFor(array('controller' => 'blog'))); $this->assertNull($utils->urlFor()); $this->assertEquals('/blog/view/3', @@ -51,20 +51,34 @@ class UtilWithExplicitTest extends PHPUnit_Framework_TestCase { $this->assertEquals('http://www.test.org/content/view/2', $utils->urlFor(array('host' => 'www.test.org', 'controller' => 'content', 'action' => 'view', 'id' => 2))); + + $m = $this->mapper; + + $utils = $m->utils; + $utils->mapperDict = array(); + + $m->connect('home', '', array('controller' => 'blog', 'action' => 'splash')); + $m->connect('category_home', 'category/:section', + array('controller' => 'blog', 'action' => 'view', 'section' => 'home')); + $m->createRegs(array('content', 'blog', 'admin/comments')); + + $this->assertEquals('/content/splash/2', + $utils->urlFor(array('controller' => 'content', 'action' => 'splash', + 'id' => 2))); } public function testUrlForWithDefaults() { $utils = $this->utils; $utils->mapperDict = array('controller' => 'blog', 'action' => 'view', 'id' => 4); - + $this->assertNull($utils->urlFor()); $this->assertNull($utils->urlFor(array('controller' => 'post'))); $this->assertNull($utils->urlFor(array('id' => 2))); $this->assertEquals('/viewpost/4', $utils->urlFor(array('controller' => 'post', 'action' => 'view', 'id' => 4))); - + $utils->mapperDict = array('controller' => 'blog', 'action' => 'view', 'year' => 2004); $this->assertNull($utils->urlFor(array('month' => 10))); $this->assertNull($utils->urlFor(array('month' => 9, 'day' => 2))); @@ -75,14 +89,14 @@ class UtilWithExplicitTest extends PHPUnit_Framework_TestCase { { $utils = $this->utils; $utils->mapperDict = array('controller' => 'blog', 'action' => 'view', 'id' => 4); - + $this->assertNull($utils->urlFor()); $this->assertNull($utils->urlFor(array('controller' => 'post'))); $this->assertNull($utils->urlFor(array('id' => 2))); $this->assertEquals('/viewpost/4', $utils->urlFor(array('controller' => 'post', 'action' => 'view', 'id' => 4))); - + $utils->mapperDict = array('controller' => 'blog', 'action' => 'view', 'year' => 2004); $this->assertNull($utils->urlFor(array('month' => 10))); $this->assertNull($utils->urlFor()); @@ -91,18 +105,18 @@ class UtilWithExplicitTest extends PHPUnit_Framework_TestCase { public function testUrlForWithDefaultsAndQualified() { $utils = $this->utils; - + $m = $this->mapper; $m->connect('home', '', array('controller' => 'blog', 'action' => 'splash')); $m->connect('category_home', 'category/:section', array('controller' => 'blog', 'action' => 'view', 'section' => 'home')); $m->connect(':controller/:action/:id'); $m->createRegs(array('content', 'blog', 'admin/comments')); - + $environ = array('SCRIPT_NAME' => '', 'SERVER_NAME' => 'www.example.com', 'SERVER_PORT' => '80', 'PATH_INFO' => '/blog/view/4'); Horde_Routes_TestHelper::updateMapper($m, $environ); - + $this->assertNull($utils->urlFor()); $this->assertNull($utils->urlFor(array('controller' => 'post'))); $this->assertNull($utils->urlFor(array('id' => 2))); @@ -112,10 +126,10 @@ class UtilWithExplicitTest extends PHPUnit_Framework_TestCase { 'action' => 'view', 'id' => 4))); $this->assertEquals('/viewpost/4', $utils->urlFor(array('controller' => 'post', 'action' => 'view', 'id' => 4))); - + $environ = array('SCRIPT_NAME' => '', 'HTTP_HOST' => 'www.example.com:8080', 'PATH_INFO' => '/blog/view/4'); Horde_Routes_TestHelper::updateMapper($m, $environ); - + $this->assertNull($utils->urlFor(array('controller' => 'post'))); $this->assertEquals('http://www.example.com:8080/blog/view/4', $utils->urlFor(array('qualified' => true, 'controller' => 'blog', @@ -125,15 +139,15 @@ class UtilWithExplicitTest extends PHPUnit_Framework_TestCase { public function testWithRouteNames() { $m = $this->mapper; - + $utils = $m->utils; $utils->mapperDict = array(); - + $m->connect('home', '', array('controller' => 'blog', 'action' => 'splash')); $m->connect('category_home', 'category/:section', array('controller' => 'blog', 'action' => 'view', 'section' => 'home')); $m->createRegs(array('content', 'blog', 'admin/comments')); - + $this->assertNull($utils->urlFor(array('controller' => 'content', 'action' => 'view'))); $this->assertNull($utils->urlFor(array('controller' => 'content'))); $this->assertNull($utils->urlFor(array('controller' => 'admin/comments'))); @@ -141,32 +155,28 @@ class UtilWithExplicitTest extends PHPUnit_Framework_TestCase { $utils->urlFor('category_home')); $this->assertEquals('/category/food', $utils->urlFor('category_home', array('section' => 'food'))); - $this->assertEquals('/category', - $utils->urlFor('home', array('action' => 'view', 'section' => 'home'))); + $this->assertNull($utils->urlFor('home', array('action' => 'view', 'section' => 'home'))); $this->assertNull($utils->urlFor('home', array('controller' => 'content'))); - $this->assertEquals('/content/splash/2', - $utils->urlFor('home', array('controller' => 'content', 'action' => 'splash', - 'id' => 2))); $this->assertEquals('/', $utils->urlFor('home')); } public function testWithRouteNamesAndDefaults() { $m = $this->mapper; - + $utils = $m->utils; $utils->mapperDict = array(); - + $m->connect('home', '', array('controller' => 'blog', 'action' => 'splash')); $m->connect('category_home', 'category/:section', array('controller' => 'blog', 'action' => 'view', 'section' => 'home')); $m->connect('building', 'building/:campus/:building/alljacks', array('controller' => 'building', 'action' => 'showjacks')); $m->createRegs(array('content', 'blog', 'admin/comments', 'building')); - + $utils->mapperDict = array('controller' => 'building', 'action' => 'showjacks', 'campus' => 'wilma', 'building' => 'port'); - + $this->assertNull($utils->urlFor()); $this->assertEquals('/building/wilma/port/alljacks', $utils->urlFor(array('controller' => 'building', 'action' => 'showjacks', @@ -179,18 +189,18 @@ class UtilWithExplicitTest extends PHPUnit_Framework_TestCase { $m = new Horde_Routes_Mapper(); $utils = $m->utils; $utils->mapperDict = array(); - - $m->resource('message', 'messages', - array('member' => array('mark' => 'GET'), + + $m->resource('message', 'messages', + array('member' => array('mark' => 'GET'), 'collection' => array('rss' => 'GET'))); $m->createRegs(array('messages')); $this->assertNull($utils->urlFor(array('controller' => 'content', 'action' => 'view'))); $this->assertNull($utils->urlFor(array('controller' => 'content'))); $this->assertNull($utils->urlFor(array('controller' => 'admin/comments'))); - $this->assertEquals('/messages', + $this->assertEquals('/messages', $utils->urlFor('messages')); - $this->assertEquals('/messages/rss', + $this->assertEquals('/messages/rss', $utils->urlFor('rss_messages')); $this->assertEquals('/messages/4', $utils->urlFor('message', array('id' => 4))); -- 2.11.0