From 9cb78c905bc3445c24eb5c4f0f58054f9a4f616c Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Tue, 21 Jun 2011 22:03:05 -0400 Subject: [PATCH] Implement UrlMap::LookupAction() --- http/url_map.php | 29 ++++++++++++++++++++++++++--- testing/tests/http/url_map_test.php | 15 +++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/http/url_map.php b/http/url_map.php index 0f2caf1..8df90e8 100644 --- a/http/url_map.php +++ b/http/url_map.php @@ -16,6 +16,8 @@ namespace hoplite\http; +require_once HOPLITE_ROOT . '/base/functions.php'; + /*! A UrlMap will translate a raw HTTP request into a http\Request object. It does so by matching the incoming URL to a pattern. For information on the format of @@ -175,7 +177,23 @@ class UrlMap @return Action|NULL The loaded action, or NULL on error. */ public function LookupAction($map_value) - {} + { + // If the first character is uppercase or a namespaced class, simply return + // the value. + $first_char = $map_value[0]; + if ($first_char == '\\' || ctype_upper($first_char)) + return $map_value; + + // Otherwise this is a path. Check if an extension is present, and if not, + // add one. + $pathinfo = pathinfo($map_value); + if (!isset($pathinfo['extension'])) { + $map_value .= '.php'; + $pathinfo['extension'] = 'php'; + } + + return $this->_ClassNameFromFileName($pathinfo); + } /*! Takes a file name and returns the name of an Action class. This uses an @@ -184,8 +202,13 @@ class UrlMap This can be overridden to provide a custom transformation. + @param array Result of a pathinfo() call + @return string Action class name. */ - protected function _ClassNameFromFileName($file_name) - {} + protected function _ClassNameFromFileName($pathinfo) + { + $filename = $pathinfo['filename']; + return \hoplite\base\UnderscoreToCamelCase($filename); + } } diff --git a/testing/tests/http/url_map_test.php b/testing/tests/http/url_map_test.php index 1df2d15..afe5436 100644 --- a/testing/tests/http/url_map_test.php +++ b/testing/tests/http/url_map_test.php @@ -149,4 +149,19 @@ class UrlMapTest extends \PHPUnit_Framework_TestCase $request = new http\Request('user/TEST'); $this->assertEquals('Third', $this->fixture->Evaluate($request)); } + + public function testLookupActionClass() + { + $test_class = '\hoplite\test\TestAction'; + $this->assertEquals($test_class, $this->fixture->LookupAction($test_class)); + + $test_class = 'TestAction'; + $this->assertEquals($test_class, $this->fixture->LookupAction($test_class)); + } + + public function testLookupActionFile() + { + $this->assertEquals('TestAction', $this->fixture->LookupAction('actions/test_action')); + $this->assertEquals('TestAction', $this->fixture->LookupAction('actions/test_action.php')); + } } -- 2.22.5