mirror of
https://github.com/jimeh/php-rack.git
synced 2026-02-19 03:46:39 +00:00
initial commit
This commit is contained in:
19
app/api.php
Normal file
19
app/api.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
class Api {
|
||||
|
||||
function __construct (&$app) {
|
||||
$this->app =& $app;
|
||||
}
|
||||
|
||||
function call (&$env) {
|
||||
if ( preg_match("/^\/api\/([a-z]{1}[a-z0-9]*)/i", $env["PATH_INFO"], $match) ) {
|
||||
return array(200, array("Content-Type" => "text/json"), array("{api_call: \"".$match[1]."\"}"));
|
||||
} else {
|
||||
return $this->app->call($env);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
16
app/app.php
Normal file
16
app/app.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
class App {
|
||||
|
||||
function call (&$env) {
|
||||
if ( $env["PATH_INFO"] == "/" ) {
|
||||
return array(200, array("Content-Type" => "text/html"), array("Welcome Home"));
|
||||
} elseif ( preg_match("/^\/about\/?/i", $env["PATH_INFO"]) ) {
|
||||
return array(200, array("Content-Type" => "text/html"), array("Rack-style middleware is cool."));
|
||||
}
|
||||
return Rack::not_found();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
29
app/format.php
Normal file
29
app/format.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
class Format {
|
||||
|
||||
function __construct (&$app) {
|
||||
$this->app =& $app;
|
||||
}
|
||||
|
||||
function call (&$env) {
|
||||
|
||||
// available formats
|
||||
$formats = array("txt" => "text/plain", "xml" => "application/xml");
|
||||
|
||||
// call the next middleware in the stack
|
||||
list($status, $headers, $body) = $this->app->call($env);
|
||||
|
||||
// do something with response headers
|
||||
foreach( $formats as $key => $value ) {
|
||||
if ( !empty($env["request.vars"]["format"]) && $env["request.get"]["format"] == $key ) {
|
||||
$headers["Content-Type"] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return array($status, $headers, $body);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user