forked from bastos/nicedog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NiceDog.php
234 lines (208 loc) · 6.39 KB
/
NiceDog.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
/*
The MIT License
Copyright (c) 2007 Tiago Bastos
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
define('__DEBUG__',true);
/*
* Controller
*/
class C {
var $layout = true;
var $layout_tamplate = 'views/layout.php';
var $headers;
/* Render function return php rendered in a variable */
public function render($file)
{
if ($this->layout==false){
return $this->open_template($file);
} else {
$this->content = $this->open_template($file);
return $this->open_template($this->layout_tamplate);
}
}
/* Open template to render and return php rendered in a variable using ob_start/ob_end_clean */
private function open_template($name)
{
$vars = get_object_vars($this);
ob_start();
if (file_exists($name)){
if (count($vars)>0)
foreach($vars as $key => $value){
$$key = $value;
}
require($name);
} else {
throw new Exception('View ['.$name.'] Not Found');
}
$out = ob_get_contents();
ob_end_clean();
return $out;
}
/* Add information in header */
public function header($text){
$this->headers[] = $text;
}
/*
Redirect page to annother place using header,
$now indicates that dispacther will not wait all process
*/
public function redirect($url,$now=false)
{
if(!$now)
$this->header("Location: {$url}");
else header("Location: {$url}");
}
}
/*
* Application core
*/
class NiceDog {
var $routes = array();
static private $instance = NULL ;
function __construct()
{
if (isset($_GET['url']))
$this->url =trim( $_GET['url'], '/');
else $this->url = '';
}
/* Singleton */
public function getInstance()
{
if(self::$instance == NULL)
{
self::$instance = new NiceDog();
}
return self::$instance;
}
/* Add url to routes */
public function add_url($rule, $klass, $klass_method, $http_method = 'GET')
{
$this->routes[] = array('/^' . str_replace('/','\/',$rule) . '$/', $klass,$klass_method,$http_method);
}
/* Process requests and dispatch */
public function dispatch()
{
foreach($this->routes as $rule=>$conf) {
if (preg_match($conf[0], $this->url, $matches) and $_SERVER['REQUEST_METHOD'] == $conf[3]){
$matches = $this->parse_urls_args($matches);//Only declared variables in url regex
$klass = new $conf[1]();
ob_start();
call_user_func_array(array($klass , $conf[2]),$matches);
$out = ob_get_contents();
ob_end_clean();
if (count($klass->headers)>0){
foreach($klass->headers as $header){
header($header);
}
}
print $out;
exit();//Argh! Its not pretty, but usefull...
}
}
call_user_func_array('r404' , $_SERVER['REQUEST_METHOD']);
}
/* Parse url arguments */
private function parse_urls_args($matches)
{
$first = array_shift($matches);
$new_matches = array();
foreach($matches as $k=>$match){
if (is_string($k)){
$new_matches[$k]=$match;
}
}
return $new_matches;
}
}
/*
* New routes, just sugar!
* R('','Test','index','GET'); turns:
* R('')->controller('test')->action('index')->on('GET');
* Thanks to: Rafael S. Souza <rafael.ssouza [__at__] gmail.com>
*/
function R($pattern){
return new Route($pattern);
}
class Route{
var $pattern;
var $controller;
var $action;
var $http_method = 'GET';
function __construct($pattern){
$this->pattern = $pattern;
return $this;
}
function controller($controller){
$this->controller = $controller;
return $this;
}
function action($action){
$this->action = $action;
return $this;
}
function on($http_method){
$this->http_method = $http_method;
$this->bind();
return $this;
}
function bind(){
$router = NiceDog::getInstance()->add_url($this->pattern,$this->controller,$this->action,$this->http_method);
}
}
/*
* Run application
*/
function Run()
{
try {
NiceDog::getInstance()->dispatch();
} catch (Exception $e) {
if (__DEBUG__==true) {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Error!</title>
</head>
<body>
<h1>Caught exception: <?= $e->getMessage(); ?></h1>
<h2>File: <?= $e->getFile()?></h2>
<h2>Line: <?= $e->getLine()?></h2>
<h3>Trace</h3>
<pre>
<?php print_r ($e->getTraceAsString()); ?>
</pre>
<h3>Exception Object</h3>
<pre>
<?php print_r ($e); ?>
</pre>
<h3>Var Dump</h3>
<pre>
<?php debug_print_backtrace (); ?>
</pre>
</body>
</html>
<?php
} else {
echo 'Oops';
}
}
}
?>