Skip to content

Commit 7182678

Browse files
committed
Initial commit
0 parents  commit 7182678

File tree

4 files changed

+168
-0
lines changed

4 files changed

+168
-0
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## generic files to ignore
2+
*~
3+
*.lock
4+
*.DS_Store
5+
*.swp
6+
*.swo
7+
*.out

.htaccess

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
RewriteEngine On
2+
RewriteCond %{REQUEST_FILENAME} !-f
3+
RewriteCond %{REQUEST_FILENAME} !-d
4+
RewriteRule ^(.+)$ index.php/$1

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
Stradlin
2+
========
3+
4+
Severely minimal PHP routing microframework inspired by Sinatra that is specially useful for prototyping RESTful APIs.
5+
6+
Usage example:
7+
8+
```php
9+
10+
require_once('stradlin.php');
11+
12+
route('/hello/(?P<name>\w+)/?', all_verbs(), function($params) {
13+
$name = $params['name'];
14+
if (is_numeric($name)) {
15+
set_status_code(400); /* Bad Request */
16+
$doc = array(
17+
"status" => "error",
18+
"errors" => array(
19+
"Come on! Your name can't be numeric! Are you a clone trooper or what?"
20+
)
21+
);
22+
} else {
23+
$doc = array(
24+
"status" => "ok",
25+
"data" => array(
26+
"greeting" => "Hello Mr. or Mrs. $name!!! Welcome to Stradlin :)"
27+
)
28+
);
29+
}
30+
serve_json($doc, JSON_PRETTY_PRINT);
31+
});
32+
```
33+
34+
In order to have the clean URLs working please use the provided .htaccess file for Apache and write/include your controllers from index.php or simple adapt it to suit your needs.
35+
36+
Speaking of controllers, this microframework should have been called 'Axl', that's a real control freak! :)
37+
38+
For more information please read the freaking sources and please stay tuned.
39+
40+
(c) 2012 - Bit Zeppelin S.A.C. - Developed by Antonio Ognio a.k.a @gnrfan

stradlin.php

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
function serve_json($doc, $options = 0) {
4+
set_content_type('application/json');
5+
$doc = json_encode($doc, $options);
6+
if (array_key_exists('HTTP_JSONP_CALLBACK', $_SERVER) && strlen($_SERVER['HTTP_JSONP_CALLBACK'])>0) {
7+
$callback = $_SERVER['HTTP_JSONP_CALLBACK'];
8+
printf("%s(\"%s\");\n", $callback, addslashes($doc));
9+
} else {
10+
echo $doc;
11+
}
12+
}
13+
14+
function get_request_uri() {
15+
return $_SERVER['REDIRECT_URL'];
16+
}
17+
18+
function request_method_matches($methods) {
19+
return in_array($_SERVER['REQUEST_METHOD'], $methods);
20+
}
21+
22+
function route($regexp, $methods, $callback) {
23+
24+
/* Sanitize regexp */
25+
if (!preg_match('/^\^(.)+$/', $regexp)) {
26+
$regexp = sprintf("^%s", $regexp);
27+
}
28+
if (!preg_match('/^(.)+\$$/', $regexp)) {
29+
$regexp = sprintf("%s$", $regexp);
30+
}
31+
$regexp = str_replace("/", "\/", $regexp);
32+
$regexp = sprintf("/%s/", $regexp);
33+
34+
/* Create array of accepted HTTP methods */
35+
$methods = split(",", $methods) ;
36+
foreach($methods as $k=>$v) {
37+
$methods[$k] = trim(strtoupper($v));
38+
}
39+
40+
/* Match */
41+
$uri = get_request_uri();
42+
if (request_method_matches($methods) && preg_match($regexp, $uri, $params)) {
43+
$callback($params);
44+
}
45+
46+
}
47+
48+
function status_code_map() {
49+
return array(
50+
200 => 'OK',
51+
201 => 'Created',
52+
202 => 'Accepted',
53+
203 => 'Non',
54+
204 => 'No Content',
55+
205 => 'Reset Content',
56+
206 => 'Partial Content',
57+
300 => 'Multiple Choices',
58+
301 => 'Moved Permanently',
59+
302 => 'Found',
60+
303 => 'See Other',
61+
304 => 'Not Modified',
62+
305 => 'Use Proxy',
63+
307 => 'Temporary Redirect',
64+
400 => 'Bad Request',
65+
401 => 'Unauthorized',
66+
402 => 'Payment Required',
67+
403 => 'Forbidden',
68+
404 => 'Not Found',
69+
405 => 'Method Not Allowed',
70+
406 => 'Not Acceptable',
71+
407 => 'Proxy Authentication Required',
72+
408 => 'Request Timeout',
73+
409 => 'Conflict',
74+
410 => 'Gone',
75+
411 => 'Length Required',
76+
412 => 'Precondition Failed',
77+
413 => 'Request Entity Too Large',
78+
414 => 'Request',
79+
415 => 'Unsupported Media Type',
80+
416 => 'Requested Range Not Satisfiable',
81+
417 => 'Expectation Failed',
82+
500 => 'Internal Server Error',
83+
501 => 'Not Implemented',
84+
502 => 'Bad Gateway',
85+
503 => 'Service Unavailable',
86+
504 => 'Gateway Timeout',
87+
505 => 'HTTP Version Not Supported',
88+
);
89+
}
90+
91+
function set_content_type($content_type) {
92+
header("Content-Type: $content_type");
93+
}
94+
95+
function set_status_code($status_code) {
96+
$map = status_code_map();
97+
header(sprintf("HTTP/1.1 %d %s", $status_code, strtoupper($map[$status_code])));
98+
}
99+
100+
function all_verbs_array() {
101+
return array(
102+
"OPTIONS",
103+
"GET",
104+
"HEAD",
105+
"POST",
106+
"PUT",
107+
"DELETE",
108+
"TRACE",
109+
"CONNECT"
110+
);
111+
}
112+
113+
function all_verbs() {
114+
return implode(", ", all_verbs_array());
115+
}
116+
117+
?>

0 commit comments

Comments
 (0)