This repository has been archived by the owner on Dec 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
deano.php
457 lines (399 loc) · 13.7 KB
/
deano.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
<?php
/****
Deano PHP framework
by Colin Dean
http://github.com/colindean/deano
See LICENSE for your rights regarding use of this code.
****/
//////////////////////// FUNCTIONS /////////////////////////////
/// CONTROLLER HELPER METHODS
/**
* Add a route to the routing table.
*
* Supply a method to use only a certain HTTP Method to react to the call.
* Valid methods: GET, POST, PUT, DELETE, HEAD, OPTIONS, TRACK, TRACE
*
* @throws DeanoRouteExistsException
* @param regex $path a regex string which will match the path
* @param function $handler the handler to be called when the path is activated
* @param string $method optional, set to handle certain HTTP methods (CRUD)
**/
function route(/* regex */ $path,
/*function or method*/ $handler,
/*string*/ $method=null){
DeanoRouter::addRoute($path, $handler, $method);
}
/**
* Add an error handler in userspace. The error handler function must accept
* a single parameter, an instance of Exception.
*
* @throws DeanoRouteExistsException
* @param int $code The HTTP Status code associated with the handler
* @param function $handler The function to be called when the code is tripped
**/
function errorHandler(/* int matching http error code */ $code,
/*function or method*/ $handler){
DeanoRouter::addErrorHandler($code, $handler);
}
/**
* Run the router. This should be executed on the last line in the routes
* definition file.
*/
function run(){
DeanoRouter::run();
}
/**
* Log a message
*
* @param string $message The message to be logged
* @param string $level one of DeanoLog::INFO (default), ::WARN, ::ERROR
*/
function dlog($message, $level=DeanoLog::INFO){
DeanoLog::addLog($message, $level);
}
// VIEW HELPER METHODS
/**
* Return the relative URL for a certain handler. Use this to set routes and
* reference the routes by handler function instead of the path itself.
*
* @param function $handler the function the path of which is desired
* @return string the path which should be used
**/
function url_for(/*function*/$handler, $method=null){
return DeanoRouter::getPathForHandler($handler, $method);
}
/**
* Render a full view stack
*
* @param string $template the filename of the template, relative to the views
* directory
*/
function render($template){
DeanoViewManager::getInstance()->render($template);
}
/**
* Render a partial, which is just a single view template
*
* @param string $template the filename of the template, relative to the views
* directory
*/
function render_partial($template){
DeanoViewManager::getInstance()->renderView($template);
}
/**
* Get the output of a view, should only be called once and within the layout
* template
*/
function yield_view(){
echo DeanoViewManager::getInstance()->getYieldView();
}
/**
* Get the current route. From this, look at its path, handler, or method
* members. Useful for determining behavior within a view based on the route.
*/
function current_route(){
return DeanoRouter::getCurrentRoute();
}
////////////////////////// CLASSES //////////////////////////////
class DeanoViewManager {
private static $instance;
public $viewsDir;
private $layoutPath, $yieldView;
private function __construct(){
$this->viewsDir = __DIR__.DIRECTORY_SEPARATOR."views";
$this->layoutPath = $this->viewsDir.DIRECTORY_SEPARATOR."layout".DIRECTORY_SEPARATOR."layout.php";
}
public static function getInstance(){
if(!self::$instance){
self::$instance = new DeanoViewManager();
}
return self::$instance;
}
function render($template){
ob_start();
$this->renderView($template);
$this->yieldView = ob_get_contents();
ob_end_clean();
$this->renderView($this->layoutPath, true);
flush();
}
/**
* Render the view and store it in $yieldView
*/
function renderView($template, $fullyResolved=false){
if(!$fullyResolved){
$templatePath = $this->viewsDir.DIRECTORY_SEPARATOR.$template.".php";
} else {
$templatePath = $template;
}
dlog("Rendering view {$templatePath}");
if(!file_exists($templatePath)){
throw new DeanoViewNotFoundException($templatePath);
}
include_once($templatePath);
}
function getYieldView(){
dlog(sprintf("Yielding view of %d bytes", strlen($this->yieldView)));
return $this->yieldView;
}
}
class DeanoRouter {
private static $handlerTable;// = array();
private static $errorTable;// = array();
private static $errorException;//this belongs elsewhere, but dunno where
public static $currentRoute;// the current DeanoRoute
static public function getPathForHandler(/*function*/$handler, $method=null){
return self::$handlerTable->getRouteByHandler($handler, $method)->path;
}
static public function addRoute(/* regex */ $path,
/*function or method*/ $handler,
/*string*/ $method=null){
dlog("Defining route [{$method} {$path}] with handler {$handler}");
//addRoute will throw an exception if a route already exists
//don't catch it here, as it's a development problem, not a user error
self::$handlerTable->addRoute(new DeanoRoute($path, $handler, $method));
}
static public function addErrorHandler(/*int matching http error code*/ $code,
/*function or method*/ $handler){
dlog("Defining error handler {$handler} for code {$code}");
//addRoute will throw an exception if a route already exists
//don't catch it here, as it's a development problem, not a user error
self::$errorTable->addRoute(new DeanoRoute($code, $handler));
}
static public function getRoute(/*regex*/$path, /*method*/$method=null){
//getRoute will return null if there isn't a route which matches
return self::$handlerTable->getRoute($path, $method);
}
static public function getCurrentRoute(){
return self::$currentRoute;
}
static public function getErrorHandler(/*int*/$code){
//this must always return something, so put a trycatch here
try {
return self::$errorTable->getRoute($code);
} catch (DeanoRouteErrorException $e) {
dlog("No error handler for {$code}, using default");
self::$errorException = $e;
return "DeanoRouter::defaultErrorHandler";
}
}
static public function defaultErrorHandler($e){
$e->header();
header("Content-Type: text/html");
echo("<html><head><title>{$e->code} {$e->status}</title></head><body>".
"<h1>{$e->code} {$e->status}</h1>");
}
static public function init(){
self::$handlerTable = new DeanoRoutingTable();
self::$errorTable = new DeanoRoutingTable();
}
static public function run(){
//get the requested path
$path = array_key_exists('PATH_INFO', $_SERVER) ? $_SERVER['PATH_INFO'] : $_SERVER['REQUEST_URI'];
$method = $_SERVER['REQUEST_METHOD'];
$phpNeedsAGoramFinally = false;
dlog("Getting handler for path {$path} method {$method}");
try {
self::$currentRoute = $route = self::getRoute($path, $method);
dlog("Handler for [{$method} {$path}] is {$route->handler}, calling");
call_user_func($route->handler);
} catch (DeanoRouteErrorException $routeException) {
dlog("Handler for [{$method} {$path}] not found", DeanoLog::WARN);
$errorHandler = self::getErrorHandler($routeException->code);
dlog("Error handler for {$routeException->code} is {$errorHandler->handler}, calling");
call_user_func($errorHandler->handler, $routeException);
} catch (Exception $e) {
echo '<div class="deano-exception" style="border:1px solid red;background-color:#fdd;padding:1em">'.
'<div><strong>Uncaught general exception</strong></div>'.
"<div>{$e}</div>".
"</div>";
if(DEANO_LOG && $phpNeedsAGoramFinally='true'){
DeanoLog::prettyPrint();
}
}
if(DEANO_LOG && !$phpNeedsAGoramFinally){
DeanoLog::prettyPrint();
}
}
}
class DeanoRoute {
public $path;
public $handler;
public $method;
function __construct($path, $handler, $method=null){
$this->path = $path;
$this->handler = $handler;
$this->method = $method;
}
}
class DeanoRoutingTable implements Iterator, Countable {
private $list;
public function __construct(){
$this->list = array();
}
public function addRoute(/*DeanoRoute*/ $route){
//check if a route with the same params already exists in the list
if(!in_array($route, $this->list)){
$this->list[] = $route;
} else {
throw new DeanoRouteDuplicationException($route);
}
}
public function getRouteByHandler($handler, $method=null){
$h = array_values(array_filter($this->list,
create_function('$r',
'return $r->handler == "'.$handler.'";')));
//if there's nothing there, throw a 404
if( count($h) == 0 ){
throw new DeanoRouteNotFoundException($handler, $method);
}
//if there's only one and its method is null, return it
if( (count($h) == 1) && ($h[0]->method == null)){
return $h[0];
}
//now we have all routes which match the location, just need to match method
$byMethod = array_values(array_filter($h,
create_function('$r',
'return $r->method == "'.$method.'";')));
if(count($byMethod) == 0){
throw new DeanoRouteNoMethodException($location, $method);
}
return $byMethod[0];
}
public function getRoute($location, $method=null){
return $this->getRouteByLocation($location, $method);
}
public function getRouteByLocation($location, $method=null){
//rather than loop, filter by location and then by method
//does 5.3 support first class functions?
//using array_values to reindex the array by 0
$byLoc = array_values(array_filter($this->list,
create_function('$r',
'return $r->path == "'.$location.'";')));
//if there's nothing there, throw a 404
if( (count($byLoc) == 0) ){
throw new DeanoRouteNotFoundException($location, $method);
}
//if there's only one and its method is null, return it
if( (count($byLoc) == 1) && ($byLoc[0]->method == null)){
return $byLoc[0];
}
//now we have all routes which match the location, just need to match method
$byMethod = array_values(array_filter($byLoc,
create_function('$r',
'return $r->method == "'.$method.'";')));
if(count($byMethod) == 0){
throw new DeanoRouteNoMethodException($location, $method);
}
return $byMethod[0];
}
//I don't think there's a distinct need for delete route
public function count(){return count($this->list);}
public function key(){return key($this->list);}
public function current(){return current($this->list);}
public function rewind(){reset($this->list);}
public function next(){return next($this->list);}
public function valid(){return $this->current() !== false;}
}
class DeanoRouteErrorException extends Exception {
public $code, $path, $method, $status;
public function __construct($code, $path, $method=null){
$this->message = "General error at [{$method} {$path}]";
$this->code = $code;
$this->path = $path;
$this->method = $method;
$this->status = '';
}
public function header(){
header("HTTP/1.1 {$this->code} {$this->status}");
}
}
class DeanoRouteNotFoundException extends DeanoRouteErrorException {
function __construct($path, $method=null){
parent::__construct(404, $path, $method);
$this->message = "Route not found: [{$method} {$path}]";
$this->status = "Not Found";
}
}
class DeanoRouteNoMethodException extends DeanoRouteErrorException {
function __construct($path, $method=null){
parent::__construct(405, $path, $method);
$this->message = "Route found, but not for given method [{$method} {$path}]";
$this->status = "Method Not Allowed";
}
}
class DeanoRouteDuplicationException extends Exception {
public $route;
function __construct($route){
$this->route = $route;
$this->message = "Duplicate route detected: [{$route->method}]->[{$route->location}]->[{$route->handler}]";
}
}
class DeanoViewNotFoundException extends Exception {
public $template_path;
function __construct($template_path){
$this->template_path = $template_path;
$this->message = "View template not found: [{$template_path}]";
}
}
class DeanoLog {
private static $log; //array();
private static $start_time; //microtime(true);
const INFO = "info";
const WARN = "warn";
const ERROR = "error";
static public function init(){
self::$start_time = microtime();
self::$log = array();
if(!defined('DEANO_LOG')){define('DEANO_LOG', false);}
self::addLog("DeanoLog started");
}
static public function addLog(/*string*/$message, $level=DeanoLog::INFO){
$time = microtime() - self::$start_time;
if(DEANO_LOG === false) return;
self::$log[] = array(
"time" => $time,
"level" => $level,
"message" => $message,
"location" => "",//this needs to be the class::method [filename:line#] of the caller
"memory" => self::_formatUsage(memory_get_peak_usage())
);
}
static public function getLog(){
return self::$log;
}
static public function prettyPrint(){
self::prettyPrintTable();
}
static public function prettyPrintText(){
echo "<pre>";
var_dump(self::getLog());
echo "</pre>";
}
static public function prettyPrintTable(){
echo "<table><thead><tr>";
foreach (array("Time","Level","Message","Location","Memory") as $h){
echo "<th>{$h}</th>";
}
echo "</tr></thead><tbody>";
foreach(self::getLog() as $ln){
echo sprintf(
'<tr><td>%f</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>',
$ln['time'], $ln['level'], $ln['message'],
$ln['location'], $ln['memory']
);
}
echo "</tbody></table>";
}
private function _formatUsage( $bytes ) {
$symbols = array('B', 'KiB', 'MiB', 'GiB', 'TiB' );
$exp = floor( log( $bytes ) / log( 1024 ) );
$formatted = ( $bytes / pow( 1024, floor( $exp ) ) );
return sprintf( '%.2f ' . $symbols[ $exp ], $formatted );
}
}
//for PHP < 5.3.0
if ( !defined('__DIR__') ) define('__DIR__', dirname(__FILE__));
DeanoLog::init();
DeanoRouter::init();
?>