forked from BaylorRae/sammy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsammy.php
executable file
·150 lines (115 loc) · 3.93 KB
/
sammy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/**
* Sammy - A bare-bones PHP version of the Ruby Sinatra framework.
*
* @version 1.0
* @author Dan Horrigan
* @license MIT License
* @copyright 2010 Dan Horrigan
*/
function get($route, $callback) {
Sammy::process($route, $callback, 'GET');
}
function post($route, $callback) {
Sammy::process($route, $callback, 'POST');
}
function put($route, $callback) {
Sammy::process($route, $callback, 'PUT');
}
function delete($route, $callback) {
Sammy::process($route, $callback, 'DELETE');
}
function ajax($route, $callback) {
Sammy::process($route, $callback, 'XMLHttpRequest');
}
class Sammy {
public static $route_found = false;
public $uri = '';
public $segments = '';
public $method = '';
public $format = '';
public static function instance() {
static $instance = null;
if( $instance === null ) {
$instance = new Sammy;
}
return $instance;
}
public static function run() {
if( !static::$route_found ) {
echo 'Route not defined!';
}
ob_end_flush();
}
public static function process($route, $callback, $type) {
$sammy = static::instance();
// Check for ajax
if( $type == 'XMLHttpRequest' )
$sammy->method = isset($_SERVER['HTTP_X_REQUESTED_WITH']) ? $_SERVER['HTTP_X_REQUESTED_WITH'] : 'GET';
if( static::$route_found || (!preg_match('@^'.$route.'(?:\.(\w+))?$@uD', $sammy->uri, $matches) || $sammy->method != $type) ) {
return false;
}
// Get the extension
$extension = $matches[count($matches)-1];
$extension_test = substr($sammy->uri, -(strlen($extension)+1), (strlen($extension)+1));
if( $extension_test == '.' . $extension )
$sammy->format = $extension;
static::$route_found = true;
echo $callback($sammy);
}
public function __construct() {
ob_start();
$this->uri = $this->get_uri();
$this->segments = explode('/', trim($this->uri, '/'));
$this->method = $this->get_method();
}
public function segment($num) {
$num--;
// Remove the extension
$this->segments[$num] = isset($this->segments[$num]) ? rtrim($this->segments[$num], '.' . $this->format) : null;
return isset($this->segments[$num]) ? $this->segments[$num] : null;
}
protected function get_method() {
return isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET';
}
protected function get_uri($prefix_slash = true) {
if( isset($_SERVER['PATH_INFO']) ) {
$uri = $_SERVER['PATH_INFO'];
}elseif( isset($_SERVER['REQUEST_URI']) ) {
$uri = $_SERVER['REQUEST_URI'];
if( strpos($uri, $_SERVER['SCRIPT_NAME']) === 0 ) {
$uri = substr($uri, strlen($_SERVER['SCRIPT_NAME']));
}elseif( strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0 ) {
$uri = substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME'])));
}
// This section ensures that even on servers that require the URI to be in the query string (Nginx) a correct
// URI is found, and also fixes the QUERY_STRING server var and $_GET array.
if( strncmp($uri, '?/', 2) === 0 ) {
$uri = substr($uri, 2);
}
$parts = preg_split('#\?#i', $uri, 2);
$uri = $parts[0];
if( isset($parts[1]) ) {
$_SERVER['QUERY_STRING'] = $parts[1];
parse_str($_SERVER['QUERY_STRING'], $_GET);
}else {
$_SERVER['QUERY_STRING'] = '';
$_GET = array();
}
$uri = parse_url($uri, PHP_URL_PATH);
}else {
// Couldn't determine the URI, so just return false
return false;
}
// Do some final cleaning of the URI and return it
return ($prefix_slash ? '/' : '').str_replace(array('//', '../'), '/', trim($uri, '/'));
}
public function format($name, $callback) {
$sammy = static::instance();
if( !empty($sammy->format) && $name == $sammy->format )
echo $callback($sammy);
else
return false;
}
}
$sammy = Sammy::instance();