kissmvc.php
20.2 KB
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
<?php
require('kissmvc_core.php');
//===============================================================
// Routing table
//===============================================================
final class Route {
static protected $table = array();
static function match($verbs, $route, $controller, $name='', $maint=false) {
$request_uri_parts = $route ? explode('/', $route) : array();
if (!is_array($verbs)) {
$verbs = array($verbs);
}
foreach ($verbs as $verb) {
static::$table[] = array('verb' => strtoupper($verb),
'uri_parts' => $request_uri_parts,
'controller' => $controller,
'name' => $name,
'route' => $route,
'maint' => $maint);
}
}
static function get($route, $controller, $name='', $maint=false) {
Route::match('GET', $route, $controller, $name, $maint);
}
static function post($route, $controller, $name='', $maint=false) {
Route::match('POST', $route, $controller, $name, $maint);
}
static function put($route, $controller, $name='', $maint=false) {
Route::match('PUT', $route, $controller, $name, $maint);
}
static function delete($route, $controller, $name='', $maint=false) {
Route::match('DELETE', $route, $controller, $name, $maint);
}
static function any($route, $controller, $name='', $maint=false) {
Route::match('*', $route, $controller, $name, $maint);
}
static function do_route($uri, $verb) {
$uri = $uri ? explode('?', $uri)[0] : '';
$request_uri_parts = $uri ? explode('/', $uri) : array();
$params = array();
foreach (static::$table as $row) {
if ($row['verb'] != $verb && $row['verb'] != '*') continue;
if (count($row['uri_parts']) != count($request_uri_parts)) continue;
for ($i = 0, $count = count($row['uri_parts']); $i < $count; ++$i) {
$table_part = $row['uri_parts'][$i];
$req_part = $request_uri_parts[$i];
// Check if this is a parameter
if (substr($table_part, 0, 1) == '{' &&
substr($table_part, -1, 1) == '}') {
$params[substr($table_part, 1, -1)] = urldecode($req_part);
continue;
}
// Otherwise this part must match exactly
if ($table_part != $req_part) continue 2;
}
// If we've made it this far, then it's time to return
return array('uri' => $uri,
'verb' => $verb,
'params' => $params,
'controller' => $row['controller'],
'maint' => $row['maint']);
}
// Didn't match - 404!
return false;
}
static function link($name, $params=array()) {
foreach (static::$table as $row) {
if ($row['name'] == $name) {
$uri_parts = explode('/', $row['route']);
foreach ($uri_parts as &$part) {
if (substr($part, 0, 1) == '{' &&
substr($part, -1, 1) == '}') {
$param_name = substr($part, 1, -1);
$part = $params[$param_name];
}
}
return htmlspecialchars(WEB_FOLDER . implode('/', $uri_parts));
}
}
}
}
//===============================================================
// Model/ORM
//===============================================================
abstract class Model {
// Override these variables as appropriate
protected $tablename = false;
protected $createtime = false;
protected $updatetime = false;
protected $softdelete = false;
protected $pkname = false;
protected $dbhfnname = 'getdbh';
protected $DB_TYPE = 'MYSQL';
protected $COMPRESS_ARRAY = true;
protected $query_builder = "QueryBuilder";
protected $rs = array();
protected $foreign = array();
protected $exists = false;
protected $dirty = array();
/**
* 'Model' class
* Provides an object-relational model interface
* __construct(mixed $arg)
* @param arg(array) parameters to place into a new Model
* @param arg index of Model to be loaded
* @param exists true if is known that the Model exists
*/
public function __construct($arg='', $exists=false) {
// Infer table name if not defined
if (!$this->tablename) {
$this->tablename = strtolower(get_class($this)) . 's';
}
// Discover the table schema
$dbh = $this->getdbh();
$sql = 'SHOW COLUMNS IN ' . $this->enquote($this->tablename);
$stmt = $dbh->prepare($sql);
$stmt->execute();
while ($rs = $stmt->fetch(PDO::FETCH_ASSOC)) {
$name = $rs['Field'];
$this->rs[$name] = '';
if ($rs['Key'] == 'PRI') {
$this->pkname = $name;
}
}
if (isset($arg) && $arg) {
if (is_array($arg)) {
// Set up a new Model if given array
$this->load_from_array($arg);
} else {
// Retrieve row for given primary key
$this->select_row($arg);
}
}
if ($exists) {
$this->exists = true;
$this->dirty = array();
}
}
/**
* Getter
* @param key name of parameter
* @return value of parameter or null
*/
public function __get($key) {
if (array_key_exists($key, $this->rs)) {
return $this->rs[$key];
} elseif (array_key_exists($key, $this->foreign)) {
return $this->foreign[$key];
} else {
return null;
}
}
/**
* Setter
* @param key name of parameter
* @param val value of parameter
* @return this instance
*/
public function __set($key, $val) {
//if (isset($this->rs[$key])) {
if (array_key_exists($key, $this->rs)) {
$this->rs[$key] = $val;
$this->dirty[] = $key;
}
return $this;
}
/**
* Stringifier
* @return dump of internal array
*/
public function __toString() {
ob_start();
echo get_class($this) . '(Model) - ';
var_dump($this->rs);
return ob_get_clean();
}
/**
* Creates a new Model based on provided data and saves to database
* @param arr associative array with Model data
* @return new Model instance with data
*/
static public function create(array $arr) {
$model = new static();
$model->load_from_array($arr);
return $model->save();
}
/**
* Find a model by index
* @param id index of the model to be retrieved
* @return new Model instance with data
*/
static public function find($id) {
return new static($id);
}
/**
* Magic function to pass unknown static calls to the query builder
*/
static public function __callStatic($method, $args) {
$static = new static();
$qb = new $static->query_builder(get_called_class());
return call_user_func_array(array($qb, $method), $args);
}
/**
* Write Model data to database
* @param arr (optional) associative array with data to be written
* @return this instance
*/
public function save(array $arr = array()) {
if (empty($arr)) $arr = $this->rs;
if ($this->exists) {
if ($this->updatetime) {
$this->rs['updated_at'] = $this->makedatetime();
}
return $this->update_row();
} else {
if ($this->createtime) {
$this->rs['created_at'] = $this->makedatetime();
}
return $this->insert_row();
}
}
/**
* Delete Model
* If $this->softdelete == true, this function sets the Model to soft-deleted
* @return true on success
*/
public function delete() {
if ($this->softdelete) {
$this->rs['deleted_at'] = $this->makedatetime();
$this->save();
} else {
$this->force_delete();
}
}
/**
* Force delete Model
* Overrides $this->softdelete
* @return true on success
*/
public function force_delete() {
if ($this->delete_row()) {
$this->exists = false;
return true;
}
return false;
}
/**
* Restore a soft-deleted Model
* @return this instance
*/
public function restore() {
if ($this->softdelete && $this->trashed()) {
$this->rs['deleted_at'] = '';
$this->save();
}
return $this;
}
/**
* Checks whether the Model is in the database
* @return true if the Model is in the database
*/
public function exists() {
return $this->exists;
}
/**
* Checks whether the Model has changed from the database state
* @return true if the Model has been changed
*/
public function dirty() {
return !empty($this->dirty);
}
/**
* Checks whether Model has been soft-deleted
* @return true if the Model has been soft-deleted
*/
public function trashed() {
return ($this->softdelete && $this->rs['deleted_at']);
}
static public function pkname() {
$static = new static();
return $static->pkname;
}
protected function getdbh() {
return call_user_func($this->dbhfnname);
}
protected function enquote($name) {
if ($this->DB_TYPE == 'MYSQL') {
return '`' . $name . '`';
} else {
// Put future databases here
return '"' . $name . '"';
}
}
protected function makedatetime() {
if ($this->DB_TYPE == 'future') {
// Put future databases here
return 'future';
} else {
// MySQL format
return date('Y-m-d H:i:s');
}
}
protected function load_from_array($arr) {
foreach ($arr as $k => $v) {
$this->{$k} = $v;
}
return $this;
}
static public function execute_query($query) {
$static = new static();
$dbh = $static->getdbh();
// Build query string
$sql = 'SELECT * FROM ' . $static->enquote($static->tablename);
// Apply where conditions
if (count($query->conditions)) {
$sql .= ' WHERE ';
$first = true;
foreach ($query->conditions as $condition) {
if ($first) {
$first = false;
} else {
$sql .= ' ' . $condition['and_or'] . ' ';
}
$sql .= $static->enquote($condition['column']) . ' ' .
$condition['operator'];
if (is_array($condition['value'])) {
$s = str_repeat(', ?', count($condition['value']));
$s = substr($s, 2); // Remove leading comma and space
$sql .= ' (' . $s . ')';
} elseif ($condition['value']) {
$sql .= ' ?';
}
}
}
// Apply order by
if (count($query->order)) {
$sql .= ' ORDER BY';
$s = '';
foreach ($query->order as $order) {
$s .= ', ' . $static->enquote($order['column']) . ' ' . $order['dir'];
}
$sql .= substr($s, 1); // Remove leading comma
}
// Apply limit on result length
if ($query->limit) {
$sql .= ' LIMIT ' . $query->limit;
}
// Prepare statement and bind values
$stmt = $dbh->prepare($sql);
if (count($query->conditions)) {
$i = 0;
foreach ($query->conditions as $condition) {
$v = $condition['value'];
if (!$v) continue;
if (is_array($v)) {
foreach ($v as $value) {
$stmt->bindValue(++$i, $value);
}
} else {
$stmt->bindValue(++$i, $v);
}
}
}
// Get results
$stmt->execute();
$rs = $stmt->fetchAll(PDO::FETCH_ASSOC);
$results = array();
foreach ($rs as $row) {
$results[] = new static($row, true);
}
// Get relationships
/**
* NOTE: This only implements belongsTo()
*/
foreach ($query->foreign as $relationship) {
$model = $relationship['foreign_model'];
$local_index = $relationship['local_index'];
$foreign_pkname = $model::pkname();
$foreign = $model::where($foreign_pkname , 'IN',
model_column($results, $local_index));
foreach ($relationship['child'] as $child) {
$foreign->with($child);
}
$foreign = $foreign->all();
foreach ($results as $rrow) {
foreach ($foreign as $frow) {
if ($frow->$foreign_pkname == $rrow->{$local_index}) {
$rrow->add_foreign($relationship['name'], $frow);
continue 2;
}
}
$rrow->add_foreign($relationship['name'], null);
}
}
// Return
return $results;
}
protected function select_row($id) {
$dbh = $this->getdbh();
$sql = 'SELECT * FROM ' . $this->enquote($this->tablename) . ' WHERE ' .
$this->enquote($this->pkname) . '=?';
$stmt = $dbh->prepare($sql);
$stmt->bindValue(1, (int)$id);
$stmt->execute();
$rs = $stmt->fetch(PDO::FETCH_ASSOC);
if ($rs) {
foreach ($rs as $key => $val) {
if (isset($this->rs[$key])) {
$this->rs[$key] = $val;
}
}
$this->exists = true;
$this->dirty = array();
}
return $this;
}
protected function insert_row() {
$dbh = $this->getdbh();
$pkname = $this->pkname;
$s1 = $s2 = '';
foreach ($this->rs as $k => $v) {
if ($k != $pkname || $v) {
$s1 .= ',' . $this->enquote($k);
$s2 .= ',?';
}
}
$s1 = substr($s1, 1); // Remove leading comma
$s2 = substr($s2, 1); // Remove leading comma
$sql = 'INSERT INTO ' . $this->enquote($this->tablename) . ' (' . $s1 .
') VALUES (' . $s2 . ')';
$stmt = $dbh->prepare($sql);
$i = 0;
foreach ($this->rs as $k => $v) {
if ($k != $pkname || $v) {
$stmt->bindValue(++$i, $v);
}
}
$stmt->execute();
if ($stmt->rowCount() == 0) {
return false;
}
$id = $this->rs[$pkname] ? $this->rs[$pkname] : $dbh->lastInsertId();
$this->select_row($id);
return $this;
}
protected function update_row() {
$dbh = $this->getdbh();
$s = '';
foreach ($this->rs as $k => $v) {
$s .= ',' . $this->enquote($k) . '=?';
}
$s = substr($s, 1); // Remove leading comma
$sql = 'UPDATE ' . $this->enquote($this->tablename) . ' SET ' . $s .
' WHERE ' . $this->enquote($this->pkname) . '=?';
$stmt = $dbh->prepare($sql);
$i = 0;
foreach ($this->rs as $k => $v) {
$stmt->bindValue(++$i, $v);
}
$stmt->bindValue(++$i, $this->rs[$this->pkname]);
$stmt->execute();
if ($stmt->rowCount() > 0) {
$this->exists = true;
$this->dirty = array();
}
return $this;
}
protected function delete_row() {
$dbh = $this->getdbh();
$sql = 'DELETE FROM ' . $this->enquote($this->tablename) . ' WHERE ' .
$this->enquote($this->pkname) . '=?';
$stmt = $dbh->prepare($sql);
$stmt->bindValue(1,$this->rs[$this->pkname]);
return $stmt->execute();
}
protected function add_foreign($name, $foreign) {
$this->foreign[$name] = $foreign;
}
static protected function belongsTo($model) {
return array("type" => "belongsTo",
"foreign_model" => $model,
"local_index" => strtolower($model) . '_id');
}
}
class QueryBuilder {
public $model;
public $conditions = array();
public $order = array();
public $foreign = array();
public $limit = false;
function __construct($model) {
$this->model = $model;
}
function where($column, $operator, $value=false, $and_or="AND") {
$this->conditions[] = compact("column", "operator", "value", "and_or");
return $this;
}
function orWhere($column, $operator, $value=false) {
return $this->where($column, $operator, $value, "OR");
}
function orderBy($column, $dir="ASC") {
$this->order[] = compact("column", "dir");
return $this;
}
function orderByDesc($column) {
return $this->orderBy($column, "DESC");
}
function orderByAsc($column) {
return $this->orderBy($column, "ASC");
}
function with($name) {
$parts = explode('.', $name);
if (count($parts) == 1) {
$relationship = call_user_func(array($this->model, $name));
$relationship['name'] = $name;
$relationship['child'] = array();
$this->foreign[] = $relationship;
} elseif (count($parts) > 1) {
$parent = $parts[0];
$child = implode('.', array_slice($parts, 1));
foreach ($this->foreign as &$foreign) {
if ($foreign['name'] == $parent) {
$foreign['child'][] = $child;
}
}
}
return $this;
}
function all() {
$this->limit = false;
return static::execute_query($this);
}
function get($number=1) {
$this->limit = $number;
return static::execute_query($this);
}
static protected function execute_query($query) {
$model = $query->model;
return $model::execute_query($query);
}
}
function model_column($arr, $column) {
$results = array();
foreach ($arr as $model) {
$results[] = $model->$column;
}
return $results;
}
//===============================================================
// Controller
//===============================================================
class Controller extends KISS_Controller {
function __construct($controller_path, $web_folder) {
$this->controller_path = $controller_path;
$this->web_folder = $web_folder;
$this->match_route_table()->add_cgi_params()->route_request();
}
function match_route_table() {
$uri = $this->get_req_uri();
if ($uri === false) $this->request_not_found("Outside application path");
$verb = isset($_POST['_method']) ? strtoupper($_POST['_method']) :
strtoupper($_SERVER['REQUEST_METHOD']);
$match = Route::do_route($uri, $verb);
if (!$match) $this->request_not_found($this->make_error_message($verb));
if (MAINT_MODE && !$match['maint'] &&
(!Auth::check() || Auth::user()->admin)) {
util::service_unavailable();
}
$this->params = $match['params'];
$GLOBALS['controller'] = $match['controller'];
$controller_parts = explode('/', $match['controller']);
$this->controller = $controller_parts[0];
$this->action = $controller_parts[1];
return $this;
}
function add_cgi_params() {
$this->params = array_merge($this->params, $_GET);
return $this;
}
function route_request() {
$this->load_controller();
$this->load_common();
$this->call_action();
return $this;
}
function get_req_uri() {
$uri = $_SERVER['REQUEST_URI'];
if ($this->web_folder) {
if (strpos($uri, $this->web_folder) === 0) {
// Remove application root path
$uri = substr($uri, strlen($this->web_folder));
} else {
return false;
}
}
if (!$uri) {
$uri = '/'; // Put root slash if string is empty
}
return $uri;
}
function load_controller() {
$controllerfile = $this->controller_path . $this->controller . '/' .
$this->action . '.php';
if (!preg_match('#^[A-Za-z0-9_-]+$#', $this->controller) ||
!file_exists($controllerfile)) {
$this->request_not_found('Controller file not found' . $controllerfile);
}
require($controllerfile);
}
function load_common() {
$commonfile = $this->controller_path . $this->controller . '/common.php';
if (file_exists($commonfile)) {
require($commonfile);
}
}
function call_action() {
$function = '_' . $this->action;
if (!preg_match('#^[A-Za-z_][A-Za-z0-9_-]*$#', $function) ||
!function_exists($function)) {
$this->request_not_found('Function not found: '.$function);
}
$reflect = new ReflectionFunction($function);
$params = array();
foreach ($reflect->getParameters() as $param) {
if (isset($this->params[$param->getName()])) {
$params[] = $this->params[$param->getName()];
} elseif ($param->isDefaultValueAvailable()) {
$params[] = $param->getDefaultValue();
} else {
$params[] = null;
}
}
call_user_func_array($function, $params);
}
function make_error_message($verb) {
$msg = 'No route found for ' . $this->get_req_uri();
if ($this->web_folder) {
$msg .= ' with root folder "' . $this->web_folder . '"';
}
$msg .= ' using ' . $verb . ' method.';
return $msg;
}
function request_not_found($msg='') {
util::not_found($msg);
}
}
//===============================================================
// View
//===============================================================
class View extends KISS_View {
static function output($view, $data=array()) {
$data['view'] = $view;
if ($view) {
$data['body'][] = View::do_fetch(APP_PATH . "views/" . $view . '.php', $data);
}
View::do_dump(APP_PATH . "views/layout.php", $data);
}
static function auto($data=array()) {
static::output($GLOBALS['controller'], $data);
}
}