Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
changed project name to presto, added variable handling in URI, added…
… filetype detection
- Loading branch information
Jake McGraw
committed
Aug 24, 2011
1 parent
b0b7d51
commit 4cdc279
Showing
4 changed files
with
252 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,34 @@ | ||
## mapd: Map HTTP requests to PHP functions | ||
## presto: A micro framework for mapping HTTP requests to PHP function calls | ||
|
||
### Example | ||
|
||
GET /foo/bar => mapd_get_foo_bar(); | ||
<?php | ||
// HTTP GET / maps to | ||
presto_get_index_index(); | ||
// HTTP GET /foo maps to | ||
presto_get_foo_index(); | ||
// HTTP GET /foo/bar maps to | ||
presto_get_foo_bar(); | ||
// HTTP GET /foo/bar-bar maps to | ||
presto_get_foo_barBar(); | ||
GET /foo/bar-spam => mapd_get_foo_barSpam(); | ||
// HTTP POST /foo/bar maps to | ||
presto_post_foo_bar(array(/* post variables */)); | ||
// HTTP GET /foo/bar/12345 maps to | ||
presto_get_foo_bar(array(12345)); | ||
// HTTP POST /foo/bar/hello maps to | ||
presto_get_foo_bar(array("hello", /* post variables */)); | ||
// HTTP GET /foo/bar/hello/world/monkey maps to | ||
presto_get_foo_bar(array("hello" => "world", "monkey")); | ||
|
||
POST /foo/bar => mapd_post_foo_bar($post_vars); | ||
## Install | ||
|
||
TODO | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,11 @@ | ||
<?php | ||
|
||
require_once "../app/mapd.php"; | ||
require_once "../app/presto.php"; | ||
// require_once "../app/<YOUR PRESTO FUNCTIONS>.php"; | ||
|
||
mapd_exec_json( | ||
mapd_route( | ||
$_SERVER["REQUEST_METHOD"], | ||
$_SERVER["REQUEST_URI"], | ||
null | ||
), | ||
$_POST | ||
list($func, $vars) = presto_route( | ||
$_SERVER["REQUEST_METHOD"], | ||
$_SERVER["REQUEST_URI"] | ||
); | ||
|
||
presto_exec_json($func, array_merge($_REQUEST, $vars)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
require_once "PHPUnit/Framework/TestCase.php"; | ||
require_once "../app/presto.php"; | ||
|
||
class PrestoTest extends PHPUnit_Framework_TestCase | ||
{ | ||
public function testRoute() | ||
{ | ||
list($func) = presto_route("GET", "/"); | ||
$this->assertEquals("presto_get_index_index", $func); | ||
|
||
list($func) = presto_route("GET", "////"); | ||
$this->assertEquals("presto_get_index_index", $func); | ||
|
||
list($func) = presto_route("GET", "/foo"); | ||
$this->assertEquals("presto_get_foo_index", $func); | ||
|
||
foreach(array("GET", "POST", "PUT", "DELETE", "HEAD") as $method) { | ||
list($func) = presto_route($method, "/foo/bar"); | ||
$this->assertEquals("presto_" . strtolower($method) . "_foo_bar", $func); | ||
|
||
list($func) = presto_route(strtolower($method), "/foo/bar"); | ||
$this->assertEquals("presto_" . strtolower($method) . "_foo_bar", $func); | ||
} | ||
|
||
list($func) = presto_route("GET", "/fOO/bAR"); | ||
$this->assertEquals("presto_get_foo_bar", $func); | ||
|
||
list($func) = presto_route("GET", "/foo/bar?hello=world"); | ||
$this->assertEquals("presto_get_foo_bar", $func); | ||
|
||
list($func) = presto_route("FAIL", "/foo/bar"); | ||
$this->assertFalse($func); | ||
|
||
list($func) = presto_route("GET", "/foo!@#$%^&*()+=/bar.;:'\"/"); | ||
$this->assertEquals("presto_get_foo_bar", $func); | ||
} | ||
|
||
public function testRouteWithBaseUrl() | ||
{ | ||
list($func) = presto_route("GET", "/api/", "/api"); | ||
$this->assertEquals("presto_get_index_index", $func); | ||
|
||
list($func) = presto_route("GET", "/api/", "/api/"); | ||
$this->assertEquals("presto_get_index_index", $func); | ||
|
||
list($func) = presto_route("GET", "/api/foo", "/api"); | ||
$this->assertEquals("presto_get_foo_index", $func); | ||
|
||
list($func) = presto_route("GET", "/api/foo/bar", "/api"); | ||
$this->assertEquals("presto_get_foo_bar", $func); | ||
} | ||
|
||
public function testRouteVariables() | ||
{ | ||
foreach(array("/", "/foo", "/foo/bar", "/foo/bar/", "/foo///bar//") as $uri) { | ||
list($func, $vars) = presto_route("GET", $uri); | ||
$this->assertEmpty($vars); | ||
} | ||
|
||
list($func, $vars) = presto_route("GET", "/foo/bar/hello"); | ||
$this->assertEquals("presto_get_foo_bar", $func); | ||
$this->assertEquals(array("hello"), $vars); | ||
|
||
list($func, $vars) = presto_route("GET", "/foo/bar/hello/"); | ||
$this->assertEquals("presto_get_foo_bar", $func); | ||
$this->assertEquals(array("hello"), $vars); | ||
|
||
list($func, $vars) = presto_route("GET", "/foo///bar///hello"); | ||
$this->assertEquals("presto_get_foo_bar", $func); | ||
$this->assertEquals(array("hello"), $vars); | ||
|
||
list($func, $vars) = presto_route("GET", "/foo/bar/hello/world"); | ||
$this->assertEquals("presto_get_foo_bar", $func); | ||
$this->assertEquals(array("hello" => "world"), $vars); | ||
|
||
list($func, $vars) = presto_route("GET", "/foo/bar/a/b/c"); | ||
$this->assertEquals("presto_get_foo_bar", $func); | ||
$this->assertEquals(array("a" => "b", "c"), $vars); | ||
|
||
list($func, $vars) = presto_route("GET", "/foo/bar/a/b/c/d"); | ||
$this->assertEquals("presto_get_foo_bar", $func); | ||
$this->assertEquals(array("a" => "b", "c" => "d"), $vars); | ||
|
||
list($func, $vars) = presto_route("GET", "/foo/bar/a/B/c/D"); | ||
$this->assertEquals("presto_get_foo_bar", $func); | ||
$this->assertEquals(array("a" => "b", "c" => "d"), $vars); | ||
|
||
list($func, $vars) = presto_route("GET", "/foo/bar/hello/world-spam"); | ||
$this->assertEquals("presto_get_foo_bar", $func); | ||
$this->assertEquals(array("hello" => "world-spam"), $vars); | ||
} | ||
|
||
public function testRouteFiletype() | ||
{ | ||
list($func, $vars) = presto_route("GET", "/foo/bar.js"); | ||
$this->assertEquals("presto_get_foo_bar", $func); | ||
$this->assertEquals(array("_filetype" => "js"), $vars); | ||
|
||
list($func, $vars) = presto_route("GET", "/foo/bar/hello.js"); | ||
$this->assertEquals("presto_get_foo_bar", $func); | ||
$this->assertEquals(array("hello.js", "_filetype" => "js"), $vars); | ||
|
||
list($func, $vars) = presto_route("GET", "/foo/bar/hello/world.js"); | ||
$this->assertEquals("presto_get_foo_bar", $func); | ||
$this->assertEquals(array("hello" => "world.js", "_filetype" => "js"), $vars); | ||
} | ||
} |