Implement UrlMap::LookupAction()
authorRobert Sesek <rsesek@bluestatic.org>
Wed, 22 Jun 2011 02:03:05 +0000 (22:03 -0400)
committerRobert Sesek <rsesek@bluestatic.org>
Wed, 22 Jun 2011 02:03:05 +0000 (22:03 -0400)
http/url_map.php
testing/tests/http/url_map_test.php

index 0f2caf1502c1a29b8ca6c3c34d124aba4a3c7d7a..8df90e82f7aa1c15d23c0f851c8452b716f7e030 100644 (file)
@@ -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);
+  }
 }
index 1df2d155d09f18dc26009de929c7710c5db07c92..afe5436aa5e22a8b2469597b305f137168948d60 100644 (file)
@@ -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'));
+  }
 }